DisplayOverview.php 6.2 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\field_ui\DisplayOverview.
6 7 8 9
 */

namespace Drupal\field_ui;

10
use Drupal\Core\Field\FieldDefinitionInterface;
11
use Drupal\Core\Entity\Display\EntityDisplayInterface;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13 14 15 16

/**
 * Field UI display overview form.
 */
17
class DisplayOverview extends DisplayOverviewBase {
18

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

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

35
  /**
36
   * {@inheritdoc}
37
   */
38
  public function getFormId() {
39 40
    return 'field_ui_display_overview_form';
  }
41

42 43 44 45 46 47 48 49 50 51 52
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state, $entity_type = NULL, $bundle = NULL) {
    if ($this->getRequest()->attributes->has('view_mode_name')) {
      $this->mode = $this->getRequest()->attributes->get('view_mode_name');
    }

    return parent::buildForm($form, $form_state, $entity_type, $bundle);
  }

53
  /**
54
   * {@inheritdoc}
55
   */
56 57 58 59 60
  protected function buildFieldRow(FieldDefinitionInterface $field_definition, EntityDisplayInterface $entity_display, array $form, array &$form_state) {
    $field_row = parent::buildFieldRow($field_definition, $entity_display, $form, $form_state);

    $field_name = $field_definition->getName();
    $display_options = $entity_display->getComponent($field_name);
61 62 63 64 65

    // Insert the label column.
    $label = array(
      'label' => array(
        '#type' => 'select',
66
        '#title' => $this->t('Label display for @title', array('@title' => $field_definition->getLabel())),
67 68 69
        '#title_display' => 'invisible',
        '#options' => $this->getFieldLabelOptions(),
        '#default_value' => $display_options ? $display_options['label'] : 'above',
70 71 72
      ),
    );

73 74
    $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);
75

76
    // Update the (invisible) title of the 'plugin' column.
77 78 79
    $field_row['plugin']['#title'] = $this->t('Formatter for @title', array('@title' => $field_definition->getLabel()));
    if (!empty($field_row['plugin']['settings_edit_form']) && ($plugin = $entity_display->getRenderer($field_name))) {
      $plugin_type_info = $plugin->getPluginDefinition();
80
      $field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Format settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
81 82
    }

83 84
    return $field_row;
  }
85

86 87 88 89 90 91 92 93 94 95 96
  /**
   * {@inheritdoc}
   */
  protected function buildExtraFieldRow($field_id, $extra_field, $entity_display) {
    $extra_field_row = parent::buildExtraFieldRow($field_id, $extra_field, $entity_display);

    // Insert an empty placeholder for the label column.
    $label = array(
      'empty_cell' => array(
        '#markup' => '&nbsp;'
      )
97
    );
98 99
    $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);
100

101
    return $extra_field_row;
102 103 104
  }

  /**
105
   * {@inheritdoc}
106
   */
107 108 109
  protected function getEntityDisplay($mode) {
    return entity_get_display($this->entity_type, $this->bundle, $mode);
  }
110

111 112 113
  /**
   * {@inheritdoc}
   */
114
  protected function getPlugin(FieldDefinitionInterface $field_definition, $configuration) {
115 116 117 118
    $plugin = NULL;

    if ($configuration && $configuration['type'] != 'hidden') {
      $plugin = $this->pluginManager->getInstance(array(
119
        'field_definition' => $field_definition,
120 121 122
        'view_mode' => $this->mode,
        'configuration' => $configuration
      ));
123
    }
124

125
    return $plugin;
126 127 128
  }

  /**
129
   * {@inheritdoc}
130
   */
131
  protected function getPluginOptions($field_type) {
132
    return parent::getPluginOptions($field_type) + array('hidden' => '- ' . $this->t('Hidden') . ' -');
133
  }
134

135 136 137 138 139 140
  /**
   * {@inheritdoc}
   */
  protected function getDefaultPlugin($field_type) {
    return $this->fieldTypes[$field_type]['default_formatter'];
  }
141

142 143 144 145 146
  /**
   * {@inheritdoc}
   */
  protected function getDisplayModes() {
    return entity_get_view_modes($this->entity_type);
147 148 149
  }

  /**
150
   * {@inheritdoc}
151
   */
152 153
  protected function getDisplayType() {
    return 'entity_display';
154
  }
155

156 157 158 159 160
  /**
   * {@inheritdoc
   */
  protected function getTableHeader() {
    return array(
161 162 163 164 165
      $this->t('Field'),
      $this->t('Weight'),
      $this->t('Parent'),
      $this->t('Label'),
      array('data' => $this->t('Format'), 'colspan' => 3),
166 167
    );
  }
168

169 170 171
  /**
   * {@inheritdoc}
   */
172 173 174 175 176 177 178 179 180
  protected function getOverviewRoute($mode) {
    return array(
      'route_name' => 'field_ui.display_overview_view_mode_' . $this->entity_type,
      'route_parameters' => array(
        $this->bundleEntityType => $this->bundle,
        'view_mode_name' => $mode,
      ),
      'options' => array(),
    );
181
  }
182

183 184 185 186 187 188 189 190
  /**
   * Returns an array of visibility options for field labels.
   *
   * @return array
   *   An array of visibility options.
   */
  protected function getFieldLabelOptions() {
    return array(
191 192 193
      'above' => $this->t('Above'),
      'inline' => $this->t('Inline'),
      'hidden' => '- ' . $this->t('Hidden') . ' -',
194 195
    );
  }
196

197 198 199
  /**
   * {@inheritdoc}
   */
200
  protected function alterSettingsForm(array &$settings_form, $plugin, FieldDefinitionInterface $field_definition, array $form, array &$form_state) {
201 202
    $context = array(
      'formatter' => $plugin,
203
      'field_definition' => $field_definition,
204 205 206 207
      'view_mode' => $this->mode,
      'form' => $form,
    );
    drupal_alter('field_formatter_settings_form', $settings_form, $form_state, $context);
208
  }
209 210

  /**
211
   * {@inheritdoc}
212
   */
213
  protected function alterSettingsSummary(array &$summary, $plugin, FieldDefinitionInterface $field_definition) {
214 215
    $context = array(
      'formatter' => $plugin,
216
      'field_definition' => $field_definition,
217 218 219
      'view_mode' => $this->mode,
    );
    drupal_alter('field_formatter_settings_summary', $summary, $context);
220 221
  }

222
}