EntityDisplayFormBase::buildFieldRow

protected EntityDisplayFormBase::buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state)

Builds the table row structure for a single field.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition.

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array A table row array.

File

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

Class

EntityDisplayFormBase
Base class for EntityDisplay edit forms.

Namespace

Drupal\field_ui\Form

Code

protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
  $field_name = $field_definition->getName();
  $display_options = $this->entity->getComponent($field_name);
  $label = $field_definition->getLabel();

  // Disable fields without any applicable plugins.
  if (empty($this->getApplicablePluginOptions($field_definition))) {
    $this->entity->removeComponent($field_name)->save();
    $display_options = $this->entity->getComponent($field_name);
  }

  $regions = array_keys($this->getRegions());
  $field_row = array(
    '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
    '#row_type' => 'field',
    '#region_callback' => array($this, 'getRowRegion'),
    '#js_settings' => array(
      'rowHandler' => 'field',
      'defaultPlugin' => $this->getDefaultPlugin($field_definition->getType()),
    ),
    'human_name' => array(
      '#plain_text' => $label,
    ),
    'weight' => array(
      '#type' => 'textfield',
      '#title' => $this->t('Weight for @title', array('@title' => $label)),
      '#title_display' => 'invisible',
      '#default_value' => $display_options ? $display_options['weight'] : '0',
      '#size' => 3,
      '#attributes' => array('class' => array('field-weight')),
    ),
    'parent_wrapper' => array(
      'parent' => array(
        '#type' => 'select',
        '#title' => $this->t('Label display for @title', array('@title' => $label)),
        '#title_display' => 'invisible',
        '#options' => array_combine($regions, $regions),
        '#empty_value' => '',
        '#attributes' => array('class' => array('js-field-parent', 'field-parent')),
        '#parents' => array('fields', $field_name, 'parent'),
      ),
      'hidden_name' => array(
        '#type' => 'hidden',
        '#default_value' => $field_name,
        '#attributes' => array('class' => array('field-name')),
      ),
    ),
  );

  $field_row['plugin'] = array(
    'type' => array(
      '#type' => 'select',
      '#title' => $this->t('Plugin for @title', array('@title' => $label)),
      '#title_display' => 'invisible',
      '#options' => $this->getPluginOptions($field_definition),
      '#default_value' => $display_options ? $display_options['type'] : 'hidden',
      '#parents' => array('fields', $field_name, 'type'),
      '#attributes' => array('class' => array('field-plugin-type')),
    ),
    'settings_edit_form' => array(),
  );

  // Get the corresponding plugin object.
  $plugin = $this->entity->getRenderer($field_name);

  // Base button element for the various plugin settings actions.
  $base_button = array(
    '#submit' => array('::multistepSubmit'),
    '#ajax' => array(
      'callback' => '::multistepAjax',
      'wrapper' => 'field-display-overview-wrapper',
      'effect' => 'fade',
    ),
    '#field_name' => $field_name,
  );

  if ($form_state->get('plugin_settings_edit') == $field_name) {
    // We are currently editing this field's plugin settings. Display the
    // settings form and submit buttons.
    $field_row['plugin']['settings_edit_form'] = array();

    if ($plugin) {
      // Generate the settings form and allow other modules to alter it.
      $settings_form = $plugin->settingsForm($form, $form_state);
      $third_party_settings_form = $this->thirdPartySettingsForm($plugin, $field_definition, $form, $form_state);

      if ($settings_form || $third_party_settings_form) {
        $field_row['plugin']['#cell_attributes'] = array('colspan' => 3);
        $field_row['plugin']['settings_edit_form'] = array(
          '#type' => 'container',
          '#attributes' => array('class' => array('field-plugin-settings-edit-form')),
          '#parents' => array('fields', $field_name, 'settings_edit_form'),
          'label' => array(
            '#markup' => $this->t('Plugin settings'),
          ),
          'settings' => $settings_form,
          'third_party_settings' => $third_party_settings_form,
          'actions' => array(
            '#type' => 'actions',
            'save_settings' => $base_button + array(
              '#type' => 'submit',
              '#button_type' => 'primary',
              '#name' => $field_name . '_plugin_settings_update',
              '#value' => $this->t('Update'),
              '#op' => 'update',
            ),
            'cancel_settings' => $base_button + array(
              '#type' => 'submit',
              '#name' => $field_name . '_plugin_settings_cancel',
              '#value' => $this->t('Cancel'),
              '#op' => 'cancel',
              // Do not check errors for the 'Cancel' button, but make sure we
              // get the value of the 'plugin type' select.
              '#limit_validation_errors' => array(array('fields', $field_name, 'type')),
            ),
          ),
        );
        $field_row['#attributes']['class'][] = 'field-plugin-settings-editing';
      }
    }
  }
  else {
    $field_row['settings_summary'] = array();
    $field_row['settings_edit'] = array();

    if ($plugin) {
      // Display a summary of the current plugin settings, and (if the
      // summary is not empty) a button to edit them.
      $summary = $plugin->settingsSummary();

      // Allow other modules to alter the summary.
      $this->alterSettingsSummary($summary, $plugin, $field_definition);

      if (!empty($summary)) {
        $field_row['settings_summary'] = array(
          '#type' => 'inline_template',
          '#template' => '<div class="field-plugin-summary">{{ summary|safe_join("<br />") }}</div>',
          '#context' => array('summary' => $summary),
          '#cell_attributes' => array('class' => array('field-plugin-summary-cell')),
        );
      }

      // Check selected plugin settings to display edit link or not.
      $settings_form = $plugin->settingsForm($form, $form_state);
      $third_party_settings_form = $this->thirdPartySettingsForm($plugin, $field_definition, $form, $form_state);
      if (!empty($settings_form) || !empty($third_party_settings_form)) {
        $field_row['settings_edit'] = $base_button + array(
          '#type' => 'image_button',
          '#name' => $field_name . '_settings_edit',
          '#src' => 'core/misc/icons/787878/cog.svg',
          '#attributes' => array('class' => array('field-plugin-settings-edit'), 'alt' => $this->t('Edit')),
          '#op' => 'edit',
          // Do not check errors for the 'Edit' button, but make sure we get
          // the value of the 'plugin type' select.
          '#limit_validation_errors' => array(array('fields', $field_name, 'type')),
          '#prefix' => '<div class="field-plugin-settings-edit-wrapper">',
          '#suffix' => '</div>',
        );
      }
    }
  }

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

Please login to continue.