EntityViewDisplayEditForm.php 6.1 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\field_ui\Form\EntityViewDisplayEditForm.
6 7
 */

8
namespace Drupal\field_ui\Form;
9

10
use Drupal\Core\Field\FieldDefinitionInterface;
11
use Drupal\Core\Field\PluginSettingsInterface;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Url;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15 16

/**
17
 * Edit form for the EntityViewDisplay entity type.
18
 */
19
class EntityViewDisplayEditForm extends EntityDisplayFormBase {
20

21 22 23 24 25
  /**
   * {@inheritdoc}
   */
  protected $displayContext = 'view';

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

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

    $field_name = $field_definition->getName();
43
    $display_options = $this->entity->getComponent($field_name);
44 45 46 47 48

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

56 57
    $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);
58

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

66 67
    return $field_row;
  }
68

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

    // Insert an empty placeholder for the label column.
    $label = array(
      'empty_cell' => array(
        '#markup' => '&nbsp;'
      )
80
    );
81 82
    $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);
83

84
    return $extra_field_row;
85 86 87
  }

  /**
88
   * {@inheritdoc}
89
   */
90 91
  protected function getEntityDisplay($entity_type_id, $bundle, $mode) {
    return entity_get_display($entity_type_id, $bundle, $mode);
92
  }
93

94 95 96
  /**
   * {@inheritdoc}
   */
97
  protected function getPlugin(FieldDefinitionInterface $field_definition, $configuration) {
98 99 100 101
    $plugin = NULL;

    if ($configuration && $configuration['type'] != 'hidden') {
      $plugin = $this->pluginManager->getInstance(array(
102
        'field_definition' => $field_definition,
103
        'view_mode' => $this->entity->mode,
104 105
        'configuration' => $configuration
      ));
106
    }
107

108
    return $plugin;
109 110
  }

111 112 113 114
  /**
   * {@inheritdoc}
   */
  protected function getDefaultPlugin($field_type) {
115
    return isset($this->fieldTypes[$field_type]['default_formatter']) ? $this->fieldTypes[$field_type]['default_formatter'] : NULL;
116
  }
117

118 119 120 121
  /**
   * {@inheritdoc}
   */
  protected function getDisplayModes() {
122
    return $this->entityManager->getViewModes($this->entity->targetEntityType);
123
  }
124

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

138 139 140
  /**
   * {@inheritdoc}
   */
141 142
  protected function getOverviewUrl($mode) {
    $entity_type = $this->entityManager->getDefinition($this->entity->targetEntityType);
143
    $field_entity_type = $entity_type->getBundleEntityType() != 'bundle'?  $entity_type->getBundleEntityType() : $entity_type->id();
144 145
    return Url::fromRoute('entity.entity_view_display.' . $field_entity_type . '.view_mode', [
      $this->bundleEntityTypeId => $this->entity->bundle,
146 147
      'view_mode_name' => $mode,
    ]);
148
  }
149

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

165 166 167
  /**
   * {@inheritdoc}
   */
168
  protected function thirdPartySettingsForm(PluginSettingsInterface $plugin, FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
169 170 171 172 173 174 175
    $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,
176
        $this->entity->mode,
177 178 179 180 181
        $form,
        $form_state,
      ));
    }
    return $settings_form;
182
  }
183 184

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

196
}