EntityDisplayFormBase.php 32.5 KB
Newer Older
1 2
<?php

3
namespace Drupal\field_ui\Form;
4

5
use Drupal\Component\Plugin\Factory\DefaultFactory;
6
use Drupal\Component\Plugin\PluginManagerBase;
7 8 9
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
10
use Drupal\Core\Field\FieldDefinitionInterface;
11
use Drupal\Core\Field\FieldTypePluginManagerInterface;
12
use Drupal\Core\Field\PluginSettingsInterface;
13
use Drupal\Core\Form\FormStateInterface;
14
use Drupal\Core\Routing\RouteMatchInterface;
15
use Drupal\field_ui\Element\FieldUiTable;
16
use Drupal\field_ui\FieldUI;
17 18

/**
19
 * Base class for EntityDisplay edit forms.
20
 */
21
abstract class EntityDisplayFormBase extends EntityForm {
22

23 24 25 26 27 28 29
  /**
   * The display context. Either 'view' or 'form'.
   *
   * @var string
   */
  protected $displayContext;

30 31 32 33 34 35 36 37 38 39 40 41 42 43
  /**
   * The widget or formatter plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerBase
   */
  protected $pluginManager;

  /**
   * A list of field types.
   *
   * @var array
   */
  protected $fieldTypes;

44
  /**
45
   * The entity being used by this form.
46
   *
47
   * @var \Drupal\Core\Entity\Display\EntityDisplayInterface
48
   */
49
  protected $entity;
50

51
  /**
52
   * Constructs a new EntityDisplayFormBase.
53
   *
54
   * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
55
   *   The field type manager.
56 57 58
   * @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager
   *   The widget or formatter plugin manager.
   */
59
  public function __construct(FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager) {
60
    $this->fieldTypes = $field_type_manager->getDefinitions();
61
    $this->pluginManager = $plugin_manager;
62 63 64 65 66
  }

  /**
   * {@inheritdoc}
   */
67 68 69
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
    $route_parameters = $route_match->getParameters()->all();

70
    return $this->getEntityDisplay($route_parameters['entity_type_id'], $route_parameters['bundle'], $route_parameters[$this->displayContext . '_mode_name']);
71 72 73
  }

  /**
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
   * Get the regions needed to create the overview form.
   *
   * @return array
   *   Example usage:
   *   @code
   *     return array(
   *       'content' => array(
   *         // label for the region.
   *         'title' => $this->t('Content'),
   *         // Indicates if the region is visible in the UI.
   *         'invisible' => TRUE,
   *         // A message to indicate that there is nothing to be displayed in
   *         // the region.
   *         'message' => $this->t('No field is displayed.'),
   *       ),
   *     );
   *   @endcode
91 92
   */
  public function getRegions() {
93 94
    return [
      'content' => [
95
        'title' => $this->t('Content'),
96
        'invisible' => TRUE,
97
        'message' => $this->t('No field is displayed.')
98 99 100
      ],
      'hidden' => [
        'title' => $this->t('Disabled', [], ['context' => 'Plural']),
101
        'message' => $this->t('No field is hidden.')
102 103
      ],
    ];
104 105
  }

106 107 108 109 110 111 112
  /**
   * Returns an associative array of all regions.
   *
   * @return array
   *   An array containing the region options.
   */
  public function getRegionOptions() {
113
    $options = [];
114 115 116 117 118 119
    foreach ($this->getRegions() as $region => $data) {
      $options[$region] = $data['title'];
    }
    return $options;
  }

120 121 122 123 124 125 126 127
  /**
   * Collects the definitions of fields whose display is configurable.
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface[]
   *   The array of field definitions
   */
  protected function getFieldDefinitions() {
    $context = $this->displayContext;
128
    return array_filter($this->entityManager->getFieldDefinitions($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle()), function(FieldDefinitionInterface $field_definition) use ($context) {
129 130 131 132
      return $field_definition->isDisplayConfigurable($context);
    });
  }

