X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Ffield%2Ffield.api.php;h=5115e42b39b3750a6d1d02bfdc4bf67d3b2ad7ba;hb=5b8bb166bfa98770daef9de5c127fc2e6ef02340;hp=b0899ea02b076bd7a1d183d50d172e2009457365;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/web/core/modules/field/field.api.php b/web/core/modules/field/field.api.php index b0899ea02..5115e42b3 100644 --- a/web/core/modules/field/field.api.php +++ b/web/core/modules/field/field.api.php @@ -43,7 +43,6 @@ * @see plugin_api */ - /** * Perform alterations on Field API field types. * @@ -58,6 +57,33 @@ function hook_field_info_alter(&$info) { } } +/** + * Perform alterations on preconfigured field options. + * + * @param array $options + * Array of options as returned from + * \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions(). + * @param string $field_type + * The field type plugin ID. + * + * @see \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions() + */ +function hook_field_ui_preconfigured_options_alter(array &$options, $field_type) { + // If the field is not an "entity_reference"-based field, bail out. + /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */ + $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); + $class = $field_type_manager->getPluginClass($field_type); + if (!is_a($class, 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem', TRUE)) { + return; + } + + // Set the default formatter for media in entity reference fields to be the + // "Rendered entity" formatter. + if (!empty($options['media'])) { + $options['media']['entity_view_display']['type'] = 'entity_reference_entity_view'; + } +} + /** * Forbid a field storage update from occurring. * @@ -137,8 +163,14 @@ function hook_field_widget_info_alter(array &$info) { /** * Alter forms for field widgets provided by other modules. * + * This hook can only modify individual elements within a field widget and + * cannot alter the top level (parent element) for multi-value fields. In most + * cases, you should use hook_field_widget_multivalue_form_alter() instead and + * loop over the elements. + * * @param $element - * The field widget form element as constructed by hook_field_widget_form(). + * The field widget form element as constructed by + * \Drupal\Core\Field\WidgetBaseInterface::form(). * @param $form_state * The current state of the form. * @param $context @@ -152,8 +184,10 @@ function hook_field_widget_info_alter(array &$info) { * - default: A boolean indicating whether the form is being shown as a dummy * form to set default values. * + * @see \Drupal\Core\Field\WidgetBaseInterface::form() * @see \Drupal\Core\Field\WidgetBase::formSingleElement() * @see hook_field_widget_WIDGET_TYPE_form_alter() + * @see hook_field_widget_multivalue_form_alter() */ function hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) { // Add a css class to widget form elements for all fields of type mytype. @@ -171,16 +205,24 @@ function hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInte * specific widget form, rather than using hook_field_widget_form_alter() and * checking the widget type. * + * This hook can only modify individual elements within a field widget and + * cannot alter the top level (parent element) for multi-value fields. In most + * cases, you should use hook_field_widget_multivalue_WIDGET_TYPE_form_alter() + * instead and loop over the elements. + * * @param $element - * The field widget form element as constructed by hook_field_widget_form(). + * The field widget form element as constructed by + * \Drupal\Core\Field\WidgetBaseInterface::form(). * @param $form_state * The current state of the form. * @param $context * An associative array. See hook_field_widget_form_alter() for the structure * and content of the array. * + * @see \Drupal\Core\Field\WidgetBaseInterface::form() * @see \Drupal\Core\Field\WidgetBase::formSingleElement() * @see hook_field_widget_form_alter() + * @see hook_field_widget_multivalue_WIDGET_TYPE_form_alter() */ function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) { // Code here will only act on widgets of type WIDGET_TYPE. For example, @@ -189,6 +231,74 @@ function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\F $element['#autocomplete_route_name'] = 'mymodule.autocomplete_route'; } +/** + * Alter forms for multi-value field widgets provided by other modules. + * + * To alter the individual elements within the widget, loop over + * \Drupal\Core\Render\Element::children($elements). + * + * @param array $elements + * The field widget form elements as constructed by + * \Drupal\Core\Field\WidgetBase::formMultipleElements(). + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * @param array $context + * An associative array containing the following key-value pairs: + * - form: The form structure to which widgets are being attached. This may be + * a full form structure, or a sub-element of a larger form. + * - widget: The widget plugin instance. + * - items: The field values, as a + * \Drupal\Core\Field\FieldItemListInterface object. + * - default: A boolean indicating whether the form is being shown as a dummy + * form to set default values. + * + * @see \Drupal\Core\Field\WidgetBaseInterface::form() + * @see \Drupal\Core\Field\WidgetBase::formMultipleElements() + * @see hook_field_widget_multivalue_WIDGET_TYPE_form_alter() + */ +function hook_field_widget_multivalue_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) { + // Add a css class to widget form elements for all fields of type mytype. + $field_definition = $context['items']->getFieldDefinition(); + if ($field_definition->getType() == 'mytype') { + // Be sure not to overwrite existing attributes. + $elements['#attributes']['class'][] = 'myclass'; + } +} + +/** + * Alter multi-value widget forms for a widget provided by another module. + * + * Modules can implement hook_field_widget_multivalue_WIDGET_TYPE_form_alter() to + * modify a specific widget form, rather than using + * hook_field_widget_form_alter() and checking the widget type. + * + * To alter the individual elements within the widget, loop over + * \Drupal\Core\Render\Element::children($elements). + * + * @param array $elements + * The field widget form elements as constructed by + * \Drupal\Core\Field\WidgetBase::formMultipleElements(). + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * @param array $context + * An associative array. See hook_field_widget_multivalue_form_alter() for + * the structure and content of the array. + * + * @see \Drupal\Core\Field\WidgetBaseInterface::form() + * @see \Drupal\Core\Field\WidgetBase::formMultipleElements() + * @see hook_field_widget_multivalue_form_alter() + */ +function hook_field_widget_multivalue_WIDGET_TYPE_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) { + // Code here will only act on widgets of type WIDGET_TYPE. For example, + // hook_field_widget_multivalue_mymodule_autocomplete_form_alter() will only + // act on widgets of type 'mymodule_autocomplete'. + // Change the autocomplete route for each autocomplete element within the + // multivalue widget. + foreach (Element::children($elements) as $delta => $element) { + $elements[$delta]['#autocomplete_route_name'] = 'mymodule.autocomplete_route'; + } +} + /** * @} End of "defgroup field_widget". */