field_ui.api.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Field UI module.
  5. */
  6. /**
  7. * @addtogroup field_types
  8. * @{
  9. */
  10. /**
  11. * Allow modules to add settings to field formatters provided by other modules.
  12. *
  13. * @param \Drupal\Core\Field\FormatterInterface $plugin
  14. * The instantiated field formatter plugin.
  15. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  16. * The field definition.
  17. * @param $view_mode
  18. * The entity view mode.
  19. * @param array $form
  20. * The (entire) configuration form array.
  21. * @param \Drupal\Core\Form\FormStateInterface $form_state
  22. * The form state.
  23. *
  24. * @return array
  25. * Returns the form array to be built.
  26. *
  27. * @see \Drupal\field_ui\DisplayOverView
  28. */
  29. 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) {
  30. $element = [];
  31. // Add a 'my_setting' checkbox to the settings form for 'foo_formatter' field
  32. // formatters.
  33. if ($plugin->getPluginId() == 'foo_formatter') {
  34. $element['my_setting'] = [
  35. '#type' => 'checkbox',
  36. '#title' => t('My setting'),
  37. '#default_value' => $plugin->getThirdPartySetting('my_module', 'my_setting'),
  38. ];
  39. }
  40. return $element;
  41. }
  42. /**
  43. * Allow modules to add settings to field widgets provided by other modules.
  44. *
  45. * @param \Drupal\Core\Field\WidgetInterface $plugin
  46. * The instantiated field widget plugin.
  47. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  48. * The field definition.
  49. * @param $form_mode
  50. * The entity form mode.
  51. * @param array $form
  52. * The (entire) configuration form array.
  53. * @param \Drupal\Core\Form\FormStateInterface $form_state
  54. * The form state.
  55. *
  56. * @return array
  57. * Returns the form array to be built.
  58. *
  59. * @see \Drupal\field_ui\FormDisplayOverView
  60. */
  61. 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) {
  62. $element = [];
  63. // Add a 'my_setting' checkbox to the settings form for 'foo_widget' field
  64. // widgets.
  65. if ($plugin->getPluginId() == 'foo_widget') {
  66. $element['my_setting'] = [
  67. '#type' => 'checkbox',
  68. '#title' => t('My setting'),
  69. '#default_value' => $plugin->getThirdPartySetting('my_module', 'my_setting'),
  70. ];
  71. }
  72. return $element;
  73. }
  74. /**
  75. * Alters the field formatter settings summary.
  76. *
  77. * @param array $summary
  78. * An array of summary messages.
  79. * @param $context
  80. * An associative array with the following elements:
  81. * - formatter: The formatter object.
  82. * - field_definition: The field definition.
  83. * - view_mode: The view mode being configured.
  84. *
  85. * @see \Drupal\field_ui\DisplayOverView
  86. */
  87. function hook_field_formatter_settings_summary_alter(&$summary, $context) {
  88. // Append a message to the summary when an instance of foo_formatter has
  89. // mysetting set to TRUE for the current view mode.
  90. if ($context['formatter']->getPluginId() == 'foo_formatter') {
  91. if ($context['formatter']->getThirdPartySetting('my_module', 'my_setting')) {
  92. $summary[] = t('My setting enabled.');
  93. }
  94. }
  95. }
  96. /**
  97. * Alters the field widget settings summary.
  98. *
  99. * @param array $summary
  100. * An array of summary messages.
  101. * @param array $context
  102. * An associative array with the following elements:
  103. * - widget: The widget object.
  104. * - field_definition: The field definition.
  105. * - form_mode: The form mode being configured.
  106. *
  107. * @see \Drupal\field_ui\FormDisplayOverView
  108. */
  109. function hook_field_widget_settings_summary_alter(&$summary, $context) {
  110. // Append a message to the summary when an instance of foo_widget has
  111. // mysetting set to TRUE for the current view mode.
  112. if ($context['widget']->getPluginId() == 'foo_widget') {
  113. if ($context['widget']->getThirdPartySetting('my_module', 'my_setting')) {
  114. $summary[] = t('My setting enabled.');
  115. }
  116. }
  117. }
  118. /**
  119. * @} End of "addtogroup field_types".
  120. */