133 134 135
  /**
   * {@inheritdoc}
   */
136 137
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
138

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

142
    $form += [
143 144
      '#entity_type' => $this->entity->getTargetEntityTypeId(),
      '#bundle' => $this->entity->getTargetBundle(),
145
      '#fields' => array_keys($field_definitions),
146
      '#extra' => array_keys($extra_fields),
147
    ];
148

149
    if (empty($field_definitions) && empty($extra_fields) && $route_info = FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle())) {
150
      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.', [':link' => $route_info->toString()]), 'warning');
151 152 153
      return $form;
    }

154
    $table = [
155 156 157
      '#type' => 'field_ui_table',
      '#header' => $this->getTableHeader(),
      '#regions' => $this->getRegions(),
158 159
      '#attributes' => [
        'class' => ['field-ui-overview'],
160
        'id' => 'field-display-overview',
161 162 163
      ],
      '#tabledrag' => [
        [
164 165 166
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'field-weight',
167 168
        ],
        [
169 170 171 172 173
          'action' => 'match',
          'relationship' => 'parent',
          'group' => 'field-parent',
          'subgroup' => 'field-parent',
          'source' => 'field-name',
174 175
        ],
        [
176 177 178 179 180
          'action' => 'match',
          'relationship' => 'parent',
          'group' => 'field-region',
          'subgroup' => 'field-region',
          'source' => 'field-name',
181 182 183
        ],
      ],
    ];
184 185

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

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

    $form['fields'] = $table;

    // Custom display settings.
198
    if ($this->entity->getMode() == 'default') {
199
      // Only show the settings if there is at least one custom display mode.
200 201 202 203
      $display_mode_options = $this->getDisplayModeOptions();
      // Unset default option.
      unset($display_mode_options['default']);
      if ($display_mode_options) {
204
        $form['modes'] = [
205
          '#type' => 'details',
206
          '#title' => $this->t('Custom display settings'),
207
        ];
208
        // Prepare default values for the 'Custom display settings' checkboxes.
209
        $default = [];
210 211
        if ($enabled_displays = array_filter($this->getDisplayStatuses())) {
          $default = array_keys(array_intersect_key($display_mode_options, $enabled_displays));
212
        }
213
        $form['modes']['display_modes_custom'] = [
214
          '#type' => 'checkboxes',
215
          '#title' => $this->t('Use custom display settings for the following @display_context modes', ['@display_context' => $this->displayContext]),
216
          '#options' => $display_mode_options,
217
          '#default_value' => $default,
218
        ];
219 220
        // Provide link to manage display modes.
        $form['modes']['display_modes_link'] = $this->getDisplayModesLink();
221 222 223 224 225 226 227 228 229
      }
    }

    // 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.
230 231
    $form['refresh_rows'] = ['#type' => 'hidden'];
    $form['refresh'] = [
232
      '#type' => 'submit',
233
      '#value' => $this->t('Refresh'),
234
      '#op' => 'refresh_table',
235 236
      '#submit' => ['::multistepSubmit'],
      '#ajax' => [
237
        'callback' => '::multistepAjax',
238 239 240 241 242
        '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',
243 244 245
      ],
      '#attributes' => ['class' => ['visually-hidden']]
    ];
246

247 248
    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
249 250 251
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this->t('Save'),
252
    ];
253

254
    $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
255 256 257 258 259 260 261

    return $form;
  }

  /**
   * Builds the table row structure for a single field.
   *
262 263
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The field definition.
264 265
   * @param array $form
   *   An associative array containing the structure of the form.
266 267
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
268 269 270 271
   *
   * @return array
   *   A table row array.
   */
272
  protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
273
    $field_name = $field_definition->getName();
274
    $display_options = $this->entity->getComponent($field_name);
275
    $label = $field_definition->getLabel();
276

277 278 279 280 281 282
    // 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);
    }

283
    $regions = array_keys($this->getRegions());
