DisplayOverviewBase.php 29.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php

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

namespace Drupal\field_ui;

use Drupal\Component\Plugin\PluginManagerBase;
11
use Drupal\Core\Config\ConfigFactoryInterface;
12
use Drupal\Core\Entity\Display\EntityDisplayInterface;
13
use Drupal\Core\Entity\EntityManagerInterface;
14
use Drupal\Core\Field\FieldDefinitionInterface;
15
use Drupal\Core\Field\FieldTypePluginManagerInterface;
16
17
18
19
20
21
22
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Field UI display overview base class.
 */
abstract class DisplayOverviewBase extends OverviewBase {

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
46
47
48
49
50
  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

51
52
53
  /**
   * Constructs a new DisplayOverviewBase.
   *
54
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
55
   *   The entity manager.
56
   * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
57
   *   The field type manager.
58
59
   * @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager
   *   The widget or formatter plugin manager.
60
61
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The configuration factory.
62
   */
63
  public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, ConfigFactoryInterface $config_factory) {
64
65
    parent::__construct($entity_manager);

66
    $this->fieldTypes = $field_type_manager->getDefinitions();
67
    $this->pluginManager = $plugin_manager;
68
    $this->configFactory = $config_factory;
69
70
71
72
73
74
75
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
76
      $container->get('entity.manager'),
77
      $container->get('plugin.manager.field.field_type'),
78
79
      $container->get('plugin.manager.field.widget'),
      $container->get('config.factory')
80
    );
81
82
83
84
85
86
87
88
  }

  /**
   * {@inheritdoc}
   */
  public function getRegions() {
    return array(
      'content' => array(
89
        'title' => $this->t('Content'),
90
        'invisible' => TRUE,
91
        'message' => $this->t('No field is displayed.')
92
93
      ),
      'hidden' => array(
94
95
        'title' => $this->t('Disabled'),
        'message' => $this->t('No field is hidden.')
96
97
98
99
      ),
    );
  }

100
101
102
103
104
105
106
107
  /**
   * 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;
108
    return array_filter($this->entityManager->getFieldDefinitions($this->entity_type, $this->bundle), function(FieldDefinitionInterface $field_definition) use ($context) {
109
110
111
112
      return $field_definition->isDisplayConfigurable($context);
    });
  }

113
114
115
  /**
   * {@inheritdoc}
   */
