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