EntityViewDisplayEditForm.php 5.98 KB
Newer Older
1
2
<?php

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\Core\Link;
10
use Drupal\field_ui\FieldUI;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13

/**
14
 * Edit form for the EntityViewDisplay entity type.
15
 */
16
class EntityViewDisplayEditForm extends EntityDisplayFormBase {
17

18
19
20
21
22
  /**
   * {@inheritdoc}
   */
  protected $displayContext = 'view';

23
24
25
26
27
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
28
      $container->get('plugin.manager.field.field_type'),
29
30
      $container->get('plugin.manager.field.formatter')
    );
31
32
  }

33
  /**
34
   * {@inheritdoc}
35
   */
36
37
  protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
    $field_row = parent::buildFieldRow($field_definition, $form, $form_state);
38
39

    $field_name = $field_definition->getName();
40
    $display_options = $this->entity->getComponent($field_name);
41
42
43
44
45

    // Insert the label column.
    $label = array(
      'label' => array(
        '#type' => 'select',
46
        '#title' => $this->t('Label display for @title', array('@title' => $field_definition->getLabel())),
47
48
49
        '#title_display' => 'invisible',
        '#options' => $this->getFieldLabelOptions(),
        '#default_value' => $display_options ? $display_options['label'] : 'above',
50
51
52
      ),
    );

53
54
    $label_position = array_search('plugin', array_keys($field_row));
    $field_row = array_slice($field_row, 0, $label_position, TRUE) + $label + array_slice($field_row, $label_position, count($field_row) - 1, TRUE);
55

56
    // Update the (invisible) title of the 'plugin' column.
57
    $field_row['plugin']['#title'] = $this->t('Formatter for @title', array('@title' => $field_definition->getLabel()));
58
    if (!empty($field_row['plugin']['settings_edit_form']) && ($plugin = $this->entity->getRenderer($field_name))) {
59
      $plugin_type_info = $plugin->getPluginDefinition();
60
      $field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Format settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
61
62
    }

63
64
    return $field_row;
  }
65

66
67
68
  /**
   * {@inheritdoc}
   */
69
70
  protected function buildExtraFieldRow($field_id, $extra_field) {
    $extra_field_row = parent::buildExtraFieldRow($field_id, $extra_field);
71
72
73
74
75
76

    // Insert an empty placeholder for the label column.
    $label = array(
      'empty_cell' => array(
        '#markup' => '&nbsp;'
      )
77
    );
78
79
    $label_position = array_search('plugin', array_keys($extra_field_row));
    $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);
80

81
    return $extra_field_row;
82
83
84
  }

  /**
85
   * {@inheritdoc}
86
   */
87
88
  protected function getEntityDisplay($entity_type_id, $bundle, $mode) {
    return entity_get_display($entity_type_id, $bundle, $mode);
89
  }
90

91
92
93
94
  /**
   * {@inheritdoc}
   */
  protected function getDefaultPlugin($field_type) {
95
    return isset($this->fieldTypes[$field_type]['default_formatter']) ? $this->fieldTypes[$field_type]['default_formatter'] : NULL;
96
  }
97

98
99
100
101
  /**
   * {@inheritdoc}
   */
  protected function getDisplayModes() {
102
    return $this->entityManager->getViewModes($this->entity->getTargetEntityTypeId());
103
  }
104

105
106
107
108
109
110
111
  /**
   * {@inheritdoc}
   */
  protected function getDisplayModeOptions() {
    return $this->entityManager->getViewModeOptions($this->entity->getTargetEntityTypeId());
  }

112
113
114
115
116
117
118
119
120
121
122
  /**
   * {@inheritdoc}
   */
  protected function getDisplayModesLink() {;
    return [
      '#type' => 'link',
      '#title' => t('Manage view modes'),
      '#url' => Url::fromRoute('entity.entity_view_mode.collection'),
    ];
  }

123
  /**
124
   * {@inheritdoc}
125
126
127
   */
  protected function getTableHeader() {
    return array(
128
129
130
131
132
      $this->t('Field'),
      $this->t('Weight'),
      $this->t('Parent'),
      $this->t('Label'),
      array('data' => $this->t('Format'), 'colspan' => 3),
133
134
    );
  }
135

136
137
138
  /**
   * {@inheritdoc}
   */
139
  protected function getOverviewUrl($mode) {
140
    $entity_type = $this->entityManager->getDefinition($this->entity->getTargetEntityTypeId());
141
    return Url::fromRoute('entity.entity_view_display.' . $this->entity->getTargetEntityTypeId() . '.view_mode', [
142
      'view_mode_name' => $mode,
143
    ] + FieldUI::getRouteBundleParameter($entity_type, $this->entity->getTargetBundle()));
144
  }
145

146
147
148
149
150
151
152
153
  /**
   * Returns an array of visibility options for field labels.
   *
   * @return array
   *   An array of visibility options.
   */
  protected function getFieldLabelOptions() {
    return array(
154
155
156
      'above' => $this->t('Above'),
      'inline' => $this->t('Inline'),
      'hidden' => '- ' . $this->t('Hidden') . ' -',
157
      'visually_hidden' => '- ' . $this->t('Visually Hidden') . ' -',
158
159
    );
  }
160

161
162
163
  /**
   * {@inheritdoc}
   */
164
  protected function thirdPartySettingsForm(PluginSettingsInterface $plugin, FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
165
166
167
168
169
170
171
    $settings_form = array();
    // Invoke hook_field_formatter_third_party_settings_form(), keying resulting
    // subforms by module name.
    foreach ($this->moduleHandler->getImplementations('field_formatter_third_party_settings_form') as $module) {
      $settings_form[$module] = $this->moduleHandler->invoke($module, 'field_formatter_third_party_settings_form', array(
        $plugin,
        $field_definition,
172
        $this->entity->getMode(),
173
174
175
176
177
        $form,
        $form_state,
      ));
    }
    return $settings_form;
178
  }
179
180

  /**
181
   * {@inheritdoc}
182
   */
183
  protected function alterSettingsSummary(array &$summary, PluginSettingsInterface $plugin, FieldDefinitionInterface $field_definition) {
184
185
    $context = array(
      'formatter' => $plugin,
186
      'field_definition' => $field_definition,
187
      'view_mode' => $this->entity->getMode(),
188
    );
189
    $this->moduleHandler->alter('field_formatter_settings_summary', $summary, $context);
190
191
  }

192
}