116
117
  public function buildForm(array $form, array &$form_state, $entity_type_id = NULL, $bundle = NULL) {
    parent::buildForm($form, $form_state, $entity_type_id, $bundle);
118

119
120
121
    if (empty($this->mode)) {
      $this->mode = 'default';
    }
122

123
    $field_definitions = $this->getFieldDefinitions();
124
125
126
127
128
129
130
131
132
133
134
    $extra_fields = $this->getExtraFields();
    $entity_display = $this->getEntityDisplay($this->mode);

    $form_state += array(
      'plugin_settings_edit' => NULL,
    );

    $form += array(
      '#entity_type' => $this->entity_type,
      '#bundle' => $this->bundle,
      '#mode' => $this->mode,
135
      '#fields' => array_keys($field_definitions),
136
137
138
      '#extra' => array_keys($extra_fields),
    );

139
140
    if (empty($field_definitions) && empty($extra_fields) && $route_info = FieldUI::getOverviewRouteInfo($this->entity_type, $this->bundle)) {
      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' => $this->url($route_info['route_name'], $route_info['route_parameters'], $route_info['options']))), 'warning');
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
      return $form;
    }

    $table = array(
      '#type' => 'field_ui_table',
      '#pre_render' => array(array($this, 'tablePreRender')),
      '#tree' => TRUE,
      '#header' => $this->getTableHeader(),
      '#regions' => $this->getRegions(),
      '#attributes' => array(
        'class' => array('field-ui-overview'),
        'id' => 'field-display-overview',
      ),
      // Add Ajax wrapper.
      '#prefix' => '<div id="field-display-overview-wrapper">',
      '#suffix' => '</div>',
157
158
159
160
161
162
163
164
165
166
167
168
169
170
      '#tabledrag' => array(
        array(
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'field-weight',
        ),
        array(
          'action' => 'match',
          'relationship' => 'parent',
          'group' => 'field-parent',
          'subgroup' => 'field-parent',
          'source' => 'field-name',
        ),
      ),
171
172
173
    );

    // Field rows.
174
175
    foreach ($field_definitions as $field_name => $field_definition) {
      $table[$field_name] = $this->buildFieldRow($field_definition, $entity_display, $form, $form_state);
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    }

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

    $form['fields'] = $table;

    // Custom display settings.
    if ($this->mode == 'default') {
      // Only show the settings if there is at least one custom display mode.
      if ($display_modes = $this->getDisplayModes()) {
        $form['modes'] = array(
          '#type' => 'details',
191
          '#title' => $this->t('Custom display settings'),
192
193
194
195
196
        );
        // Collect options and default values for the 'Custom display settings'
        // checkboxes.
        $options = array();
        $default = array();
197
        $display_statuses = $this->getDisplayStatuses();
198
199
        foreach ($display_modes as $mode_name => $mode_info) {
          $options[$mode_name] = $mode_info['label'];
200
          if (!empty($display_statuses[$mode_name])) {
201
202
203
204
205
            $default[] = $mode_name;
          }
        }
        $form['modes']['display_modes_custom'] = array(
          '#type' => 'checkboxes',
206
          '#title' => $this->t('Use custom display settings for the following modes'),
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
          '#options' => $options,
          '#default_value' => $default,
        );
      }
    }

    // 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',
222
      '#value' => $this->t('Refresh'),
223
224
225
226
227
228
229
230
231
232
233
234
235
236
      '#op' => 'refresh_table',
      '#submit' => array(array($this, 'multistepSubmit')),
      '#ajax' => array(
        'callback' => array($this, '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');
237
238
239
240
241
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this->t('Save'),
    );
242

243
    $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
244
245
246
247
248
249
250

    return $form;
  }

  /**
   * Builds the table row structure for a single field.
   *
251
252
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The field definition.
253
   * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $entity_display
254
255
256
257
258
259
260
261
262
   *   The entity display.
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_state
   *   A reference to a keyed array containing the current state of the form.
   *
   * @return array
   *   A table row array.
   */
263
264
265
266
  protected function buildFieldRow(FieldDefinitionInterface $field_definition, EntityDisplayInterface $entity_display, array $form, array &$form_state) {
    $field_name = $field_definition->getName();
    $display_options = $entity_display->getComponent($field_name);
    $label = $field_definition->getLabel();
267

268
    $regions = array_keys($this->getRegions());
269
270
271
272
273
274
    $field_row = array(
      '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
      '#row_type' => 'field',
      '#region_callback' => array($this, 'getRowRegion'),
      '#js_settings' => array(
        'rowHandler' => 'field',
275
        'defaultPlugin' => $this->getDefaultPlugin($field_definition->getType()),
276
277
      ),
      'human_name' => array(
278
        '#markup' => check_plain($label),
279
280
281
      ),
      'weight' => array(
        '#type' => 'textfield',
282
        '#title' => $this->t('Weight for @title', array('@title' => $label)),
283
284
285
286
287
288
289
290
        '#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',
291
          '#title' => $this->t('Label display for @title', array('@title' => $label)),
292
          '#title_display' => 'invisible',
293
          '#options' => array_combine($regions, $regions),
294
295
          '#empty_value' => '',
          '#attributes' => array('class' => array('field-parent')),
296
          '#parents' => array('fields', $field_name, 'parent'),
297
298
299
        ),
        'hidden_name' => array(
          '#type' => 'hidden',
300
          '#default_value' => $field_name,
301
302
303
304
305
306
307
308
309
          '#attributes' => array('class' => array('field-name')),
        ),
      ),

    );

    $field_row['plugin'] = array(
      'type' => array(
        '#type' => 'select',
310
        '#title' => $this->t('Plugin for @title', array('@title' => $label)),
311
        '#title_display' => 'invisible',
312
        '#options' => $this->getPluginOptions($field_definition->getType()),
313
        '#default_value' => $display_options ? $display_options['type'] : 'hidden',
314
        '#parents' => array('fields', $field_name, 'type'),
315
316
317
318
319
320
321
        '#attributes' => array('class' => array('field-plugin-type')),
      ),
      'settings_edit_form' => array(),
    );

    // Check the currently selected plugin, and merge persisted values for its
    // settings.
