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\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 19 20 21
  /**
   * {@inheritdoc}
   */
  protected $displayContext = 'view';

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

32
  /**
33
   * {@inheritdoc}
34
   */
35 36
  protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
    $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 42 43 44

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

52 53
    $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);
54

55
    // Update the (invisible) title of the 'plugin' column.
56
    $field_row['plugin']['#title'] = $this->t('Formatter for @title', array('@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 63
    return $field_row;
  }
64

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

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

80
    return $extra_field_row;
81 82 83
  }

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

90 91 92 93
  /**
   * {@inheritdoc}
   */
  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 99 100
  /**
   * {@inheritdoc}
   */
  protected function getDisplayModes() {
101
    return $this->entityManager->getViewModes($this->entity->getTargetEntityTypeId());
102
  }
103

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

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

122
  /**
123
   * {@inheritdoc}
124 125 126
   */
  protected function getTableHeader() {
    return array(
127 128 129
      $this->t('Field'),
      $this->t('Weight'),
      $this->t('Parent'),
130
      $this->t('Region'),
131 132
      $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
}