284 285
    $field_row = [
      '#attributes' => ['class' => ['draggable', 'tabledrag-leaf']],
286
      '#row_type' => 'field',
287 288
      '#region_callback' => [$this, 'getRowRegion'],
      '#js_settings' => [
289
        'rowHandler' => 'field',
290
        'defaultPlugin' => $this->getDefaultPlugin($field_definition->getType()),
291 292
      ],
      'human_name' => [
293
        '#plain_text' => $label,
294 295
      ],
      'weight' => [
296
        '#type' => 'textfield',
297
        '#title' => $this->t('Weight for @title', ['@title' => $label]),
298 299 300
        '#title_display' => 'invisible',
        '#default_value' => $display_options ? $display_options['weight'] : '0',
        '#size' => 3,
301 302 303 304
        '#attributes' => ['class' => ['field-weight']],
      ],
      'parent_wrapper' => [
        'parent' => [
305
          '#type' => 'select',
306
          '#title' => $this->t('Label display for @title', ['@title' => $label]),
307
          '#title_display' => 'invisible',
308
          '#options' => array_combine($regions, $regions),
309
          '#empty_value' => '',
310 311 312 313
          '#attributes' => ['class' => ['js-field-parent', 'field-parent']],
          '#parents' => ['fields', $field_name, 'parent'],
        ],
        'hidden_name' => [
314
          '#type' => 'hidden',
315
          '#default_value' => $field_name,
316 317 318 319
          '#attributes' => ['class' => ['field-name']],
        ],
      ],
      'region' => [
320
        '#type' => 'select',
321
        '#title' => $this->t('Region for @title', ['@title' => $label]),
322 323 324
        '#title_display' => 'invisible',
        '#options' => $this->getRegionOptions(),
        '#default_value' => $display_options ? $display_options['region'] : 'hidden',
325 326 327
        '#attributes' => ['class' => ['field-region']],
      ],
    ];
328

329 330
    $field_row['plugin'] = [
      'type' => [
331
        '#type' => 'select',
332
        '#title' => $this->t('Plugin for @title', ['@title' => $label]),
333
        '#title_display' => 'invisible',
334
        '#options' => $this->getApplicablePluginOptions($field_definition),
335
        '#default_value' => $display_options ? $display_options['type'] : 'hidden',
336 337 338 339 340
        '#parents' => ['fields', $field_name, 'type'],
        '#attributes' => ['class' => ['field-plugin-type']],
      ],
      'settings_edit_form' => [],
    ];
341 342

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

    // Base button element for the various plugin settings actions.
346 347 348
    $base_button = [
      '#submit' => ['::multistepSubmit'],
      '#ajax' => [
349
        'callback' => '::multistepAjax',
350 351
        'wrapper' => 'field-display-overview-wrapper',
        'effect' => 'fade',
352
      ],
353
      '#field_name' => $field_name,
354
    ];
355

