FormDisplayOverview.php 5.98 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\field_ui\FormDisplayOverview.
 */

namespace Drupal\field_ui;

10
use Drupal\Component\Plugin\PluginManagerBase;
11
use Drupal\Core\Config\ConfigFactoryInterface;
12
use Drupal\Core\Entity\Display\EntityDisplayInterface;
13 14
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
15
use Drupal\Core\Field\FieldDefinitionInterface;
16
use Drupal\Core\Field\FieldTypePluginManager;
17 18 19 20 21
use Symfony\Component\DependencyInjection\ContainerInterface;

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

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

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 to use for invoking hooks.
47 48
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   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.widget'),
63 64
      $container->get('module_handler'),
      $container->get('config.factory')
65 66 67 68 69 70
    );
  }

  /**
   * {@inheritdoc}
   */
71
  public function getFormId() {
72 73 74
    return 'field_ui_form_display_overview_form';
  }

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('form_mode_name')) {
      $this->mode = $this->getRequest()->attributes->get('form_mode_name');
    }

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

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

    // Update the (invisible) title of the 'plugin' column.
95 96
    $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))) {
97
      $plugin_type_info = $plugin->getPluginDefinition();
98
      $field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Widget settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    }

    return $field_row;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntityDisplay($mode) {
    return entity_get_form_display($this->entity_type, $this->bundle, $mode);
  }

  /**
   * {@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 123 124 125 126 127
        'form_mode' => $this->mode,
        'configuration' => $configuration
      ));
    }

    return $plugin;
  }

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

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  /**
   * {@inheritdoc}
   */
  protected function getDefaultPlugin($field_type) {
    return $this->fieldTypes[$field_type]['default_widget'];
  }

  /**
   * {@inheritdoc}
   */
  protected function getDisplayModes() {
    return entity_get_form_modes($this->entity_type);
  }

  /**
   * {@inheritdoc}
   */
152 153
  protected function getDisplayType() {
    return 'entity_form_display';
154 155 156
  }

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

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

  /**
   * {@inheritdoc}
   */
185
  protected function alterSettingsForm(array &$settings_form, $plugin, FieldDefinitionInterface $field_definition, array $form, array &$form_state) {
186 187
    $context = array(
      'widget' => $plugin,
188
      'field_definition' => $field_definition,
189 190 191
      'form_mode' => $this->mode,
      'form' => $form,
    );
192
    $this->moduleHandler->alter('field_widget_settings_form', $settings_form, $form_state, $context);
193 194 195 196 197
  }

  /**
   * {@inheritdoc}
   */
198
  protected function alterSettingsSummary(array &$summary, $plugin, FieldDefinitionInterface $field_definition) {
199 200
    $context = array(
      'widget' => $plugin,
201
      'field_definition' => $field_definition,
202 203
      'form_mode' => $this->mode,
    );
204
    $this->moduleHandler->alter('field_widget_settings_summary', $summary, $context);
205 206 207
  }

}