bd82d682907b4cf6d8b3a5ec87896c0f5fec8d52
[yaffs-website] / web / core / modules / field_ui / src / Form / EntityViewDisplayEditForm.php
1 <?php
2
3 namespace Drupal\field_ui\Form;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\PluginSettingsInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9 use Drupal\field_ui\FieldUI;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Edit form for the EntityViewDisplay entity type.
14  *
15  * @internal
16  */
17 class EntityViewDisplayEditForm extends EntityDisplayFormBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected $displayContext = 'view';
23
24   /**
25    * {@inheritdoc}
26    */
27   public static function create(ContainerInterface $container) {
28     return new static(
29       $container->get('plugin.manager.field.field_type'),
30       $container->get('plugin.manager.field.formatter')
31     );
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
38     $field_row = parent::buildFieldRow($field_definition, $form, $form_state);
39
40     $field_name = $field_definition->getName();
41     $display_options = $this->entity->getComponent($field_name);
42
43     // Insert the label column.
44     $label = [
45       'label' => [
46         '#type' => 'select',
47         '#title' => $this->t('Label display for @title', ['@title' => $field_definition->getLabel()]),
48         '#title_display' => 'invisible',
49         '#options' => $this->getFieldLabelOptions(),
50         '#default_value' => $display_options ? $display_options['label'] : 'above',
51       ],
52     ];
53
54     $label_position = array_search('plugin', array_keys($field_row));
55     $field_row = array_slice($field_row, 0, $label_position, TRUE) + $label + array_slice($field_row, $label_position, count($field_row) - 1, TRUE);
56
57     // Update the (invisible) title of the 'plugin' column.
58     $field_row['plugin']['#title'] = $this->t('Formatter for @title', ['@title' => $field_definition->getLabel()]);
59     if (!empty($field_row['plugin']['settings_edit_form']) && ($plugin = $this->entity->getRenderer($field_name))) {
60       $plugin_type_info = $plugin->getPluginDefinition();
61       $field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Format settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
62     }
63
64     return $field_row;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   protected function buildExtraFieldRow($field_id, $extra_field) {
71     $extra_field_row = parent::buildExtraFieldRow($field_id, $extra_field);
72
73     // Insert an empty placeholder for the label column.
74     $label = [
75       'empty_cell' => [
76         '#markup' => '&nbsp;'
77       ]
78     ];
79     $label_position = array_search('plugin', array_keys($extra_field_row));
80     $extra_field_row = array_slice($extra_field_row, 0, $label_position, TRUE) + $label + array_slice($extra_field_row, $label_position, count($extra_field_row) - 1, TRUE);
81
82     return $extra_field_row;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   protected function getEntityDisplay($entity_type_id, $bundle, $mode) {
89     return entity_get_display($entity_type_id, $bundle, $mode);
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   protected function getDefaultPlugin($field_type) {
96     return isset($this->fieldTypes[$field_type]['default_formatter']) ? $this->fieldTypes[$field_type]['default_formatter'] : NULL;
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   protected function getDisplayModes() {
103     return $this->entityManager->getViewModes($this->entity->getTargetEntityTypeId());
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   protected function getDisplayModeOptions() {
110     return $this->entityManager->getViewModeOptions($this->entity->getTargetEntityTypeId());
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   protected function getDisplayModesLink() {
117     return [
118       '#type' => 'link',
119       '#title' => t('Manage view modes'),
120       '#url' => Url::fromRoute('entity.entity_view_mode.collection'),
121     ];
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   protected function getTableHeader() {
128     return [
129       $this->t('Field'),
130       $this->t('Weight'),
131       $this->t('Parent'),
132       $this->t('Region'),
133       $this->t('Label'),
134       ['data' => $this->t('Format'), 'colspan' => 3],
135     ];
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   protected function getOverviewUrl($mode) {
142     $entity_type = $this->entityManager->getDefinition($this->entity->getTargetEntityTypeId());
143     return Url::fromRoute('entity.entity_view_display.' . $this->entity->getTargetEntityTypeId() . '.view_mode', [
144       'view_mode_name' => $mode,
145     ] + FieldUI::getRouteBundleParameter($entity_type, $this->entity->getTargetBundle()));
146   }
147
148   /**
149    * Returns an array of visibility options for field labels.
150    *
151    * @return array
152    *   An array of visibility options.
153    */
154   protected function getFieldLabelOptions() {
155     return [
156       'above' => $this->t('Above'),
157       'inline' => $this->t('Inline'),
158       'hidden' => '- ' . $this->t('Hidden') . ' -',
159       'visually_hidden' => '- ' . $this->t('Visually Hidden') . ' -',
160     ];
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   protected function thirdPartySettingsForm(PluginSettingsInterface $plugin, FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
167     $settings_form = [];
168     // Invoke hook_field_formatter_third_party_settings_form(), keying resulting
169     // subforms by module name.
170     foreach ($this->moduleHandler->getImplementations('field_formatter_third_party_settings_form') as $module) {
171       $settings_form[$module] = $this->moduleHandler->invoke($module, 'field_formatter_third_party_settings_form', [
172         $plugin,
173         $field_definition,
174         $this->entity->getMode(),
175         $form,
176         $form_state,
177       ]);
178     }
179     return $settings_form;
180   }
181
182   /**
183    * {@inheritdoc}
184    */
185   protected function alterSettingsSummary(array &$summary, PluginSettingsInterface $plugin, FieldDefinitionInterface $field_definition) {
186     $context = [
187       'formatter' => $plugin,
188       'field_definition' => $field_definition,
189       'view_mode' => $this->entity->getMode(),
190     ];
191     $this->moduleHandler->alter('field_formatter_settings_summary', $summary, $context);
192   }
193
194 }