322
323
    if (isset($form_state['values']['fields'][$field_name]['type'])) {
      $display_options['type'] = $form_state['values']['fields'][$field_name]['type'];
324
    }
325
326
    if (isset($form_state['plugin_settings'][$field_name])) {
      $display_options['settings'] = $form_state['plugin_settings'][$field_name];
327
328
329
    }

    // Get the corresponding plugin object.
330
    $plugin = $this->getPlugin($field_definition, $display_options);
331
332
333
334
335
336
337
338
339

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

343
    if ($form_state['plugin_settings_edit'] == $field_name) {
344
345
346
347
348
349
350
      // 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);
351
        $this->alterSettingsForm($settings_form, $plugin, $field_definition, $form, $form_state);
352
353
354
355
356
357

        if ($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')),
358
            '#parents' => array('fields', $field_name, 'settings_edit_form'),
359
            'label' => array(
360
              '#markup' => $this->t('Plugin settings'),
361
362
363
364
365
366
            ),
            'settings' => $settings_form,
            'actions' => array(
              '#type' => 'actions',
              'save_settings' => $base_button + array(
                '#type' => 'submit',
367
                '#button_type' => 'primary',
368
                '#name' => $field_name . '_plugin_settings_update',
369
                '#value' => $this->t('Update'),
370
371
372
373
                '#op' => 'update',
              ),
              'cancel_settings' => $base_button + array(
                '#type' => 'submit',
374
                '#name' => $field_name . '_plugin_settings_cancel',
375
                '#value' => $this->t('Cancel'),
376
377
378
                '#op' => 'cancel',
                // Do not check errors for the 'Cancel' button, but make sure we
                // get the value of the 'plugin type' select.
379
                '#limit_validation_errors' => array(array('fields', $field_name, 'type')),
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
              ),
            ),
          );
          $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.
397
        $this->alterSettingsSummary($summary, $plugin, $field_definition);
398
399
400
401
402
403

        if (!empty($summary)) {
          $field_row['settings_summary'] = array(
            '#markup' => '<div class="field-plugin-summary">' . implode('<br />', $summary) . '</div>',
            '#cell_attributes' => array('class' => array('field-plugin-summary-cell')),
          );
404
        }
405
406

        // Check selected plugin settings to display edit link or not.
407
        if ($this->pluginManager->getDefaultSettings($display_options['type'])) {
408
409
          $field_row['settings_edit'] = $base_button + array(
            '#type' => 'image_button',
410
            '#name' => $field_name . '_settings_edit',
411
            '#src' => 'core/misc/configure-dark.png',
412
            '#attributes' => array('class' => array('field-plugin-settings-edit'), 'alt' => $this->t('Edit')),
413
414
415
            '#op' => 'edit',
            // Do not check errors for the 'Edit' button, but make sure we get
            // the value of the 'plugin type' select.
416
            '#limit_validation_errors' => array(array('fields', $field_name, 'type')),
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
            '#prefix' => '<div class="field-plugin-settings-edit-wrapper">',
            '#suffix' => '</div>',
          );
        }
      }
    }

    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.
434
   * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $entity_display
435
436
437
438
439
   *   The entity display.
   *
   * @return array
   *   A table row array.
   */