356
    if ($form_state->get('plugin_settings_edit') == $field_name) {
357 358
      // We are currently editing this field's plugin settings. Display the
      // settings form and submit buttons.
359
      $field_row['plugin']['settings_edit_form'] = [];
360 361 362 363

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

366
        if ($settings_form || $third_party_settings_form) {
367 368
          $field_row['plugin']['#cell_attributes'] = ['colspan' => 3];
          $field_row['plugin']['settings_edit_form'] = [
369
            '#type' => 'container',
370 371 372
            '#attributes' => ['class' => ['field-plugin-settings-edit-form']],
            '#parents' => ['fields', $field_name, 'settings_edit_form'],
            'label' => [
373
              '#markup' => $this->t('Plugin settings'),
374
            ],
375
            'settings' => $settings_form,
376
            'third_party_settings' => $third_party_settings_form,
377
            'actions' => [
378
              '#type' => 'actions',
379
              'save_settings' => $base_button + [
380
                '#type' => 'submit',
381
                '#button_type' => 'primary',
382
                '#name' => $field_name . '_plugin_settings_update',
383
                '#value' => $this->t('Update'),
384
                '#op' => 'update',
385 386
              ],
              'cancel_settings' => $base_button + [
387
                '#type' => 'submit',
388
                '#name' => $field_name . '_plugin_settings_cancel',
389
                '#value' => $this->t('Cancel'),
390 391 392
                '#op' => 'cancel',
                // Do not check errors for the 'Cancel' button, but make sure we
                // get the value of the 'plugin type' select.
393 394 395 396
                '#limit_validation_errors' => [['fields', $field_name, 'type']],
              ],
            ],
          ];
397 398 399 400 401
          $field_row['#attributes']['class'][] = 'field-plugin-settings-editing';
        }
      }
    }
    else {
402 403
      $field_row['settings_summary'] = [];
      $field_row['settings_edit'] = [];
404 405 406 407 408 409 410

      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.
411
        $this->alterSettingsSummary($summary, $plugin, $field_definition);
412 413

        if (!empty($summary)) {
414
          $field_row['settings_summary'] = [
415 416
            '#type' => 'inline_template',
            '#template' => '<div class="field-plugin-summary">{{ summary|safe_join("<br />") }}</div>',
417 418 419
            '#context' => ['summary' => $summary],
            '#cell_attributes' => ['class' => ['field-plugin-summary-cell']],
          ];
420
        }
421 422

        // Check selected plugin settings to display edit link or not.
423 424 425
        $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)) {
426
          $field_row['settings_edit'] = $base_button + [
427
            '#type' => 'image_button',
428
            '#name' => $field_name . '_settings_edit',
429
            '#src' => 'core/misc/icons/787878/cog.svg',
430
            '#attributes' => ['class' => ['field-plugin-settings-edit'], 'alt' => $this->t('Edit')],
431 432 433
            '#op' => 'edit',
            // Do not check errors for the 'Edit' button, but make sure we get
            // the value of the 'plugin type' select.
434
            '#limit_validation_errors' => [['fields', $field_name, 'type']],
435 436
            '#prefix' => '<div class="field-plugin-settings-edit-wrapper">',
            '#suffix' => '</div>',
437
          ];
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
        }
      }
    }

    return $field_row;
  }

  /**
   * Builds the table row structure for a single extra field.
   *
   * @param string $field_id
   *   The field ID.
   * @param array $extra_field
   *   The pseudo-field element.
   *
   * @return array
   *   A table row array.
   */
456 457
  protected function buildExtraFieldRow($field_id, $extra_field) {
    $display_options = $this->entity->getComponent($field_id);
458

459
    $regions = array_keys($this->getRegions());
460 461
    $extra_field_row = [
      '#attributes' => ['class' => ['draggable', 'tabledrag-leaf']],
462
      '#row_type' => 'extra_field',
463 464 465
      '#region_callback' => [$this, 'getRowRegion'],
      '#js_settings' => ['rowHandler' => 'field'],
      'human_name' => [
466
        '#markup' => $extra_field['label'],
467 468
      ],
      'weight' => [
469
        '#type' => 'textfield',
470
        '#title' => $this->t('Weight for @title', ['@title' => $extra_field['label']]),
471 472 473
        '#title_display' => 'invisible',
        '#default_value' => $display_options ? $display_options['weight'] : 0,
        '#size' => 3,
474 475 476 477
        '#attributes' => ['class' => ['field-weight']],
      ],
      'parent_wrapper' => [
        'parent' => [
478
          '#type' => 'select',
479
          '#title' => $this->t('Parents for @title', ['@title' => $extra_field['label']]),
480
          '#title_display' => 'invisible',
481
          '#options' => array_combine($regions, $regions),
482
          '#empty_value' => '',
483 484 485 486
          '#attributes' => ['class' => ['js-field-parent', 'field-parent']],
          '#parents' => ['fields', $field_id, 'parent'],
        ],
        'hidden_name' => [
487 488
          '#type' => 'hidden',
          '#default_value' => $field_id,
489 490 491 492
          '#attributes' => ['class' => ['field-name']],
        ],
      ],
      'region' => [
493
        '#type' => 'select',
494
        '#title' => $this->t('Region for @title', ['@title' => $extra_field['label']]),
495 496 497
        '#title_display' => 'invisible',
        '#options' => $this->getRegionOptions(),
        '#default_value' => $display_options ? $display_options['region'] : 'hidden',
498 499
        '#attributes' => ['class' => ['field-region']],
      ],
500 501 502 503 504 505 506 507
      'plugin' => [
        'type' => [
          '#type' => 'hidden',
          '#value' => $display_options ? 'visible' : 'hidden',
          '#parents' => ['fields', $field_id, 'type'],
          '#attributes' => ['class' => ['field-plugin-type']],
        ],
      ],
508 509 510
      'settings_summary' => [],
      'settings_edit' => [],
    ];
511 512 513 514 515 516 517

    return $extra_field_row;
  }

  /**
   * {@inheritdoc}
   */
