.../EntityReferenceEntityFormatter.php | 45 +++++++++++++++++++++- core/misc/ajax.es6.js | 6 +++ core/misc/ajax.js | 4 ++ .../src/Controller/EntityDisplayModeController.php | 10 +++++ .../field_ui/src/Form/EntityDisplayFormBase.php | 2 + .../field_ui/src/Routing/RouteSubscriber.php | 5 ++- ...core.entity_view_display.media.file.default.yml | 36 +++-------------- ...ore.entity_view_display.media.image.default.yml | 34 +++------------- 8 files changed, 79 insertions(+), 63 deletions(-) diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php index af99436..e57cefd 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php @@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -128,12 +129,54 @@ public static function defaultSettings() { * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { + $entity_type_id = $this->getFieldSetting('target_type'); + $bundle_entity_type_id = $this->entityTypeManager->getDefinition($entity_type_id)->getBundleEntityType(); + $bundles = array_keys($this->getFieldSetting('handler_settings')['target_bundles']); + if (count($bundles) === 1) { + $description = [ + '#type' => 'link', + '#title' => $this->t('Configure view modes'), + '#url' => Url::fromRoute(sprintf('entity.entity_view_display.%s.default', $entity_type_id), [$bundle_entity_type_id => $bundles[0]]), + '#attributes' => [ + 'class' => ['use-ajax'], + 'data-dialog-type' => 'modal', + 'data-dialog-options' => json_encode([ + 'width' => 800, + ]), + ], + ]; + } + else { + $bundle_info = entity_get_bundles($entity_type_id); + $description = [ + '#theme' => 'item_list', + '#context' => ['list_style' => 'comma-list'], + '#items' => [], + ]; + for ($i = 0; $i < count($bundles); $i++) { + $description['#items'][] = [ + '#type' => 'link', + '#title' => $this->t('Configure %bundle-label view modes', ['%bundle-label' => $bundle_info[$bundles[$i]]['label']]), + '#url' => Url::fromRoute(sprintf('entity.entity_view_display.%s.default', $entity_type_id), [$bundle_entity_type_id => $bundles[$i]]), + '#attributes' => [ + 'class' => ['use-ajax'], + 'data-dialog-type' => 'modal', + 'data-dialog-options' => json_encode([ + 'width' => 800, + ]), + ], + ]; + } + } $elements['view_mode'] = [ '#type' => 'select', - '#options' => $this->entityDisplayRepository->getViewModeOptions($this->getFieldSetting('target_type')), + '#options' => $this->entityDisplayRepository->getViewModeOptions($entity_type_id), '#title' => t('View mode'), '#default_value' => $this->getSetting('view_mode'), '#required' => TRUE, + '#description' => $description + [ + '#access' => \Drupal::currentUser()->hasPermission('administer ' . $entity_type_id . ' display') + ], ]; return $elements; diff --git a/core/misc/ajax.es6.js b/core/misc/ajax.es6.js index daa6254..3afe193 100644 --- a/core/misc/ajax.es6.js +++ b/core/misc/ajax.es6.js @@ -367,6 +367,12 @@ * @type {string} */ this.wrapper = `#${this.wrapper}`; + // To avoid problems when the same form exists multiple times on the same page, allow its + // wrapper to be scoped to the form it lives in. This can f.e. happen when the same base form + // is both in the parent page and in a modal. + if (this.scope && this.scope === 'form') { + this.wrapper = `#${element.form.id} ` + this.wrapper; + } } /** diff --git a/core/misc/ajax.js b/core/misc/ajax.js index 1e15175..e827c88 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -163,6 +163,10 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr if (this.wrapper) { this.wrapper = '#' + this.wrapper; + + if (this.scope && this.scope === 'form') { + this.wrapper = '#' + element.form.id + ' ' + this.wrapper; + } } this.element = element; diff --git a/core/modules/field_ui/src/Controller/EntityDisplayModeController.php b/core/modules/field_ui/src/Controller/EntityDisplayModeController.php index b4a608a..8edfd45 100644 --- a/core/modules/field_ui/src/Controller/EntityDisplayModeController.php +++ b/core/modules/field_ui/src/Controller/EntityDisplayModeController.php @@ -3,6 +3,7 @@ namespace Drupal\field_ui\Controller; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Entity\Entity\EntityViewMode; use Drupal\Core\Url; /** @@ -10,6 +11,15 @@ */ class EntityDisplayModeController extends ControllerBase { + public function editViewDisplayTitle($entity_type_id, $bundle, $view_mode_name) { + return $this->t('Edit %view-mode-label display of %bundle-label %entity-type-label', [ + '%view-mode-label' => $view_mode_name === 'default' + ? t('default') + : EntityViewMode::load("$entity_type_id.$view_mode_name")->label(), + '%bundle-label' => $this->entityManager()->getBundleInfo($entity_type_id)[$bundle]['label'], + '%entity-type-label' => $this->entityTypeManager()->getDefinition($entity_type_id)->getLowercaseLabel(), + ]); + } /** * Provides a list of eligible entity types for adding view modes. * diff --git a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php index 0a91862..8684a19 100644 --- a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php +++ b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php @@ -235,6 +235,7 @@ public function form(array $form, FormStateInterface $form_state) { '#submit' => ['::multistepSubmit'], '#ajax' => [ 'callback' => '::multistepAjax', + 'scope' => 'form', 'wrapper' => 'field-display-overview-wrapper', 'effect' => 'fade', // The button stays hidden, so we hide the Ajax spinner too. Ad-hoc @@ -347,6 +348,7 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, arr '#submit' => ['::multistepSubmit'], '#ajax' => [ 'callback' => '::multistepAjax', + 'scope' => 'form', 'wrapper' => 'field-display-overview-wrapper', 'effect' => 'fade', ], diff --git a/core/modules/field_ui/src/Routing/RouteSubscriber.php b/core/modules/field_ui/src/Routing/RouteSubscriber.php index b08e7f8..495f14a 100644 --- a/core/modules/field_ui/src/Routing/RouteSubscriber.php +++ b/core/modules/field_ui/src/Routing/RouteSubscriber.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Routing\RouteSubscriberBase; use Drupal\Core\Routing\RoutingEvents; +use Drupal\field_ui\Controller\EntityDisplayModeController; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; @@ -136,7 +137,7 @@ protected function alterRoutes(RouteCollection $collection) { "$path/display", [ '_entity_form' => 'entity_view_display.edit', - '_title' => 'Manage display', + '_title_callback' => EntityDisplayModeController::class . '::editViewDisplayTitle', 'view_mode_name' => 'default', ] + $defaults, ['_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display'], @@ -148,7 +149,7 @@ protected function alterRoutes(RouteCollection $collection) { "$path/display/{view_mode_name}", [ '_entity_form' => 'entity_view_display.edit', - '_title' => 'Manage display', + '_title_callback' => EntityDisplayModeController::class . '::editViewDisplayTitle', ] + $defaults, ['_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display'], $options diff --git a/core/modules/media/config/install/core.entity_view_display.media.file.default.yml b/core/modules/media/config/install/core.entity_view_display.media.file.default.yml index 697f117..dd704e1 100644 --- a/core/modules/media/config/install/core.entity_view_display.media.file.default.yml +++ b/core/modules/media/config/install/core.entity_view_display.media.file.default.yml @@ -3,48 +3,22 @@ status: true dependencies: config: - field.field.media.file.field_media_file - - image.style.thumbnail - media.type.file module: - file - - image - - user id: media.file.default targetEntityType: media bundle: file mode: default content: - created: - label: hidden - type: timestamp - weight: 0 - region: content - settings: - date_format: medium - custom_date_format: '' - timezone: '' - third_party_settings: { } field_media_file: - label: above + label: hidden settings: { } third_party_settings: { } type: file_default - weight: 6 - region: content - thumbnail: - type: image - weight: 5 - label: hidden - settings: - image_style: thumbnail - image_link: '' - region: content - third_party_settings: { } - uid: - label: hidden - type: author weight: 0 region: content - settings: { } - third_party_settings: { } -hidden: { } +hidden: + created: true + thumbnail: true + uid: true diff --git a/core/modules/media/config/install/core.entity_view_display.media.image.default.yml b/core/modules/media/config/install/core.entity_view_display.media.image.default.yml index e541d7f..f46a79c 100644 --- a/core/modules/media/config/install/core.entity_view_display.media.image.default.yml +++ b/core/modules/media/config/install/core.entity_view_display.media.image.default.yml @@ -7,45 +7,21 @@ dependencies: - media.type.image module: - image - - user id: media.image.default targetEntityType: media bundle: image mode: default content: - created: - label: hidden - type: timestamp - weight: 0 - region: content - settings: - date_format: medium - custom_date_format: '' - timezone: '' - third_party_settings: { } field_media_image: - label: above - settings: - image_style: '' - image_link: '' - third_party_settings: { } - type: image - weight: 6 - region: content - thumbnail: - type: image - weight: 5 label: hidden settings: image_style: thumbnail image_link: '' - region: content third_party_settings: { } - uid: - label: hidden - type: author + type: image weight: 0 region: content - settings: { } - third_party_settings: { } -hidden: { } +hidden: + created: true + thumbnail: true + uid: true