DisplayOverview.php 7.75 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\Component\Plugin\PluginManagerBase;
11
use Drupal\Core\Config\ConfigFactoryInterface;
12
use Drupal\Core\Field\FieldDefinitionInterface;
13
use Drupal\Core\Entity\Display\EntityDisplayInterface;
14 15 16
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldTypePluginManager;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18 19 20 21

/**
 * Field UI display overview form.
 */
22
class DisplayOverview extends DisplayOverviewBase {
23

24 25 26 27 28
  /**
   * {@inheritdoc}
   */
  protected $displayContext = 'view';

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  /**
   * Stores the module manager.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a new class instance.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Field\FieldTypePluginManager $field_type_manager
   *   The field type manager.
   * @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager
   *   The widget or formatter plugin manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler class to use for invoking hooks.
47
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
48
   *   The configuration factory.
49
   */
50 51
  public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManager $field_type_manager, PluginManagerBase $plugin_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
    parent::__construct($entity_manager, $field_type_manager, $plugin_manager, $config_factory);
52 53 54
    $this->moduleHandler = $module_handler;
  }

55 56 57 58 59
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
60
      $container->get('entity.manager'),
61
      $container->get('plugin.manager.field.field_type'),
62
      $container->get('plugin.manager.field.formatter'),
63 64 65
      $container->get('module_handler'),
      $container->get('config.factory')
  );
66 67
  }

68
  /**
69
   * {@inheritdoc}
70
   */
71
  public function getFormId() {
72 73
    return 'field_ui_display_overview_form';
  }
74

75 76 77
  /**
   * {@inheritdoc}
   */
78
  public function buildForm(array $form, array &$form_state, $entity_type_id = NULL, $bundle = NULL) {
79 80 81 82
    if ($this->getRequest()->attributes->has('view_mode_name')) {
      $this->mode = $this->getRequest()->attributes->get('view_mode_name');
    }

83
    return parent::buildForm($form, $form_state, $entity_type_id, $bundle);
84 85
  }

86
  /**
87
   * {@inheritdoc}
88
   */
89 90 91 92 93
  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);
94 95 96 97 98

    // Insert the label column.
    $label = array(
      'label' => array(
        '#type' => 'select',
99
        '#title' => $this->t('Label display for @title', array('@title' => $field_definition->getLabel())),
100 101 102
        '#title_display' => 'invisible',
        '#options' => $this->getFieldLabelOptions(),
        '#default_value' => $display_options ? $display_options['label'] : 'above',
103 104 105
      ),
    );

106 107
    $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);
108

109
    // Update the (invisible) title of the 'plugin' column.
110 111 112
    $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();
113
      $field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Format settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
114 115
    }

116 117
    return $field_row;
  }
118

119 120 121
  /**
   * {@inheritdoc}
   */
122
  protected function buildExtraFieldRow($field_id, $extra_field, EntityDisplayInterface $entity_display) {
123 124 125 126 127 128 129
    $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;'
      )
130
    );
131 132
    $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);
133

134
    return $extra_field_row;
135 136 137
  }

  /**
138
   * {@inheritdoc}
139
   */
140 141 142
  protected function getEntityDisplay($mode) {
    return entity_get_display($this->entity_type, $this->bundle, $mode);
  }
143

144 145 146
  /**
   * {@inheritdoc}
   */
147
  protected function getPlugin(FieldDefinitionInterface $field_definition, $configuration) {
148 149 150 151
    $plugin = NULL;

    if ($configuration && $configuration['type'] != 'hidden') {
      $plugin = $this->pluginManager->getInstance(array(
152
        'field_definition' => $field_definition,
153 154 155
        'view_mode' => $this->mode,
        'configuration' => $configuration
      ));
156
    }
157

158
    return $plugin;
159 160 161
  }

  /**
162
   * {@inheritdoc}
163
   */
164
  protected function getPluginOptions($field_type) {
165
    return parent::getPluginOptions($field_type) + array('hidden' => '- ' . $this->t('Hidden') . ' -');
166
  }
167

168 169 170 171
  /**
   * {@inheritdoc}
   */
  protected function getDefaultPlugin($field_type) {
172
    return isset($this->fieldTypes[$field_type]['default_formatter']) ? $this->fieldTypes[$field_type]['default_formatter'] : NULL;
173
  }
174

175 176 177 178
  /**
   * {@inheritdoc}
   */
  protected function getDisplayModes() {
179
    return $this->entityManager->getViewModes($this->entity_type);
180 181 182
  }

  /**
183
   * {@inheritdoc}
184
   */
185
  protected function getDisplayType() {
186
    return 'entity_view_display';
187
  }
188

189
  /**
190
   * {@inheritdoc}
191 192 193
   */
  protected function getTableHeader() {
    return array(
194 195 196 197 198
      $this->t('Field'),
      $this->t('Weight'),
      $this->t('Parent'),
      $this->t('Label'),
      array('data' => $this->t('Format'), 'colspan' => 3),
199 200
    );
  }
201

202 203 204
  /**
   * {@inheritdoc}
   */
205 206 207 208 209 210 211 212 213
  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(),
    );
214
  }
215

216 217 218 219 220 221 222 223
  /**
   * Returns an array of visibility options for field labels.
   *
   * @return array
   *   An array of visibility options.
   */
  protected function getFieldLabelOptions() {
    return array(
224 225 226
      'above' => $this->t('Above'),
      'inline' => $this->t('Inline'),
      'hidden' => '- ' . $this->t('Hidden') . ' -',
227 228
    );
  }
229

230 231 232
  /**
   * {@inheritdoc}
   */
233
  protected function alterSettingsForm(array &$settings_form, $plugin, FieldDefinitionInterface $field_definition, array $form, array &$form_state) {
234 235
    $context = array(
      'formatter' => $plugin,
236
      'field_definition' => $field_definition,
237 238 239
      'view_mode' => $this->mode,
      'form' => $form,
    );
240
    $this->moduleHandler->alter('field_formatter_settings_form', $settings_form, $form_state, $context);
241
  }
242 243

  /**
244
   * {@inheritdoc}
245
   */
246
  protected function alterSettingsSummary(array &$summary, $plugin, FieldDefinitionInterface $field_definition) {
247 248
    $context = array(
      'formatter' => $plugin,
249
      'field_definition' => $field_definition,
250 251
      'view_mode' => $this->mode,
    );
252
    $this->moduleHandler->alter('field_formatter_settings_summary', $summary, $context);
253 254
  }

255
}