518
  public function submitForm(array &$form, FormStateInterface $form_state) {
519 520 521 522 523 524 525
    // If the main "Save" button was submitted while a field settings subform
    // was being edited, update the new incoming settings when rebuilding the
    // entity, just as if the subform's "Update" button had been submitted.
    if ($edit_field = $form_state->get('plugin_settings_edit')) {
      $form_state->set('plugin_settings_update', $edit_field);
    }

526
    parent::submitForm($form, $form_state);
527
    $form_values = $form_state->getValues();
528 529

    // Handle the 'display modes' checkboxes if present.
530
    if ($this->entity->getMode() == 'default' && !empty($form_values['display_modes_custom'])) {
531 532 533
      $display_modes = $this->getDisplayModes();
      $current_statuses = $this->getDisplayStatuses();

534
      $statuses = [];
535 536 537 538 539
      foreach ($form_values['display_modes_custom'] as $mode => $value) {
        if (!empty($value) && empty($current_statuses[$mode])) {
          // If no display exists for the newly enabled view mode, initialize
          // it with those from the 'default' view mode, which were used so
          // far.
540 541
          if (!$this->entityManager->getStorage($this->entity->getEntityTypeId())->load($this->entity->getTargetEntityTypeId() . '.' . $this->entity->getTargetBundle() . '.' . $mode)) {
            $display = $this->getEntityDisplay($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle(), 'default')->createCopy($mode);
542 543 544 545 546
            $display->save();
          }

          $display_mode_label = $display_modes[$mode]['label'];
          $url = $this->getOverviewUrl($mode);
547
          drupal_set_message($this->t('The %display_mode mode now uses custom display settings. You might want to <a href=":url">configure them</a>.', ['%display_mode' => $display_mode_label, ':url' => $url->toString()]));
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
        }
        $statuses[$mode] = !empty($value);
      }

      $this->saveDisplayStatuses($statuses);
    }

    drupal_set_message($this->t('Your settings have been saved.'));
  }

  /**
   * {@inheritdoc}
   */
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    $form_values = $form_state->getValues();

    if ($this->entity instanceof EntityWithPluginCollectionInterface) {
      // Do not manually update values represented by plugin collections.
      $form_values = array_diff_key($form_values, $this->entity->getPluginCollections());
    }
568 569 570

    // Collect data for 'regular' fields.
    foreach ($form['#fields'] as $field_name) {
571
      $values = $form_values['fields'][$field_name];
572

573
      if ($values['region'] == 'hidden') {
574
        $entity->removeComponent($field_name);
575
      }
576 577 578 579 580 581 582 583 584 585 586
      else {
        $options = $entity->getComponent($field_name);

        // Update field settings only if the submit handler told us to.
        if ($form_state->get('plugin_settings_update') === $field_name) {
          // Only store settings actually used by the selected plugin.
          $default_settings = $this->pluginManager->getDefaultSettings($options['type']);
          $options['settings'] = isset($values['settings_edit_form']['settings']) ? array_intersect_key($values['settings_edit_form']['settings'], $default_settings) : [];
          $options['third_party_settings'] = isset($values['settings_edit_form']['third_party_settings']) ? $values['settings_edit_form']['third_party_settings'] : [];
          $form_state->set('plugin_settings_update', NULL);
        }
587

588
        $options['type'] = $values['type'];
589
        $options['weight'] = $values['weight'];
590
        $options['region'] = $values['region'];
591 592 593 594 595
        // Only formatters have configurable label visibility.
        if (isset($values['label'])) {
          $options['label'] = $values['label'];
        }
        $entity->setComponent($field_name, $options);
596 597 598
      }
    }

