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

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

namespace Drupal\field_ui;

10 11 12
use Drupal\Component\Utility\NestedArray;
use Drupal\entity\EntityDisplayBaseInterface;
use Drupal\field\FieldInstanceInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14 15 16 17

/**
 * Field UI display overview form.
 */
18
class DisplayOverview extends DisplayOverviewBase {
19 20 21 22 23 24

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

31
  /**
32
   * {@inheritdoc}
33
   */
34
  public function getFormId() {
35 36
    return 'field_ui_display_overview_form';
  }
37

38
  /**
39
   * {@inheritdoc}
40
   */
41 42 43 44 45 46 47 48
  protected function buildFieldRow($field_id, FieldInstanceInterface $instance, EntityDisplayBaseInterface $entity_display, array $form, array &$form_state) {
    $field_row = parent::buildFieldRow($field_id, $instance, $entity_display, $form, $form_state);
    $display_options = $entity_display->getComponent($field_id);

    // Insert the label column.
    $label = array(
      'label' => array(
        '#type' => 'select',
49
        '#title' => $this->t('Label display for @title', array('@title' => $instance->getFieldLabel())),
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' => $instance->getFieldLabel()));
61 62
    if (!empty($field_row['plugin']['settings_edit_form'])) {
      $plugin_type_info = $entity_display->getRenderer($field_id)->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 72 73 74 75 76 77 78 79
  /**
   * {@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;'
      )
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 92
  protected function getEntityDisplay($mode) {
    return entity_get_display($this->entity_type, $this->bundle, $mode);
  }
93

94 95 96 97 98 99
  /**
   * {@inheritdoc}
   */
  protected function getExtraFields() {
    return field_info_extra_fields($this->entity_type, $this->bundle, 'display');
  }
100

101 102 103 104 105 106 107 108 109 110 111 112
  /**
   * {@inheritdoc}
   */
  protected function getPlugin($instance, $configuration) {
    $plugin = NULL;

    if ($configuration && $configuration['type'] != 'hidden') {
      $plugin = $this->pluginManager->getInstance(array(
        'field_definition' => $instance,
        'view_mode' => $this->mode,
        'configuration' => $configuration
      ));
113
    }
114

115
    return $plugin;
116 117 118
  }

  /**
119
   * {@inheritdoc}
120
   */
121
  protected function getPluginOptions($field_type) {
122
    return parent::getPluginOptions($field_type) + array('hidden' => '- ' . $this->t('Hidden') . ' -');
123
  }
124

125 126 127 128 129 130
  /**
   * {@inheritdoc}
   */
  protected function getDefaultPlugin($field_type) {
    return $this->fieldTypes[$field_type]['default_formatter'];
  }
131

132 133 134 135 136
  /**
   * {@inheritdoc}
   */
  protected function getDisplayModes() {
    return entity_get_view_modes($this->entity_type);
137 138 139
  }

  /**
140
   * {@inheritdoc}
141
   */
142 143
  protected function getDisplayType() {
    return 'entity_display';
144
  }
145

146 147 148 149 150
  /**
   * {@inheritdoc
   */
  protected function getTableHeader() {
    return array(
151 152 153 154 155
      $this->t('Field'),
      $this->t('Weight'),
      $this->t('Parent'),
      $this->t('Label'),
      array('data' => $this->t('Format'), 'colspan' => 3),
156 157
    );
  }
158

159 160 161 162 163 164
  /**
   * {@inheritdoc}
   */
  protected function getOverviewPath($mode) {
    return $this->entityManager->getAdminPath($this->entity_type, $this->bundle) . "/display/$mode";
  }
165

166 167 168 169 170 171 172 173
  /**
   * Returns an array of visibility options for field labels.
   *
   * @return array
   *   An array of visibility options.
   */
  protected function getFieldLabelOptions() {
    return array(
174 175 176
      'above' => $this->t('Above'),
      'inline' => $this->t('Inline'),
      'hidden' => '- ' . $this->t('Hidden') . ' -',
177 178
    );
  }
179

180 181 182 183 184 185 186 187 188 189 190 191
  /**
   * {@inheritdoc}
   */
  protected function alterSettingsForm(array &$settings_form, $plugin, FieldInstanceInterface $instance, array $form, array &$form_state) {
    $context = array(
      'formatter' => $plugin,
      'field' => $instance->getField(),
      'instance' => $instance,
      'view_mode' => $this->mode,
      'form' => $form,
    );
    drupal_alter('field_formatter_settings_form', $settings_form, $form_state, $context);
192
  }
193 194

  /**
195
   * {@inheritdoc}
196
   */
197 198 199 200 201 202 203 204
  protected function alterSettingsSummary(array &$summary, $plugin, FieldInstanceInterface $instance) {
    $context = array(
      'formatter' => $plugin,
      'field' => $instance->getField(),
      'instance' => $instance,
      'view_mode' => $this->mode,
    );
    drupal_alter('field_formatter_settings_summary', $summary, $context);
205 206
  }

207
}