EntityDisplayFormBase::form

public EntityDisplayFormBase::form(array $form, FormStateInterface $form_state)

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/field_ui/src/Form/EntityDisplayFormBase.php, line 136

Class

EntityDisplayFormBase
Base class for EntityDisplay edit forms.

Namespace

Drupal\field_ui\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  $field_definitions = $this->getFieldDefinitions();
  $extra_fields = $this->getExtraFields();

  $form += array(
    '#entity_type' => $this->entity->getTargetEntityTypeId(),
    '#bundle' => $this->entity->getTargetBundle(),
    '#fields' => array_keys($field_definitions),
    '#extra' => array_keys($extra_fields),
  );

  if (empty($field_definitions) && empty($extra_fields) && $route_info = FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle())) {
    drupal_set_message($this->t('There are no fields yet added. You can add new fields on the <a href=":link">Manage fields</a> page.', array(':link' => $route_info->toString())), 'warning');
    return $form;
  }

  $table = array(
    '#type' => 'field_ui_table',
    '#header' => $this->getTableHeader(),
    '#regions' => $this->getRegions(),
    '#attributes' => array(
      'class' => array('field-ui-overview'),
      'id' => 'field-display-overview',
    ),
    '#tabledrag' => array(
      array(
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'field-weight',
      ),
      array(
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'field-parent',
        'subgroup' => 'field-parent',
        'source' => 'field-name',
      ),
    ),
  );

  // Field rows.
  foreach ($field_definitions as $field_name => $field_definition) {
    $table[$field_name] = $this->buildFieldRow($field_definition, $form, $form_state);
  }

  // Non-field elements.
  foreach ($extra_fields as $field_id => $extra_field) {
    $table[$field_id] = $this->buildExtraFieldRow($field_id, $extra_field);
  }

  $form['fields'] = $table;

  // Custom display settings.
  if ($this->entity->getMode() == 'default') {
    // Only show the settings if there is at least one custom display mode.
    $display_mode_options = $this->getDisplayModeOptions();
    // Unset default option.
    unset($display_mode_options['default']);
    if ($display_mode_options) {
      $form['modes'] = array(
        '#type' => 'details',
        '#title' => $this->t('Custom display settings'),
      );
      // Prepare default values for the 'Custom display settings' checkboxes.
      $default = array();
      if ($enabled_displays = array_filter($this->getDisplayStatuses())) {
        $default = array_keys(array_intersect_key($display_mode_options, $enabled_displays));
      }
      $form['modes']['display_modes_custom'] = array(
        '#type' => 'checkboxes',
        '#title' => $this->t('Use custom display settings for the following @display_context modes', ['@display_context' => $this->displayContext]),
        '#options' => $display_mode_options,
        '#default_value' => $default,
      );
      // Provide link to manage display modes.
      $form['modes']['display_modes_link'] = $this->getDisplayModesLink();
    }
  }

  // In overviews involving nested rows from contributed modules (i.e
  // field_group), the 'plugin type' selects can trigger a series of changes
  // in child rows. The #ajax behavior is therefore not attached directly to
  // the selects, but triggered by the client-side script through a hidden
  // #ajax 'Refresh' button. A hidden 'refresh_rows' input tracks the name of
  // affected rows.
  $form['refresh_rows'] = array('#type' => 'hidden');
  $form['refresh'] = array(
    '#type' => 'submit',
    '#value' => $this->t('Refresh'),
    '#op' => 'refresh_table',
    '#submit' => array('::multistepSubmit'),
    '#ajax' => array(
      'callback' => '::multistepAjax',
      'wrapper' => 'field-display-overview-wrapper',
      'effect' => 'fade',
      // The button stays hidden, so we hide the Ajax spinner too. Ad-hoc
      // spinners will be added manually by the client-side script.
      'progress' => 'none',
    ),
    '#attributes' => array('class' => array('visually-hidden'))
  );

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#button_type' => 'primary',
    '#value' => $this->t('Save'),
  );

  $form['#attached']['library'][] = 'field_ui/drupal.field_ui';

  return $form;
}
doc_Drupal
2016-10-29 09:05:23
Comments
Leave a Comment

Please login to continue.