440
  protected function buildExtraFieldRow($field_id, $extra_field, EntityDisplayInterface $entity_display) {
441
442
    $display_options = $entity_display->getComponent($field_id);

443
    $regions = array_keys($this->getRegions());
444
445
446
447
448
449
    $extra_field_row = array(
      '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
      '#row_type' => 'extra_field',
      '#region_callback' => array($this, 'getRowRegion'),
      '#js_settings' => array('rowHandler' => 'field'),
      'human_name' => array(
450
        '#markup' => $extra_field['label'],
451
452
453
      ),
      'weight' => array(
        '#type' => 'textfield',
454
        '#title' => $this->t('Weight for @title', array('@title' => $extra_field['label'])),
455
456
457
458
459
460
461
462
        '#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',
463
          '#title' => $this->t('Parents for @title', array('@title' => $extra_field['label'])),
464
          '#title_display' => 'invisible',
465
          '#options' => array_combine($regions, $regions),
466
467
468
469
470
471
472
473
474
475
476
477
478
          '#empty_value' => '',
          '#attributes' => array('class' => array('field-parent')),
          '#parents' => array('fields', $field_id, 'parent'),
        ),
        'hidden_name' => array(
          '#type' => 'hidden',
          '#default_value' => $field_id,
          '#attributes' => array('class' => array('field-name')),
        ),
      ),
      'plugin' => array(
        'type' => array(
          '#type' => 'select',
479
          '#title' => $this->t('Visibility for @title', array('@title' => $extra_field['label'])),
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
          '#title_display' => 'invisible',
          '#options' => $this->getExtraFieldVisibilityOptions(),
          '#default_value' => $display_options ? 'visible' : 'hidden',
          '#parents' => array('fields', $field_id, 'type'),
          '#attributes' => array('class' => array('field-plugin-type')),
        ),
      ),
      'settings_summary' => array(),
      'settings_edit' => array(),
    );

    return $extra_field_row;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    $form_values = $form_state['values'];
    $display = $this->getEntityDisplay($this->mode);

    // Collect data for 'regular' fields.
    foreach ($form['#fields'] as $field_name) {
      // Retrieve the stored instance settings to merge with the incoming
      // values.
      $values = $form_values['fields'][$field_name];

      if ($values['type'] == 'hidden') {
        $display->removeComponent($field_name);
      }
      else {
        // Get plugin settings. They lie either directly in submitted form
        // values (if the whole form was submitted while some plugin settings
        // were being edited), or have been persisted in $form_state.
        $settings = array();
        if (isset($values['settings_edit_form']['settings'])) {
          $settings = $values['settings_edit_form']['settings'];
        }
        elseif (isset($form_state['plugin_settings'][$field_name])) {
          $settings = $form_state['plugin_settings'][$field_name];
        }
        elseif ($current_options = $display->getComponent($field_name)) {
          $settings = $current_options['settings'];
        }

        // Only save settings actually used by the selected plugin.
        $default_settings = $this->pluginManager->getDefaultSettings($values['type']);
        $settings = array_intersect_key($settings, $default_settings);

        // Default component values.
        $component_values = array(
          'type' => $values['type'],
          'weight' => $values['weight'],
          'settings' => $settings
        );

        // Only formatters have configurable label visibility.
        if (isset($values['label'])) {
          $component_values['label'] = $values['label'];
        }

        $display->setComponent($field_name, $component_values);
      }
    }

    // Collect data for 'extra' fields.
    foreach ($form['#extra'] as $name) {
      if ($form_values['fields'][$name]['type'] == 'hidden') {
        $display->removeComponent($name);
      }
      else {
        $display->setComponent($name, array(
          'weight' => $form_values['fields'][$name]['weight'],
        ));
      }
    }

    // Save the display.
    $display->save();

560
    // Handle the 'display modes' checkboxes if present.
