diff --git a/core/includes/entity.inc b/core/includes/entity.inc index 91cbc1e..9e822d5 100644 --- a/core/includes/entity.inc +++ b/core/includes/entity.inc @@ -604,12 +604,12 @@ function entity_view_multiple(array $entities, $view_mode, $langcode = NULL, $re } /** - * Returns the entity_display object associated to a bundle and view mode. + * Returns the entity_display object for a bundle and view mode. * - * Use this function when assigning suggested display options for a component - * in a given view mode. Note that they will only be actually used at render - * time if the view mode itself is configured to use dedicated display settings - * for the bundle; if not, the 'default' display is used instead. + * Use this function when assigning suggested display options for a component in + * a given view mode. Note that they will only be actually used at render time + * if the "status" of the display is set to TRUE; if not, the 'default' display + * is used instead. * * The function reads the entity_display object from the current configuration, * or returns a ready-to-use empty one if configuration entry exists yet for @@ -664,62 +664,6 @@ function entity_get_display($entity_type, $bundle, $view_mode) { } /** - * Returns the entity_display object used to render an entity. - * - * Depending on the configuration of the view mode for the bundle, this can be - * either the display object associated to the view mode, or the 'default' - * display. - * - * This function should only be used internally when rendering an entity. When - * assigning suggested display options for a component in a given view mode, - * entity_get_display() should be used instead, in order to avoid inadvertently - * modifying the output of other view modes that might happen to use the - * 'default' display too. Those options will then be effectively applied only - * if the view mode is configured to use them. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity being rendered. - * @param string $view_mode - * The view mode being rendered. - * - * @return \Drupal\entity\Entity\EntityDisplay - * The display object that should be used to render the entity. - * - * @see entity_get_display(). - */ -function entity_get_render_display(EntityInterface $entity, $view_mode) { - $entity_type = $entity->entityType(); - $bundle = $entity->bundle(); - $render_view_mode = 'default'; - - // Look at the default display and display for the view mode, and fallback to - // the former if the latter does not exist is disabled. - if ($view_mode != 'default') { - $ids = array( - 'default' => $entity_type . '.' . $bundle . '.default', - $view_mode => $entity_type . '.' . $bundle . '.' . $view_mode, - ); - $entity_displays = entity_load_multiple('entity_display', $ids); - if (isset($entity_displays[$ids[$view_mode]]) && $entity_displays[$ids[$view_mode]]->status()) { - $render_view_mode = $view_mode; - } - } - - $display = entity_get_display($entity_type, $bundle, $render_view_mode); - $display->originalMode = $view_mode; - - // Let modules alter the display. - $display_context = array( - 'entity_type' => $entity_type, - 'bundle' => $bundle, - 'view_mode' => $view_mode, - ); - drupal_alter('entity_display', $display, $display_context); - - return $display; -} - -/** * Returns the entity_form_display object associated to a bundle and form mode. * * The function reads the entity_form_display object from the current @@ -795,37 +739,7 @@ function entity_get_form_display($entity_type, $bundle, $form_mode) { * * @see entity_get_form_display(). */ -function entity_get_render_form_display(EntityInterface $entity, $form_mode) { - $entity_type = $entity->entityType(); - $bundle = $entity->bundle(); - $render_form_mode = 'default'; - // Look at the default form display and form display for the view mode, and - // fallback to the former if the latter does not exist is disabled. - if ($form_mode != 'default') { - $ids = array( - 'default' => $entity_type . '.' . $bundle . '.default', - $form_mode => $entity_type . '.' . $bundle . '.' . $form_mode, - ); - $entity_form_displays = entity_load_multiple('entity_form_display', $ids); - if (isset($entity_form_displays[$ids[$form_mode]]) && $entity_form_displays[$ids[$form_mode]]->status()) { - $render_form_mode = $form_mode; - } - } - - $form_display = entity_get_form_display($entity_type, $bundle, $render_form_mode); - $form_display->originalMode = $form_mode; - - // Let modules alter the form display. - $form_display_context = array( - 'entity_type' => $entity_type, - 'bundle' => $bundle, - 'form_mode' => $form_mode, - ); - drupal_alter('entity_form_display', $form_display, $form_display_context); - - return $form_display; -} /** * Generic access callback for entity pages. diff --git a/core/lib/Drupal/Core/Entity/EntityFormController.php b/core/lib/Drupal/Core/Entity/EntityFormController.php index 17743ec..d3ad831 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormController.php +++ b/core/lib/Drupal/Core/Entity/EntityFormController.php @@ -8,11 +8,8 @@ namespace Drupal\Core\Entity; use Drupal\Core\Form\FormBase; -use Drupal\Core\TypedData\TranslatableInterface; use Drupal\entity\EntityFormDisplayInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Language\Language; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base class for entity form controllers. @@ -124,7 +121,7 @@ protected function init(array &$form_state) { // Prepare the entity to be presented in the entity form. $this->prepareEntity(); - $form_display = entity_get_render_form_display($this->entity, $this->getOperation()); + $form_display = $this->collectRenderDisplay($this->entity->entityType(), $this->entity->bundle(), $this->getOperation()); $this->setFormDisplay($form_display, $form_state); // Invoke the prepare form hooks. @@ -133,6 +130,61 @@ protected function init(array &$form_state) { } /** + * Returns the entity_form_display object used to build the form. + * + * If the "status" of the display object associated to the form mode is FALSE, + * the 'default' display is used for rendering. + * + * @param array $entities + * An array of entities implementing EntityInterface. + * @param string $form_mode + * The form mode.. + * + * @return \Drupal\entity\EntityFormDisplayInterface + * The EntityFormDisplay object to use for the form. + */ + public function collectRenderDisplay($entity_type, $bundle, $form_mode) { + // Look at the default form display and form display for the view mode, and + // fallback to the former if the latter does not exist is disabled. + $ids['default'] = $entity_type . '.' . $bundle . '.default'; + if ($form_mode != 'default') { + $ids[$form_mode] = $entity_type . '.' . $bundle . '.' . $form_mode; + } + + $displays = \Drupal::entityManager()->getStorageController('entity_form_display')->loadMultiple($ids); + + // Use the display for the form mode if it exists and is not disabled. + if ($form_mode != 'default') { + $candidate_id = $ids[$form_mode]; + if (isset($displays[$candidate_id]) && $displays[$candidate_id]->status()) { + $display = $displays[$candidate_id]; + } + } + // Else, use the 'default' if it exists. + if (empty($display)) { + $candidate_id = $ids['default']; + if (isset($displays[$candidate_id])) { + $display = $displays[$candidate_id]; + } + } + // Else, create a fresh display object. + if (empty($display)) { + $display = entity_get_form_display($entity_type, $bundle, 'default'); + } + $display->originalMode = $form_mode; + + // Let modules alter the display. + $display_context = array( + 'entity_type' => $entity_type, + 'bundle' => $bundle, + 'form_mode' => $form_mode, + ); + $this->moduleHandler->alter('entity_form_display', $display, $display_context); + + return $display; + } + + /** * Returns the actual form array to be built. * * @see \Drupal\Core\Entity\EntityFormController::build() diff --git a/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php index 6ad106b..a6e7e4d 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php @@ -83,6 +83,11 @@ public function getFormDisplay(array $form_state); public function setFormDisplay(EntityFormDisplayInterface $form_display, array &$form_state); /** + * {@inheritdoc} + */ + public function collectRenderDisplay($entity_type, $bundle, $form_mode); + + /** * Returns the form entity. * * The form entity which has been used for populating form element defaults. diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php index 178d9c4..87baea9 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Entity; -use Drupal\Core\Entity\EntityManager; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\Language; use Drupal\entity\Entity\EntityDisplay; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -41,6 +41,13 @@ class EntityViewBuilder implements EntityControllerInterface, EntityViewBuilderI protected $entityManager; /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** * An array of view mode info for the type of entities for which this * controller is instantiated. * @@ -68,10 +75,11 @@ class EntityViewBuilder implements EntityControllerInterface, EntityViewBuilderI * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager service. */ - public function __construct($entity_type, array $entity_info, EntityManager $entity_manager) { + public function __construct($entity_type, array $entity_info, EntityManager $entity_manager, ModuleHandlerInterface $module_handler) { $this->entityType = $entity_type; $this->entityInfo = $entity_info; $this->entityManager = $entity_manager; + $this->moduleHandler = $module_handler; $this->viewModesInfo = entity_get_view_modes($entity_type); } @@ -79,7 +87,7 @@ public function __construct($entity_type, array $entity_info, EntityManager $ent * {@inheritdoc} */ public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { - return new static($entity_type, $entity_info, $container->get('entity.manager')); + return new static($entity_type, $entity_info, $container->get('entity.manager'), $container->get('module_handler')); } /** @@ -105,7 +113,7 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang } } - module_invoke_all('entity_prepare_view', $this->entityType, $entities, $displays, $view_mode); + $this->moduleHandler->invokeAll('entity_prepare_view', array($this->entityType, $entities, $displays, $view_mode)); foreach ($entities as $entity) { // Remove previously built content, if exists. @@ -190,42 +198,38 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la $langcode = language(Language::TYPE_CONTENT)->id; } - // Build the view modes and display objects. $view_modes = array(); - $displays = array(); - $context = array('langcode' => $langcode); foreach ($entities as $key => $entity) { - $bundle = $entity->bundle(); - // Ensure that from now on we are dealing with the proper translation // object. $entity = $this->entityManager->getTranslationFromContext($entity, $langcode); $entities[$key] = $entity; - // Allow modules to change the view mode. + // Allow modules to switch individual entities to a different view mode, + // and group entities by view mode. $entity_view_mode = $view_mode; - drupal_alter('entity_view_mode', $entity_view_mode, $entity, $context); - // Store entities for rendering by view_mode. + $context = array('langcode' => $langcode); + $this->moduleHandler->alter('entity_view_mode', $entity_view_mode, $entity, $context); $view_modes[$entity_view_mode][$entity->id()] = $entity; - - // Get the corresponding display settings. - if (!isset($displays[$entity_view_mode][$bundle])) { - $displays[$entity_view_mode][$bundle] = entity_get_render_display($entity, $entity_view_mode); - } } - foreach ($view_modes as $mode => $view_mode_entities) { - $this->buildContent($view_mode_entities, $displays[$mode], $mode, $langcode); + // For each view mode, collect the display objects to use for the various + // entities, and let the controller build the initial content. + $displays = array(); + foreach ($view_modes as $view_mode => $view_mode_entities) { + $displays[$view_mode] = $this->collectRenderDisplays($view_mode_entities, $view_mode); + $this->buildContent($view_mode_entities, $displays[$view_mode], $view_mode, $langcode); } + // Build the rest of the content entity by entity. $view_hook = "{$this->entityType}_view"; $build = array('#sorted' => TRUE); $weight = 0; foreach ($entities as $key => $entity) { $entity_view_mode = isset($entity->content['#view_mode']) ? $entity->content['#view_mode'] : $view_mode; $display = $displays[$entity_view_mode][$entity->bundle()]; - module_invoke_all($view_hook, $entity, $display, $entity_view_mode, $langcode); - module_invoke_all('entity_view', $entity, $display, $entity_view_mode, $langcode); + $this->moduleHandler->invokeAll($view_hook, array($entity, $display, $entity_view_mode, $langcode)); + $this->moduleHandler->invokeAll('entity_view', array($entity, $display, $entity_view_mode, $langcode)); $build[$key] = $entity->content; // We don't need duplicate rendering info in $entity->content. @@ -244,7 +248,7 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la $build[$key]['#weight'] = $weight++; // Allow modules to modify the render array. - drupal_alter(array($view_hook, 'entity_view'), $build[$key], $entity, $display); + $this->moduleHandler->alter(array($view_hook, 'entity_view'), $build[$key], $entity, $display); } return $build; @@ -253,6 +257,73 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la /** * {@inheritdoc} */ + public function collectRenderDisplays($entities, $view_mode) { + // Collect bundles. + $bundles = array(); + foreach ($entities as $entity) { + $bundles[$entity->bundle()] = TRUE; + } + $bundles = array_keys($bundles); + + // For each bundle, load the 'default' display and the display for the view + // mode. + foreach ($bundles as $bundle) { + $ids["{$bundle}_default"] = $this->entityType . '.' . $bundle . '.default'; + if ($view_mode != 'default') { + $ids["{$bundle}_{$view_mode}"] = $this->entityType . '.' . $bundle . '.' . $view_mode; + } + } + + $displays = $this->entityManager->getStorageController('entity_display')->loadMultiple($ids); + + // Determine the actual display to use for each bundle. + $displays_by_bundle = array(); + foreach ($bundles as $bundle) { + // Use the display for the view mode if it exists and is not disabled. + if ($view_mode != 'default') { + $candidate_id = $ids["{$bundle}_{$view_mode}"]; + if (isset($displays[$candidate_id]) && $displays[$candidate_id]->status()) { + $display = $displays[$candidate_id]; + } + } + // Else, use the 'default' display if it exists. + if (empty($display)) { + $candidate_id = $ids["{$bundle}_default"]; + if (isset($displays[$candidate_id])) { + $display = $displays[$candidate_id]; + } + } + // Else, create a fresh display object. + if (empty($display)) { + $display = entity_get_display($this->entityType, $bundle, 'default'); + } + $display->originalMode = $view_mode; + + // Let modules alter the display. + $display_context = array( + 'entity_type' => $this->entityType, + 'bundle' => $bundle, + 'view_mode' => $view_mode, + ); + $this->moduleHandler->alter('entity_display', $display, $display_context); + + $displays_by_bundle[$bundle] = $display; + } + + return $displays_by_bundle; + } + + /** + * {@inheritdoc} + */ + public function collectRenderDisplay($entity, $view_mode) { + $displays = $this->collectRenderDisplays(array($entity), $view_mode); + return $displays[$entity->bundle()]; + } + + /** + * {@inheritdoc} + */ public function resetCache(array $entities = NULL) { if (isset($entities)) { $tags = array(); diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php b/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php index 0d026f2..6ec9cdb 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php @@ -15,8 +15,8 @@ /** * Build the structured $content property on the entity. * - * @param array $entities - * The entities, implementing EntityInterface, whose content is being built. + * @param \Drupal\Core\Entity\EntityInterface[] $entities + * The entities whose content is being built. * @param array $displays * The array of entity_display objects holding the display options * configured for the entity components, keyed by bundle name. @@ -56,10 +56,10 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N /** * Returns the render array for the provided entities. * - * @param array $entities - * An array of entities implementing EntityInterface to view. + * @param \Drupal\Core\Entity\EntityInterface[] $entities + * An array of entities to view. * @param string $view_mode - * (optional) The view mode that should be used to render the entity. + * (optional) The view mode that should be used to render the entities. * @param string $langcode * (optional) For which language the entity should be rendered, defaults to * the current content language. @@ -77,6 +77,39 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL); /** + * Returns the entity_display objects used to render entities. + * + * If the "status" of the display object associated to the view mode is FALSE, + * the 'default' display is used for rendering. + * + * @param \Drupal\Core\Entity\EntityInterface[] $entities + * An array of entities. + * @param string $view_mode + * The view mode. + * + * @return \Drupal\entity\EntityDisplayInterface[] + * The array of EntityDisplay objects to use for rendering, keyed by bundle + * name. + */ + public function collectRenderDisplays($entities, $view_mode); + + /** + * Returns the entity_display objects used to render a single entity. + * + * If the "status" of the display object associated to the view mode is FALSE, + * the 'default' display is used for rendering. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity. + * @param string $view_mode + * The view mode that should be used to render the entity. + * + * @return \Drupal\entity\EntityDisplayInterface + * The display objecy to use for rendering the entity. + */ + public function collectRenderDisplay($entity, $view_mode); + + /** * Resets the entity render cache. * * @param \Drupal\Core\Entity\EntityInterface[] $entities diff --git a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php index bfc136b..aea76d2 100644 --- a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php +++ b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php @@ -7,20 +7,13 @@ namespace Drupal\block; -use Drupal\Core\Entity\EntityViewBuilderInterface; +use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Core\Entity\EntityInterface; /** * Provides a Block view builder. */ -class BlockViewBuilder implements EntityViewBuilderInterface { - - /** - * {@inheritdoc} - */ - public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) { - return array(); - } +class BlockViewBuilder extends EntityViewBuilder { /** * {@inheritdoc} diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php index 82827ab..7a52f0d 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php @@ -24,7 +24,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase { * * @var array */ - public static $modules = array('block', 'block_test', 'system'); + public static $modules = array('block', 'block_test', 'system', 'entity'); /** * The block storage controller. diff --git a/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php b/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php index 230bd49..fbaee79 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php +++ b/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php @@ -31,13 +31,6 @@ class CommentViewBuilder extends EntityViewBuilder implements EntityViewBuilderI protected $fieldInfo; /** - * The module handler service. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** * The CSRF token manager service. * * @var \Drupal\Core\Access\CsrfTokenGenerator @@ -52,8 +45,8 @@ public static function createInstance(ContainerInterface $container, $entity_typ $entity_type, $entity_info, $container->get('entity.manager'), - $container->get('field.info'), $container->get('module_handler'), + $container->get('field.info'), $container->get('csrf_token') ); } @@ -67,17 +60,16 @@ public static function createInstance(ContainerInterface $container, $entity_typ * The entity information array. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager service. - * @param \Drupal\field\FieldInfo $field_info - * The field info service. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler service. + * @param \Drupal\field\FieldInfo $field_info + * The field info service. * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token * The CSRF token manager service. */ - public function __construct($entity_type, array $entity_info, EntityManagerInterface $entity_manager, FieldInfo $field_info, ModuleHandlerInterface $module_handler, CsrfTokenGenerator $csrf_token) { - parent::__construct($entity_type, $entity_info, $entity_manager); + public function __construct($entity_type, array $entity_info, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, FieldInfo $field_info, CsrfTokenGenerator $csrf_token) { + parent::__construct($entity_type, $entity_info, $entity_manager, $module_handler); $this->fieldInfo = $field_info; - $this->moduleHandler = $module_handler; $this->csrfToken = $csrf_token; } diff --git a/core/modules/edit/edit.services.yml b/core/modules/edit/edit.services.yml index 73f7f25..f7dd5ab 100644 --- a/core/modules/edit/edit.services.yml +++ b/core/modules/edit/edit.services.yml @@ -17,4 +17,4 @@ services: arguments: ['@plugin.manager.edit.editor', '@plugin.manager.field.formatter'] edit.metadata.generator: class: Drupal\edit\MetadataGenerator - arguments: ['@access_check.edit.entity_field', '@edit.editor.selector', '@plugin.manager.edit.editor'] + arguments: ['@entity.manager', '@access_check.edit.entity_field', '@edit.editor.selector', '@plugin.manager.edit.editor'] diff --git a/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php b/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php index 2c27409..aadcac3 100644 --- a/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php +++ b/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php @@ -7,17 +7,24 @@ namespace Drupal\edit; -use Drupal\Core\Entity\EntityInterface; use Drupal\Component\Plugin\PluginManagerInterface; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityManager; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\edit\Access\EditEntityFieldAccessCheckInterface; -use Drupal\field\FieldInstanceInterface; /** * Generates in-place editing metadata for an entity field. */ class MetadataGenerator implements MetadataGeneratorInterface { + /** + * The entity manager. + * + * @var \Drupal\Core\Entity\EntityManager + */ + protected $entityManager; + /** * An object that checks if a user has access to edit a given entity field. * @@ -42,6 +49,8 @@ class MetadataGenerator implements MetadataGeneratorInterface { /** * Constructs a new MetadataGenerator. * + * @param \Drupal\Core\Entity\EntityManager $entity_manager + * The entity manager. * @param \Drupal\edit\Access\EditEntityFieldAccessCheckInterface $access_checker * An object that checks if a user has access to edit a given field. * @param \Drupal\edit\EditorSelectorInterface $editor_selector @@ -49,7 +58,8 @@ class MetadataGenerator implements MetadataGeneratorInterface { * @param \Drupal\Component\Plugin\PluginManagerInterface * The manager for editor plugins. */ - public function __construct(EditEntityFieldAccessCheckInterface $access_checker, EditorSelectorInterface $editor_selector, PluginManagerInterface $editor_manager) { + public function __construct(EntityManager $entity_manager, EditEntityFieldAccessCheckInterface $access_checker, EditorSelectorInterface $editor_selector, PluginManagerInterface $editor_manager) { + $this->entityManager = $entity_manager; $this->accessChecker = $access_checker; $this->editorSelector = $editor_selector; $this->editorManager = $editor_manager; @@ -77,7 +87,8 @@ public function generateField(EntityInterface $entity, FieldDefinitionInterface } // Early-return if no editor is available. - $formatter_id = entity_get_render_display($entity, $view_mode)->getRenderer($field_name)->getPluginId(); + $view_builder = $this->entityManager->getViewBuilder($entity->entityType()); + $formatter_id = $view_builder->collectRenderDisplay($entity, $view_mode)->getRenderer($field_name)->getPluginId(); $items = $entity->getTranslation($langcode)->get($field_name)->getValue(); $editor_id = $this->editorSelector->getEditor($formatter_id, $field_definition, $items); if (!isset($editor_id)) { diff --git a/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php b/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php index 7cc16aa..999f0cd 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php @@ -60,7 +60,7 @@ public function setUp() { $this->editorManager = $this->container->get('plugin.manager.edit.editor'); $this->accessChecker = new MockEditEntityFieldAccessCheck(); $this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter')); - $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager); + $this->metadataGenerator = new MetadataGenerator($this->container->get('entity.manager'), $this->accessChecker, $this->editorSelector, $this->editorManager); } /** diff --git a/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php b/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php index 9081290..9edf4f2 100644 --- a/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php +++ b/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php @@ -150,7 +150,7 @@ public function testMetadata() { $this->editorManager = new InPlaceEditorManager($this->container->get('container.namespaces')); $this->accessChecker = new MockEditEntityFieldAccessCheck(); $this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter')); - $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager); + $this->metadataGenerator = new MetadataGenerator($this->container->get('entity.manager'), $this->accessChecker, $this->editorSelector, $this->editorManager); // Create an entity with values for the field. $this->entity = entity_create('entity_test', array()); diff --git a/core/modules/field/field.module b/core/modules/field/field.module index cef1cad..dda5667 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -430,7 +430,8 @@ function field_view_field(ContentEntityInterface $entity, $field_name, $display_ // Get the formatter object. if (is_string($display_options)) { $view_mode = $display_options; - $formatter = entity_get_render_display($entity, $view_mode)->getRenderer($field_name); + $view_builder = \Drupal::entityManager()->getViewBuilder($entity_type); + $formatter = $view_builder->collectRenderDisplay($entity, $view_mode)->getRenderer($field_name); } else { $view_mode = '_custom';