599 600
    // Collect data for 'extra' fields.
    foreach ($form['#extra'] as $name) {
601
      if ($form_values['fields'][$name]['region'] == 'hidden') {
602
        $entity->removeComponent($name);
603 604
      }
      else {
605
        $entity->setComponent($name, [
606
          'weight' => $form_values['fields'][$name]['weight'],
607
          'region' => $form_values['fields'][$name]['region'],
608
        ]);
609 610 611 612 613 614 615
      }
    }
  }

  /**
   * Form submission handler for multistep buttons.
   */
616
  public function multistepSubmit($form, FormStateInterface $form_state) {
617
    $trigger = $form_state->getTriggeringElement();
618 619 620 621 622 623
    $op = $trigger['#op'];

    switch ($op) {
      case 'edit':
        // Store the field whose settings are currently being edited.
        $field_name = $trigger['#field_name'];
624
        $form_state->set('plugin_settings_edit', $field_name);
625 626 627
        break;

      case 'update':
628 629
        // Set the field back to 'non edit' mode, and update $this->entity with
        // the new settings fro the next rebuild.
630
        $field_name = $trigger['#field_name'];
631
        $form_state->set('plugin_settings_edit', NULL);
632 633
        $form_state->set('plugin_settings_update', $field_name);
        $this->entity = $this->buildEntity($form, $form_state);
634 635 636 637
        break;

      case 'cancel':
        // Set the field back to 'non edit' mode.
638
        $form_state->set('plugin_settings_edit', NULL);
639 640 641 642 643
        break;

      case 'refresh_table':
        // If the currently edited field is one of the rows to be refreshed, set
        // it back to 'non edit' mode.
644
        $updated_rows = explode(' ', $form_state->getValue('refresh_rows'));
645 646 647
        $plugin_settings_edit = $form_state->get('plugin_settings_edit');
        if ($plugin_settings_edit && in_array($plugin_settings_edit, $updated_rows)) {
          $form_state->set('plugin_settings_edit', NULL);
648 649 650 651
        }
        break;
    }

652
    $form_state->setRebuild();
653 654 655 656 657
  }

  /**
   * Ajax handler for multistep buttons.
   */
658
  public function multistepAjax($form, FormStateInterface $form_state) {
659
    $trigger = $form_state->getTriggeringElement();
660 661 662 663 664
    $op = $trigger['#op'];

    // Pick the elements that need to receive the ajax-new-content effect.
    switch ($op) {
      case 'edit':
665 666
        $updated_rows = [$trigger['#field_name']];
        $updated_columns = ['plugin'];
667 668 669 670
        break;

      case 'update':
      case 'cancel':
671 672
        $updated_rows = [$trigger['#field_name']];
        $updated_columns = ['plugin', 'settings_summary', 'settings_edit'];
673 674 675
        break;

      case 'refresh_table':
676
        $updated_rows = array_values(explode(' ', $form_state->getValue('refresh_rows')));
677
        $updated_columns = ['settings_summary', 'settings_edit'];
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
        break;
    }

    foreach ($updated_rows as $name) {
      foreach ($updated_columns as $key) {
        $element = &$form['fields'][$name][$key];
        $element['#prefix'] = '<div class="ajax-new-content">' . (isset($element['#prefix']) ? $element['#prefix'] : '');
        $element['#suffix'] = (isset($element['#suffix']) ? $element['#suffix'] : '') . '</div>';
      }
    }

    // Return the whole table.
    return $form['fields'];
  }

