You are here

protected function FieldsSettingsForm::buildFieldRow in Diff 8

Builds a row for the table. Each row corresponds to a field type.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type.

\Drupal\Core\Field\FieldStorageDefinitionInterface $field_definition: Definition the field type.

FormStateInterface $form_state: THe form state object.

Return value

array A table row for the field type listing table.

1 call to FieldsSettingsForm::buildFieldRow()
FieldsSettingsForm::buildForm in src/Form/FieldsSettingsForm.php
Form constructor.

File

src/Form/FieldsSettingsForm.php, line 171

Class

FieldsSettingsForm
Configure fields with their diff builder plugin settings.

Namespace

Drupal\diff\Form

Code

protected function buildFieldRow(EntityTypeInterface $entity_type, FieldStorageDefinitionInterface $field_definition, FormStateInterface $form_state) {
  $entity_type_label = $entity_type
    ->getLabel();
  $field_name = $field_definition
    ->getName();
  $field_type = $field_definition
    ->getType();
  $field_key = $entity_type
    ->id() . '__' . $field_name;
  $display_options = $this->diffBuilderManager
    ->getSelectedPluginForFieldStorageDefinition($field_definition);
  $plugin_options = $this->diffBuilderManager
    ->getApplicablePluginOptions($field_definition);

  // Base button element for the various plugin settings actions.
  $base_button = [
    '#submit' => [
      [
        $this,
        'multiStepSubmit',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'multiStepAjax',
      ],
      'wrapper' => 'field-display-overview-wrapper',
      'effect' => 'fade',
    ],
    '#field_key' => $field_key,
  ];
  $field_row['entity_type'] = [
    '#markup' => $entity_type_label,
  ];
  $labels = _diff_field_label($entity_type
    ->id(), $field_name);
  $field_row['field_label'] = [
    '#markup' => array_shift($labels),
  ];
  $field_type_label = $this->fieldTypePluginManager
    ->getDefinitions()[$field_type]['label'];
  $field_row['field_type'] = [
    '#markup' => $field_type_label,
  ];

  // Check the currently selected plugin, and merge persisted values for its
  // settings.
  if ($type = $form_state
    ->getValue([
    'fields',
    $field_key,
    'plugin',
    'type',
  ])) {
    $display_options['type'] = $type;
  }
  $plugin_settings = $form_state
    ->get('plugin_settings');
  if (isset($plugin_settings[$field_key]['settings'])) {
    $modified = FALSE;
    if (!empty($display_options['settings'])) {
      foreach ($display_options['settings'] as $key => $value) {
        if ($plugin_settings[$field_key]['settings'][$key] != $value) {
          $modified = TRUE;
          break;
        }
      }
    }

    // In case settings are not identical to the ones in the config display
    // a warning message. Don't display it twice.
    if ($modified && empty($_SESSION['messages']['warning'])) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('You have unsaved changes.'));
    }
    $display_options['settings'] = $plugin_settings[$field_key]['settings'];
  }
  $field_row['plugin'] = array(
    'type' => array(
      '#type' => 'select',
      '#options' => $plugin_options,
      '#empty_option' => $this
        ->t("- Don't compare -"),
      '#empty_value' => 'hidden',
      '#title_display' => 'invisible',
      '#attributes' => array(
        'class' => array(
          'field-plugin-type',
        ),
      ),
      '#default_value' => $display_options,
      '#ajax' => array(
        'callback' => [
          $this,
          'multiStepAjax',
        ],
        'method' => 'replace',
        'wrapper' => 'field-display-overview-wrapper',
        'effect' => 'fade',
      ),
      '#field_key' => $field_key,
    ),
    'settings_edit_form' => array(),
  );

  // Get a configured instance of the plugin.
  $plugin = $this
    ->getPlugin($display_options);

  // We are currently editing this field's plugin settings. Display the
  // settings form and submit buttons.
  if ($form_state
    ->get('plugin_settings_edit') == $field_key) {
    $field_row['plugin']['settings_edit_form'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'field-plugin-settings-edit-form',
        ),
      ),
      '#parents' => [
        'fields',
        $field_key,
        'settings_edit_form',
      ],
      'label' => array(
        '#markup' => $this
          ->t('Plugin settings:' . ' <span class="plugin-name">' . $plugin_options[$display_options['type']] . '</span>'),
      ),
      'settings' => $plugin
        ->buildConfigurationForm(array(), $form_state),
      'actions' => array(
        '#type' => 'actions',
        'save_settings' => $base_button + [
          '#type' => 'submit',
          '#button_type' => 'primary',
          '#name' => $field_key . '_plugin_settings_update',
          '#value' => $this
            ->t('Update'),
          '#op' => 'update',
        ],
        'cancel_settings' => $base_button + [
          '#type' => 'submit',
          '#name' => $field_key . '_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' => [
            [
              'fields',
              $field_key,
              'plugin',
              'type',
            ],
          ],
        ],
      ),
    );
    $field_row['settings_edit'] = array();
    $field_row['#attributes']['class'][] = 'field-plugin-settings-editing';
  }
  else {
    $field_row['settings_edit'] = [];

    // Display the configure settings button only if a plugin is selected.
    if ($plugin) {
      $field_row['settings_edit'] = $base_button + array(
        '#type' => 'image_button',
        '#name' => $field_key . '_settings_edit',
        '#src' => 'core/misc/icons/787878/cog.svg',
        '#attributes' => [
          'class' => [
            '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' => [
          [
            'fields',
            $field_key,
            'plugin',
            'type',
          ],
        ],
        '#prefix' => '<div class="field-plugin-settings-edit-wrapper">',
        '#suffix' => '</div>',
      );
    }
  }
  return $field_row;
}