field_ui.api.php 4 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * Hooks provided by the Field UI module.
 */

/**
9
 * @addtogroup field_types
10 11 12
 * @{
 */

13
/**
14
 * Allow modules to add settings to field formatters provided by other modules.
15
 *
16 17 18 19 20 21 22 23
 * @param \Drupal\Core\Field\FormatterInterface $plugin
 *   The instantiated field formatter plugin.
 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
 *   The field definition.
 * @param $view_mode
 *   The entity view mode.
 * @param array $form
 *   The (entire) configuration form array.
24
 * @param \Drupal\Core\Form\FormStateInterface $form_state
25 26 27 28
 *   The form state.
 *
 * @return array
 *   Returns the form array to be built.
29
 *
30
 * @see \Drupal\field_ui\DisplayOverView
31
 */
32
function hook_field_formatter_third_party_settings_form(\Drupal\Core\Field\FormatterInterface $plugin, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, $view_mode, $form, \Drupal\Core\Form\FormStateInterface $form_state) {
33
  $element = [];
34 35 36
  // Add a 'my_setting' checkbox to the settings form for 'foo_formatter' field
  // formatters.
  if ($plugin->getPluginId() == 'foo_formatter') {
37
    $element['my_setting'] = [
38 39
      '#type' => 'checkbox',
      '#title' => t('My setting'),
40
      '#default_value' => $plugin->getThirdPartySetting('my_module', 'my_setting'),
41
    ];
42
  }
43
  return $element;
44 45
}

46
/**
47
 * Allow modules to add settings to field widgets provided by other modules.
48
 *
49 50 51 52 53 54 55 56
 * @param \Drupal\Core\Field\WidgetInterface $plugin
 *   The instantiated field widget plugin.
 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
 *   The field definition.
 * @param $form_mode
 *   The entity form mode.
 * @param array $form
 *   The (entire) configuration form array.
57
 * @param \Drupal\Core\Form\FormStateInterface $form_state
58 59 60 61
 *   The form state.
 *
 * @return array
 *   Returns the form array to be built.
62
 *
63
 * @see \Drupal\field_ui\FormDisplayOverView
64
 */
65
function hook_field_widget_third_party_settings_form(\Drupal\Core\Field\WidgetInterface $plugin, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, $form_mode, $form, \Drupal\Core\Form\FormStateInterface $form_state) {
66
  $element = [];
67 68 69
  // Add a 'my_setting' checkbox to the settings form for 'foo_widget' field
  // widgets.
  if ($plugin->getPluginId() == 'foo_widget') {
70
    $element['my_setting'] = [
71 72
      '#type' => 'checkbox',
      '#title' => t('My setting'),
73
      '#default_value' => $plugin->getThirdPartySetting('my_module', 'my_setting'),
74
    ];
75
  }
76
  return $element;
77 78
}

79
/**
80
 * Alters the field formatter settings summary.
81
 *
82 83
 * @param array $summary
 *   An array of summary messages.
84 85
 * @param $context
 *   An associative array with the following elements:
86
 *   - formatter: The formatter object.
87
 *   - field_definition: The field definition.
88
 *   - view_mode: The view mode being configured.
89
 *
90
 * @see \Drupal\field_ui\DisplayOverView
91 92
 */
function hook_field_formatter_settings_summary_alter(&$summary, $context) {
93
  // Append a message to the summary when an instance of foo_formatter has
94
  // mysetting set to TRUE for the current view mode.
95
  if ($context['formatter']->getPluginId() == 'foo_formatter') {
96
    if ($context['formatter']->getThirdPartySetting('my_module', 'my_setting')) {
97
      $summary[] = t('My setting enabled.');
98 99 100 101
    }
  }
}

102 103 104 105
/**
 * Alters the field widget settings summary.
 *
 * @param array $summary
106
 *   An array of summary messages.
107 108 109
 * @param array $context
 *   An associative array with the following elements:
 *   - widget: The widget object.
110
 *   - field_definition: The field definition.
111 112
 *   - form_mode: The form mode being configured.
 *
113
 * @see \Drupal\field_ui\FormDisplayOverView
114 115
 */
function hook_field_widget_settings_summary_alter(&$summary, $context) {
116
  // Append a message to the summary when an instance of foo_widget has
117
  // mysetting set to TRUE for the current view mode.
118 119
  if ($context['widget']->getPluginId() == 'foo_widget') {
    if ($context['widget']->getThirdPartySetting('my_module', 'my_setting')) {
120 121 122 123 124
      $summary[] = t('My setting enabled.');
    }
  }
}

125
/**
126
 * @} End of "addtogroup field_types".
127
 */