693 694 695 696 697 698 699 700 701 702
  /**
   * Performs pre-render tasks on field_ui_table elements.
   *
   * @param array $elements
   *   A structured array containing two sub-levels of elements. Properties
   *   used:
   *   - #tabledrag: The value is a list of $options arrays that are passed to
   *     drupal_attach_tabledrag(). The HTML ID of the table is added to each
   *     $options array.
   *
703 704
   * @return array
   *
705 706
   * @see drupal_render()
   * @see \Drupal\Core\Render\Element\Table::preRenderTable()
707 708
   *
   * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
709 710
   */
  public function tablePreRender($elements) {
711
    return FieldUiTable::tablePreRender($elements);
712 713 714 715 716 717
  }

  /**
   * Determines the rendering order of an array representing a tree.
   *
   * Callback for array_reduce() within
718
   * \Drupal\field_ui\Form\EntityDisplayFormBase::tablePreRender().
719 720
   *
   * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
721 722
   */
  public function reduceOrder($array, $a) {
723
    return FieldUiTable::reduceOrder($array, $a);
724 725
  }

726 727 728 729
  /**
   * Returns the extra fields of the entity type and bundle used by this form.
   *
   * @return array
730 731 732
   *   An array of extra field info.
   *
   * @see \Drupal\Core\Entity\EntityManagerInterface::getExtraFields()
733
   */
734
  protected function getExtraFields() {
735
    $context = $this->displayContext == 'view' ? 'display' : $this->displayContext;
736
    $extra_fields = $this->entityManager->getExtraFields($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle());
737
    return isset($extra_fields[$context]) ? $extra_fields[$context] : [];
738
  }
739

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
  /**
   * Returns an entity display object to be used by this form.
   *
   * @param string $entity_type_id
   *   The target entity type ID of the entity display.
   * @param string $bundle
   *   The target bundle of the entity display.
   * @param string $mode
   *   A view or form mode.
   *
   * @return \Drupal\Core\Entity\Display\EntityDisplayInterface
   *   An entity display.
   */
  abstract protected function getEntityDisplay($entity_type_id, $bundle, $mode);

755
  /**
756
   * Returns an array of applicable widget or formatter options for a field.
757
   *
758 759
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The field definition.
760 761
   *
   * @return array
762
   *   An array of applicable widget or formatter options.
763
   */
764
  protected function getApplicablePluginOptions(FieldDefinitionInterface $field_definition) {
765
    $options = $this->pluginManager->getOptions($field_definition->getType());
766
    $applicable_options = [];
767 768 769 770 771 772
    foreach ($options as $option => $label) {
      $plugin_class = DefaultFactory::getPluginClass($option, $this->pluginManager->getDefinition($option));
      if ($plugin_class::isApplicable($field_definition)) {
        $applicable_options[$option] = $label;
      }
    }
773 774 775
    return $applicable_options;
  }

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  /**
   * Returns the ID of the default widget or formatter plugin for a field type.
   *
   * @param string $field_type
   *   The field type.
   *
   * @return string
   *   The widget or formatter plugin ID.
   */
  abstract protected function getDefaultPlugin($field_type);

  /**
   * Returns the form or view modes used by this form.
   *
   * @return array
   *   An array of form or view mode info.
   */
  abstract protected function getDisplayModes();

795 796 797 798 799 800 801 802
  /**
   * Returns an array of form or view mode options.
   *
   * @return array
   *   An array of form or view mode options.
   */
  abstract protected function getDisplayModeOptions();

803 804 805 806 807 808 809 810
  /**
   * Returns a link to the form or view mode admin page.
   *
   * @return array
   *   An array of a form element to be rendered as a link.
   */
  abstract protected function getDisplayModesLink();

811 812 813 814 815 816 817 818 819
  /**
   * Returns the region to which a row in the display overview belongs.
   *
   * @param array $row
   *   The row element.
   *
   * @return string|null
   *   The region name this row belongs to.
   */