561
562
    if ($this->mode == 'default' && !empty($form_values['display_modes_custom'])) {
      $display_modes = $this->getDisplayModes();
563
      $current_statuses = $this->getDisplayStatuses();
564

565
      $statuses = array();
566
      foreach ($form_values['display_modes_custom'] as $mode => $value) {
567
        if (!empty($value) && empty($current_statuses[$mode])) {
568
569
570
          // If no display exists for the newly enabled view mode, initialize
          // it with those from the 'default' view mode, which were used so
          // far.
571
          if (!entity_load($this->getEntityDisplay('default')->getEntityTypeId(), $this->entity_type . '.' . $this->bundle . '.' . $mode)) {
572
573
574
575
576
            $display = $this->getEntityDisplay('default')->createCopy($mode);
            $display->save();
          }

          $display_mode_label = $display_modes[$mode]['label'];
577
578
          $route = $this->getOverviewRoute($mode);
          drupal_set_message($this->t('The %display_mode mode now uses custom display settings. You might want to <a href="@url">configure them</a>.', array('%display_mode' => $display_mode_label, '@url' => $this->url($route['route_name'], $route['route_parameters'], $route['options']))));
579
        }
580
        $statuses[$mode] = !empty($value);
581
582
      }

583
      $this->saveDisplayStatuses($statuses);
584
585
    }

586
    drupal_set_message($this->t('Your settings have been saved.'));
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  }

  /**
   * Form submission handler for multistep buttons.
   */
  public function multistepSubmit($form, &$form_state) {
    $trigger = $form_state['triggering_element'];
    $op = $trigger['#op'];

    switch ($op) {
      case 'edit':
        // Store the field whose settings are currently being edited.
        $field_name = $trigger['#field_name'];
        $form_state['plugin_settings_edit'] = $field_name;
        break;

      case 'update':
        // Store the saved settings, and set the field back to 'non edit' mode.
        $field_name = $trigger['#field_name'];
        $values = $form_state['values']['fields'][$field_name]['settings_edit_form']['settings'];
        $form_state['plugin_settings'][$field_name] = $values;
        unset($form_state['plugin_settings_edit']);
        break;

      case 'cancel':
        // Set the field back to 'non edit' mode.
        unset($form_state['plugin_settings_edit']);
        break;

      case 'refresh_table':
        // If the currently edited field is one of the rows to be refreshed, set
        // it back to 'non edit' mode.
        $updated_rows = explode(' ', $form_state['values']['refresh_rows']);
        if (isset($form_state['plugin_settings_edit']) && in_array($form_state['plugin_settings_edit'], $updated_rows)) {
          unset($form_state['plugin_settings_edit']);
        }
        break;
    }

    $form_state['rebuild'] = TRUE;
  }

  /**
   * Ajax handler for multistep buttons.
   */
  public function multistepAjax($form, &$form_state) {
    $trigger = $form_state['triggering_element'];
    $op = $trigger['#op'];

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

      case 'update':
      case 'cancel':
        $updated_rows = array($trigger['#field_name']);
        $updated_columns = array('plugin', 'settings_summary', 'settings_edit');
        break;

      case 'refresh_table':
        $updated_rows = array_values(explode(' ', $form_state['values']['refresh_rows']));
        $updated_columns = array('settings_summary', 'settings_edit');
        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'];
  }

  /**
   * Returns the entity display object used by this form.
   *
   * @param string $mode
   *   A view or form mode.
   *
673
   * @return \Drupal\Core\Entity\Display\EntityDisplayInterface
674
675
676
677
678
679
680
681
682
683
   *   An entity display.
   */
  abstract protected function getEntityDisplay($mode);

  /**
   * Returns the extra fields of the entity type and bundle used by this form.
   *
   * @return array
   *   An array of extra field info, as provided by field_info_extra_fields().
   */
684
685
686
  protected function getExtraFields() {
    return field_info_extra_fields($this->entity_type, $this->bundle, ($this->displayContext == 'view' ? 'display' : $this->displayContext));
  }
