FormDisplayOverview.php 4.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

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

namespace Drupal\field_ui;

use Drupal\Component\Utility\NestedArray;
use Drupal\entity\EntityDisplayBaseInterface;
use Drupal\field\FieldInstanceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Field UI form display overview form.
 */
18
class FormDisplayOverview 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.field.field_type'),
27 28 29 30 31 32 33
      $container->get('plugin.manager.field.widget')
    );
  }

  /**
   * {@inheritdoc}
   */
34
  public function getFormId() {
35 36 37
    return 'field_ui_form_display_overview_form';
  }

38 39 40 41 42 43 44 45 46 47 48
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state, $entity_type = NULL, $bundle = NULL) {
    if ($this->getRequest()->attributes->has('form_mode_name')) {
      $this->mode = $this->getRequest()->attributes->get('form_mode_name');
    }

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

49 50 51 52 53 54 55
  /**
   * {@inheritdoc}
   */
  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);

    // Update the (invisible) title of the 'plugin' column.
56
    $field_row['plugin']['#title'] = $this->t('Formatter for @title', array('@title' => $instance->getLabel()));
57 58
    if (!empty($field_row['plugin']['settings_edit_form']) && ($plugin = $entity_display->getRenderer($field_id))) {
      $plugin_type_info = $plugin->getPluginDefinition();
59
      $field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Widget settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }

    return $field_row;
  }

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

  /**
   * {@inheritdoc}
   */
  protected function getExtraFields() {
    return field_info_extra_fields($this->entity_type, $this->bundle, 'form');
  }

  /**
   * {@inheritdoc}
   */
  protected function getPlugin($instance, $configuration) {
    $plugin = NULL;

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

    return $plugin;
  }

96 97 98 99 100 101 102
  /**
   * {@inheritdoc}
   */
  protected function getPluginOptions($field_type) {
    return parent::getPluginOptions($field_type) + array('hidden' => '- ' . t('Hidden') . ' -');
  }

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  /**
   * {@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}
   */
120 121
  protected function getDisplayType() {
    return 'entity_form_display';
122 123 124 125 126 127 128
  }

  /**
   * {@inheritdoc
   */
  protected function getTableHeader() {
    return array(
129 130 131 132
      $this->t('Field'),
      $this->t('Weight'),
      $this->t('Parent'),
      array('data' => $this->t('Widget'), 'colspan' => 3),
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function getOverviewPath($mode) {
    return $this->entityManager->getAdminPath($this->entity_type, $this->bundle) . "/form-display/$mode";
  }

  /**
   * {@inheritdoc}
   */
  protected function alterSettingsForm(array &$settings_form, $plugin, FieldInstanceInterface $instance, array $form, array &$form_state) {
    $context = array(
      'widget' => $plugin,
      'field' => $instance->getField(),
      'instance' => $instance,
      'form_mode' => $this->mode,
      'form' => $form,
    );
    drupal_alter('field_widget_settings_form', $settings_form, $form_state, $context);
  }

  /**
   * {@inheritdoc}
   */
  protected function alterSettingsSummary(array &$summary, $plugin, FieldInstanceInterface $instance) {
    $context = array(
      'widget' => $plugin,
      'field' => $instance->getField(),
      'instance' => $instance,
      'form_mode' => $this->mode,
    );
    drupal_alter('field_widget_settings_summary', $summary, $context);
  }

}