820 821 822 823
  public function getRowRegion(&$row) {
    $regions = $this->getRegions();
    if (!isset($regions[$row['region']['#value']])) {
      $row['region']['#value'] = 'hidden';
824
    }
825
    return $row['region']['#value'];
826 827
  }

828 829 830
  /**
   * Returns entity (form) displays for the current entity display type.
   *
831
   * @return \Drupal\Core\Entity\Display\EntityDisplayInterface[]
832 833 834
   *   An array holding entity displays or entity form displays.
   */
  protected function getDisplays() {
835
    $load_ids = [];
836
    $display_entity_type = $this->entity->getEntityTypeId();
837 838
    $entity_type = $this->entityManager->getDefinition($display_entity_type);
    $config_prefix = $entity_type->getConfigPrefix();
839
    $ids = $this->configFactory()->listAll($config_prefix . '.' . $this->entity->getTargetEntityTypeId() . '.' . $this->entity->getTargetBundle() . '.');
840 841 842 843 844 845 846
    foreach ($ids as $id) {
      $config_id = str_replace($config_prefix . '.', '', $id);
      list(,, $display_mode) = explode('.', $config_id);
      if ($display_mode != 'default') {
        $load_ids[] = $config_id;
      }
    }
847
    return $this->entityManager->getStorage($display_entity_type)->loadMultiple($load_ids);
848 849 850 851 852 853 854 855 856
  }

  /**
   * Returns form or view modes statuses for the bundle used by this form.
   *
   * @return array
   *   An array of form or view mode statuses.
   */
  protected function getDisplayStatuses() {
857
    $display_statuses = [];
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
    $displays = $this->getDisplays();
    foreach ($displays as $display) {
      $display_statuses[$display->get('mode')] = $display->status();
    }
    return $display_statuses;
  }

  /**
   * Saves the updated display mode statuses.
   *
   * @param array $display_statuses
   *   An array holding updated form or view mode statuses.
   */
  protected function saveDisplayStatuses($display_statuses) {
    $displays = $this->getDisplays();
    foreach ($displays as $display) {
874 875 876 877 878 879
      // Only update the display if the status is changing.
      $new_status = $display_statuses[$display->get('mode')];
      if ($new_status !== $display->status()) {
        $display->set('status', $new_status);
        $display->save();
      }
880 881 882
    }
  }

883 884 885 886 887 888 889 890 891
  /**
   * Returns an array containing the table headers.
   *
   * @return array
   *   The table header.
   */
  abstract protected function getTableHeader();

  /**
892
   * Returns the Url object for a specific entity (form) display edit form.
893 894 895 896
   *
   * @param string $mode
   *   The form or view mode.
   *
897 898
   * @return \Drupal\Core\Url
   *   A Url object for the overview route.
899
   */
900
  abstract protected function getOverviewUrl($mode);
901 902

  /**
903
   * Adds the widget or formatter third party settings forms.
904
   *
905
   * @param \Drupal\Core\Field\PluginSettingsInterface $plugin
906
   *   The widget or formatter.
907 908
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The field definition.
909
   * @param array $form
910
   *   The (entire) configuration form array.
911
   * @param \Drupal\Core\Form\FormStateInterface $form_state
912
   *   The form state.
913 914 915
   *
   * @return array
   *   The widget or formatter third party settings form.
916
   */
917
  abstract protected function thirdPartySettingsForm(PluginSettingsInterface $plugin, FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state);
918 919 920 921 922 923

  /**
   * Alters the widget or formatter settings summary.
   *
   * @param array $summary
   *   The widget or formatter settings summary.
924
   * @param \Drupal\Core\Field\PluginSettingsInterface $plugin
925
   *   The widget or formatter.
926 927
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The field definition.
928
   */
929
  abstract protected function alterSettingsSummary(array &$summary, PluginSettingsInterface $plugin, FieldDefinitionInterface $field_definition);
930 931

}