687
688
689
690

  /**
   * Returns the widget or formatter plugin for a field.
   *
691
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
692
693
694
695
696
697
698
   *   The field instance.
   * @param array $configuration
   *   The plugin configuration
   *
   * @return object
   *   The corresponding plugin.
   */
699
  abstract protected function getPlugin(FieldDefinitionInterface $field_definition, $configuration);
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733

  /**
   * Returns an array of widget or formatter options for a field type.
   *
   * @param string $field_type
   *   The name of the field type.
   *
   * @return array
   *   An array of widget or formatter options.
   */
  protected function getPluginOptions($field_type) {
    return $this->pluginManager->getOptions($field_type);
  }

  /**
   * 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();

  /**
734
   * Returns the display entity type.
735
   *
736
737
   * @return string
   *   The name of the display entity type.
738
   */
739
  abstract protected function getDisplayType();
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765

  /**
   * 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.
   */
  public function getRowRegion($row) {
    switch ($row['#row_type']) {
      case 'field':
      case 'extra_field':
        return ($row['plugin']['type']['#value'] == 'hidden' ? 'hidden' : 'content');
    }
  }

  /**
   * Returns an array of visibility options for extra fields.
   *
   * @return array
   *   An array of visibility options.
   */
  protected function getExtraFieldVisibilityOptions() {
    return array(
766
767
      'visible' => $this->t('Visible'),
      'hidden' => '- ' . $this->t('Hidden') . ' -',
768
769
770
    );
  }

771
772
773
774
775
776
777
778
779
  /**
   * Returns entity (form) displays for the current entity display type.
   *
   * @return array
   *   An array holding entity displays or entity form displays.
   */
  protected function getDisplays() {
    $load_ids = array();
    $display_entity_type = $this->getDisplayType();
780
781
    $entity_type = $this->entityManager->getDefinition($display_entity_type);
    $config_prefix = $entity_type->getConfigPrefix();
782
    $ids = $this->configFactory->listAll($config_prefix . '.' . $this->entity_type . '.' . $this->bundle . '.');
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
    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;
      }
    }
    return entity_load_multiple($display_entity_type, $load_ids);
  }

  /**
   * 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() {
    $display_statuses = array();
    $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) {
      $display->set('status', $display_statuses[$display->get('mode')]);
      $display->save();
    }
  }

822
823
824
825
826
827
828
829
830
  /**
   * Returns an array containing the table headers.
   *
   * @return array
   *   The table header.
   */
  abstract protected function getTableHeader();

  /**
831
   * Returns the route info of a specific form or view mode form.
832
833
834
835
   *
   * @param string $mode
   *   The form or view mode.
   *
836
837
838
839
840
841
842
843
   * @return array
   *   An associative array with the following keys:
   *   - route_name: The name of the route.
   *   - route_parameters: (optional) An associative array of parameter names
   *     and values.
   *   - options: (optional) An associative array of additional options. See
   *     \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
   *     comprehensive documentation.
844
   */
845
  abstract protected function getOverviewRoute($mode);
846
847
848
849
850
851
852
853

  /**
   * Alters the widget or formatter settings form.
   *
   * @param array $settings_form
   *   The widget or formatter settings form.
   * @param object $plugin
   *   The widget or formatter.
854
855
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The field definition.
856
857
858
859
860
   * @param array $form
   *   The The (entire) configuration form array.
   * @param array $form_state
   *   The form state.
   */
861
  abstract protected function alterSettingsForm(array &$settings_form, $plugin, FieldDefinitionInterface $field_definition, array $form, array &$form_state);
862
863
864
865
866
867
868
869

  /**
   * Alters the widget or formatter settings summary.
   *
   * @param array $summary
   *   The widget or formatter settings summary.
   * @param object $plugin
   *   The widget or formatter.
870
871
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The field definition.
872
   */
873
  abstract protected function alterSettingsSummary(array &$summary, $plugin, FieldDefinitionInterface $field_definition);
874
875

}