From e10d7ab7b6895c4b738b4bb0e7f6a0549eaa0437 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Mon, 18 Mar 2019 15:26:14 +0000 Subject: [PATCH] Issue #3038926 by Berdir: Add @trigger_error() to deprecated EntityManager->EntityDisplayRepository methods (cherry picked from commit 29e5277a14aebd94bbefd182be20f9a0bff4ade3) --- core/lib/Drupal/Core/Entity/EntityManager.php | 9 ++ .../CommentDefaultFormatter.php | 41 ++++-- .../comment/src/Plugin/views/row/Rss.php | 4 +- .../comment/src/Tests/CommentTestTrait.php | 5 +- .../Plugin/Derivative/FieldUiLocalTask.php | 59 ++++++--- .../tests/src/Kernel/EntityDisplayTest.php | 8 +- core/modules/node/node.module | 2 +- .../modules/node/src/Plugin/views/row/Rss.php | 15 ++- .../src/Plugin/views/row/RssPluginBase.php | 44 +++++-- .../Tests/Core/Entity/EntityManagerTest.php | 118 ++++++++++++++++++ 10 files changed, 249 insertions(+), 56 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index e46d9f9f489e..91a904da5e65 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -445,6 +445,7 @@ public function getCanonicalMultiple($entity_type_id, array $entity_ids, array $ * @see https://www.drupal.org/node/2549139 */ public function getAllViewModes() { + @trigger_error('EntityManagerInterface::getAllViewModes() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getAllViewModes() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_display.repository')->getAllViewModes(); } @@ -458,6 +459,7 @@ public function getAllViewModes() { * @see https://www.drupal.org/node/2549139 */ public function getViewModes($entity_type_id) { + @trigger_error('EntityManagerInterface::getViewModes() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewModes() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_display.repository')->getViewModes($entity_type_id); } @@ -471,6 +473,7 @@ public function getViewModes($entity_type_id) { * @see https://www.drupal.org/node/2549139 */ public function getAllFormModes() { + @trigger_error('EntityManagerInterface::getAllFormModes() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getAllFormModes() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_display.repository')->getAllFormModes(); } @@ -484,6 +487,7 @@ public function getAllFormModes() { * @see https://www.drupal.org/node/2549139 */ public function getFormModes($entity_type_id) { + @trigger_error('EntityManagerInterface::getFormModes() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getFormModes() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_display.repository')->getFormModes($entity_type_id); } @@ -497,6 +501,7 @@ public function getFormModes($entity_type_id) { * @see https://www.drupal.org/node/2549139 */ public function getViewModeOptions($entity_type_id) { + @trigger_error('EntityManagerInterface::getViewModeOptions() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewModeOptions() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_display.repository')->getViewModeOptions($entity_type_id); } @@ -510,6 +515,7 @@ public function getViewModeOptions($entity_type_id) { * @see https://www.drupal.org/node/2549139 */ public function getFormModeOptions($entity_type_id) { + @trigger_error('EntityManagerInterface::getFormModeOptions() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getFormModeOptions() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_display.repository')->getFormModeOptions($entity_type_id); } @@ -523,6 +529,7 @@ public function getFormModeOptions($entity_type_id) { * @see https://www.drupal.org/node/2549139 */ public function getViewModeOptionsByBundle($entity_type_id, $bundle) { + @trigger_error('EntityManagerInterface::getViewModeOptionsByBundle() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewModeOptionsByBundle() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_display.repository')->getViewModeOptionsByBundle($entity_type_id, $bundle); } @@ -536,6 +543,7 @@ public function getViewModeOptionsByBundle($entity_type_id, $bundle) { * @see https://www.drupal.org/node/2549139 */ public function getFormModeOptionsByBundle($entity_type_id, $bundle) { + @trigger_error('EntityManagerInterface::getFormModeOptionsByBundle() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getFormModeOptionsByBundle() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_display.repository')->getFormModeOptionsByBundle($entity_type_id, $bundle); } @@ -549,6 +557,7 @@ public function getFormModeOptionsByBundle($entity_type_id, $bundle) { * @see https://www.drupal.org/node/2549139 */ public function clearDisplayModeInfo() { + @trigger_error('EntityManagerInterface::clearDisplayModeInfo() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::clearDisplayModeInfo() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); $this->container->get('entity_display.repository')->clearDisplayModeInfo(); } diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index 8adf25200071..3dbc63c4775a 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -3,9 +3,11 @@ namespace Drupal\comment\Plugin\Field\FieldFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; +use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; use Drupal\Core\Entity\Entity\EntityViewDisplay; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityDisplayRepositoryInterface; use Drupal\Core\Entity\EntityFormBuilderInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; @@ -31,6 +33,12 @@ * ) */ class CommentDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface { + use DeprecatedServicePropertyTrait; + + /** + * {@inheritdoc} + */ + protected $deprecatedProperties = ['entityManager' => 'entity.manager']; /** * {@inheritdoc} @@ -64,11 +72,11 @@ public static function defaultSettings() { protected $viewBuilder; /** - * The entity manager. + * The entity display repository. * - * @var \Drupal\Core\Entity\EntityManagerInterface + * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface */ - protected $entityManager; + protected $entityDisplayRepository; /** * The entity form builder. @@ -95,9 +103,10 @@ public static function create(ContainerInterface $container, array $configuratio $configuration['view_mode'], $configuration['third_party_settings'], $container->get('current_user'), - $container->get('entity.manager'), + $container->get('entity_type.manager'), $container->get('entity.form_builder'), - $container->get('current_route_match') + $container->get('current_route_match'), + $container->get('entity_display.repository') ); } @@ -120,21 +129,27 @@ public static function create(ContainerInterface $container, array $configuratio * Third party settings. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder * The entity form builder. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match object. + * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository + * The entity display repository. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityManagerInterface $entity_manager, EntityFormBuilderInterface $entity_form_builder, RouteMatchInterface $route_match) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, EntityFormBuilderInterface $entity_form_builder, RouteMatchInterface $route_match, EntityDisplayRepositoryInterface $entity_display_repository = NULL) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); - $this->viewBuilder = $entity_manager->getViewBuilder('comment'); - $this->storage = $entity_manager->getStorage('comment'); + $this->viewBuilder = $entity_type_manager->getViewBuilder('comment'); + $this->storage = $entity_type_manager->getStorage('comment'); $this->currentUser = $current_user; - $this->entityManager = $entity_manager; $this->entityFormBuilder = $entity_form_builder; $this->routeMatch = $route_match; + if (!$entity_display_repository) { + @trigger_error('Calling RssPluginBase::__construct() with the $entity_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); + $entity_display_repository = \Drupal::service('entity_display.repository'); + } + $this->entityDisplayRepository = $entity_display_repository; } /** @@ -280,7 +295,7 @@ public function calculateDependencies() { * as value. */ protected function getViewModes() { - return $this->entityManager->getViewModeOptionsByBundle('comment', $this->getFieldSetting('comment_type')); + return $this->entityDisplayRepository->getViewModeOptionsByBundle('comment', $this->getFieldSetting('comment_type')); } } diff --git a/core/modules/comment/src/Plugin/views/row/Rss.php b/core/modules/comment/src/Plugin/views/row/Rss.php index 023e45df8448..1447c3ff522b 100644 --- a/core/modules/comment/src/Plugin/views/row/Rss.php +++ b/core/modules/comment/src/Plugin/views/row/Rss.php @@ -46,7 +46,7 @@ public function preRender($result) { $cids[] = $row->cid; } - $this->comments = $this->entityManager->getStorage('comment')->loadMultiple($cids); + $this->comments = $this->entityTypeManager->getStorage('comment')->loadMultiple($cids); } /** @@ -99,7 +99,7 @@ public function render($row) { // The comment gets built and modules add to or modify // $comment->rss_elements and $comment->rss_namespaces. - $build = $this->entityManager->getViewBuilder('comment')->view($comment, 'rss'); + $build = $this->entityTypeManager->getViewBuilder('comment')->view($comment, 'rss'); unset($build['#theme']); if (!empty($comment->rss_namespaces)) { diff --git a/core/modules/comment/src/Tests/CommentTestTrait.php b/core/modules/comment/src/Tests/CommentTestTrait.php index 038a3df0910f..0c0d65de160b 100644 --- a/core/modules/comment/src/Tests/CommentTestTrait.php +++ b/core/modules/comment/src/Tests/CommentTestTrait.php @@ -35,6 +35,7 @@ trait CommentTestTrait { */ public function addDefaultCommentField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment', $comment_view_mode = 'full') { $entity_manager = \Drupal::entityManager(); + $entity_display_repository = \Drupal::service('entity_display.repository'); // Create the comment type if needed. $comment_type_storage = $entity_manager->getStorage('comment_type'); if ($comment_type = $comment_type_storage->load($comment_type_id)) { @@ -94,7 +95,7 @@ public function addDefaultCommentField($entity_type, $bundle, $field_name = 'com 'weight' => 20, ]) ->save(); - foreach ($entity_manager->getFormModes($entity_type) as $id => $form_mode) { + foreach ($entity_display_repository->getFormModes($entity_type) as $id => $form_mode) { $display = entity_get_form_display($entity_type, $bundle, $id); // Only update existing displays. if ($display && !$display->isNew()) { @@ -112,7 +113,7 @@ public function addDefaultCommentField($entity_type, $bundle, $field_name = 'com 'settings' => ['view_mode' => $comment_view_mode], ]) ->save(); - foreach ($entity_manager->getViewModes($entity_type) as $id => $view_mode) { + foreach ($entity_display_repository->getViewModes($entity_type) as $id => $view_mode) { $display = entity_get_display($entity_type, $bundle, $id); // Only update existing displays. if ($display && !$display->isNew()) { diff --git a/core/modules/field_ui/src/Plugin/Derivative/FieldUiLocalTask.php b/core/modules/field_ui/src/Plugin/Derivative/FieldUiLocalTask.php index 6d95abdd1a86..515682b78687 100644 --- a/core/modules/field_ui/src/Plugin/Derivative/FieldUiLocalTask.php +++ b/core/modules/field_ui/src/Plugin/Derivative/FieldUiLocalTask.php @@ -2,7 +2,9 @@ namespace Drupal\field_ui\Plugin\Derivative; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; +use Drupal\Core\Entity\EntityDisplayRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Component\Plugin\Derivative\DeriverBase; use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; use Drupal\Core\Routing\RouteProviderInterface; @@ -15,6 +17,12 @@ */ class FieldUiLocalTask extends DeriverBase implements ContainerDeriverInterface { use StringTranslationTrait; + use DeprecatedServicePropertyTrait; + + /** + * {@inheritdoc} + */ + protected $deprecatedProperties = ['entityManager' => 'entity.manager']; /** * The route provider. @@ -24,26 +32,40 @@ class FieldUiLocalTask extends DeriverBase implements ContainerDeriverInterface protected $routeProvider; /** - * The entity manager + * The entity type manager. * - * @var \Drupal\Core\Entity\EntityManagerInterface + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityManager; + protected $entityTypeManager; + + /** + * The entity display repository. + * + * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface + */ + protected $entityDisplayRepository; /** * Creates an FieldUiLocalTask object. * * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider * The route provider. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager. + * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository + * The entity display repository. */ - public function __construct(RouteProviderInterface $route_provider, EntityManagerInterface $entity_manager, TranslationInterface $string_translation) { + public function __construct(RouteProviderInterface $route_provider, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, EntityDisplayRepositoryInterface $entity_display_repository = NULL) { $this->routeProvider = $route_provider; - $this->entityManager = $entity_manager; + $this->entityTypeManager = $entity_type_manager; $this->stringTranslation = $string_translation; + if (!$entity_display_repository) { + @trigger_error('Calling FieldUiLocalTask::__construct() with the $entity_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); + $entity_display_repository = \Drupal::service('entity_display.repository'); + } + $this->entityDisplayRepository = $entity_display_repository; } /** @@ -52,8 +74,9 @@ public function __construct(RouteProviderInterface $route_provider, EntityManage public static function create(ContainerInterface $container, $base_plugin_id) { return new static( $container->get('router.route_provider'), - $container->get('entity.manager'), - $container->get('string_translation') + $container->get('entity_type.manager'), + $container->get('string_translation'), + $container->get('entity_display.repository') ); } @@ -63,7 +86,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) { public function getDerivativeDefinitions($base_plugin_definition) { $this->derivatives = []; - foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) { + foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { if ($entity_type->get('field_ui_base_route')) { $this->derivatives["overview_$entity_type_id"] = [ 'route_name' => "entity.$entity_type_id.field_ui_fields", @@ -124,7 +147,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { // One local task for each form mode. $weight = 0; - foreach ($this->entityManager->getFormModes($entity_type_id) as $form_mode => $form_mode_info) { + foreach ($this->entityDisplayRepository->getFormModes($entity_type_id) as $form_mode => $form_mode_info) { $this->derivatives['field_form_display_' . $form_mode . '_' . $entity_type_id] = [ 'title' => $form_mode_info['label'], 'route_name' => "entity.entity_form_display.$entity_type_id.form_mode", @@ -133,13 +156,13 @@ public function getDerivativeDefinitions($base_plugin_definition) { ], 'parent_id' => "field_ui.fields:form_display_overview_$entity_type_id", 'weight' => $weight++, - 'cache_tags' => $this->entityManager->getDefinition('entity_form_display')->getListCacheTags(), + 'cache_tags' => $this->entityTypeManager->getDefinition('entity_form_display')->getListCacheTags(), ]; } // One local task for each view mode. $weight = 0; - foreach ($this->entityManager->getViewModes($entity_type_id) as $view_mode => $form_mode_info) { + foreach ($this->entityDisplayRepository->getViewModes($entity_type_id) as $view_mode => $form_mode_info) { $this->derivatives['field_display_' . $view_mode . '_' . $entity_type_id] = [ 'title' => $form_mode_info['label'], 'route_name' => "entity.entity_view_display.$entity_type_id.view_mode", @@ -148,7 +171,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { ], 'parent_id' => "field_ui.fields:display_overview_$entity_type_id", 'weight' => $weight++, - 'cache_tags' => $this->entityManager->getDefinition('entity_view_display')->getListCacheTags(), + 'cache_tags' => $this->entityTypeManager->getDefinition('entity_view_display')->getListCacheTags(), ]; } } @@ -168,7 +191,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { * An array of local tasks plugin definitions, keyed by plugin ID. */ public function alterLocalTasks(&$local_tasks) { - foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) { + foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { if ($route_name = $entity_type->get('field_ui_base_route')) { $local_tasks["field_ui.fields:overview_$entity_type_id"]['base_route'] = $route_name; $local_tasks["field_ui.fields:form_display_overview_$entity_type_id"]['base_route'] = $route_name; @@ -176,11 +199,11 @@ public function alterLocalTasks(&$local_tasks) { $local_tasks["field_ui.fields:field_form_display_default_$entity_type_id"]['base_route'] = $route_name; $local_tasks["field_ui.fields:field_display_default_$entity_type_id"]['base_route'] = $route_name; - foreach ($this->entityManager->getFormModes($entity_type_id) as $form_mode => $form_mode_info) { + foreach ($this->entityDisplayRepository->getFormModes($entity_type_id) as $form_mode => $form_mode_info) { $local_tasks['field_ui.fields:field_form_display_' . $form_mode . '_' . $entity_type_id]['base_route'] = $route_name; } - foreach ($this->entityManager->getViewModes($entity_type_id) as $view_mode => $form_mode_info) { + foreach ($this->entityDisplayRepository->getViewModes($entity_type_id) as $view_mode => $form_mode_info) { $local_tasks['field_ui.fields:field_display_' . $view_mode . '_' . $entity_type_id]['base_route'] = $route_name; } } diff --git a/core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php b/core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php index 5f0b5f1f1b17..10f391d3e6d1 100644 --- a/core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php +++ b/core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php @@ -497,17 +497,17 @@ public function testGetDisplayModeOptions() { $form_display_teaser->save(); // Test getViewModeOptionsByBundle(). - $view_modes = \Drupal::entityManager()->getViewModeOptionsByBundle('node', 'article'); + $view_modes = \Drupal::service('entity_display.repository')->getViewModeOptionsByBundle('node', 'article'); $this->assertEqual($view_modes, ['default' => 'Default']); $display_teaser->setStatus(TRUE)->save(); - $view_modes = \Drupal::entityManager()->getViewModeOptionsByBundle('node', 'article'); + $view_modes = \Drupal::service('entity_display.repository')->getViewModeOptionsByBundle('node', 'article'); $this->assertEqual($view_modes, ['default' => 'Default', 'teaser' => 'Teaser']); // Test getFormModeOptionsByBundle(). - $form_modes = \Drupal::entityManager()->getFormModeOptionsByBundle('user', 'user'); + $form_modes = \Drupal::service('entity_display.repository')->getFormModeOptionsByBundle('user', 'user'); $this->assertEqual($form_modes, ['default' => 'Default']); $form_display_teaser->setStatus(TRUE)->save(); - $form_modes = \Drupal::entityManager()->getFormModeOptionsByBundle('user', 'user'); + $form_modes = \Drupal::service('entity_display.repository')->getFormModeOptionsByBundle('user', 'user'); $this->assertEqual($form_modes, ['default' => 'Default', 'register' => 'Register']); } diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 58f79f7ec48b..aa242e6f0c4a 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -384,7 +384,7 @@ function node_add_body_field(NodeTypeInterface $type, $label = 'Body') { // The teaser view mode is created by the Standard profile and therefore // might not exist. - $view_modes = \Drupal::entityManager()->getViewModes('node'); + $view_modes = \Drupal::service('entity_display.repository')->getViewModes('node'); if (isset($view_modes['teaser'])) { entity_get_display('node', $type->id(), 'teaser') ->setComponent('body', [ diff --git a/core/modules/node/src/Plugin/views/row/Rss.php b/core/modules/node/src/Plugin/views/row/Rss.php index 59a1df012bd6..b98b98feefd9 100644 --- a/core/modules/node/src/Plugin/views/row/Rss.php +++ b/core/modules/node/src/Plugin/views/row/Rss.php @@ -2,7 +2,8 @@ namespace Drupal\node\Plugin\views\row; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityDisplayRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\views\Plugin\views\row\RssPluginBase; /** @@ -50,12 +51,14 @@ class Rss extends RssPluginBase { * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository + * The entity display repository. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager); - $this->nodeStorage = $entity_manager->getStorage('node'); + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository = NULL) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_display_repository); + $this->nodeStorage = $entity_type_manager->getStorage('node'); } /** diff --git a/core/modules/views/src/Plugin/views/row/RssPluginBase.php b/core/modules/views/src/Plugin/views/row/RssPluginBase.php index b8c518bc9e51..4106147574b3 100644 --- a/core/modules/views/src/Plugin/views/row/RssPluginBase.php +++ b/core/modules/views/src/Plugin/views/row/RssPluginBase.php @@ -2,7 +2,9 @@ namespace Drupal\views\Plugin\views\row; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; +use Drupal\Core\Entity\EntityDisplayRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -11,12 +13,26 @@ */ abstract class RssPluginBase extends RowPluginBase { + use DeprecatedServicePropertyTrait; + + /** + * {@inheritdoc} + */ + protected $deprecatedProperties = ['entityManager' => 'entity.manager']; + /** * The entity manager. * - * @var \Drupal\Core\Entity\EntityManagerInterface + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityManager; + protected $entityTypeManager; + + /** + * The entity display repository. + * + * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface + */ + protected $entityDisplayRepository; /** * Constructs a RssPluginBase object. @@ -27,13 +43,20 @@ abstract class RssPluginBase extends RowPluginBase { * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository + * The entity display repository. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository = NULL) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->entityManager = $entity_manager; + $this->entityTypeManager = $entity_type_manager; + if (!$entity_display_repository) { + @trigger_error('Calling RssPluginBase::__construct() with the $entity_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); + $entity_display_repository = \Drupal::service('entity_display.repository'); + } + $this->entityDisplayRepository = $entity_display_repository; } /** @@ -44,7 +67,8 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('entity.manager') + $container->get('entity_type.manager'), + $container->get('entity_display.repository') ); } @@ -84,7 +108,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { * Return the main options, which are shown in the summary title. */ public function buildOptionsForm_summary_options() { - $view_modes = $this->entityManager->getViewModes($this->entityTypeId); + $view_modes = $this->entityDisplayRepository->getViewModes($this->entityTypeId); $options = []; foreach ($view_modes as $mode => $settings) { $options[$mode] = $settings['label']; @@ -98,7 +122,7 @@ public function buildOptionsForm_summary_options() { public function calculateDependencies() { $dependencies = parent::calculateDependencies(); - $view_mode = $this->entityManager + $view_mode = $this->entityTypeManager ->getStorage('entity_view_mode') ->load($this->entityTypeId . '.' . $this->options['view_mode']); if ($view_mode) { diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php index 81f4c0e8cefb..0b1a3329afc7 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\Core\Entity; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Entity\EntityDisplayRepositoryInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface; @@ -42,6 +43,13 @@ class EntityManagerTest extends UnitTestCase { */ protected $entityTypeRepository; + /** + * The entity display repository. + * + * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface|\Prophecy\Prophecy\ProphecyInterface + */ + protected $entityDisplayRepository; + /** * The entity type bundle info. * @@ -81,6 +89,7 @@ protected function setUp() { $this->entityTypeBundleInfo = $this->prophesize(EntityTypeBundleInfoInterface::class); $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class); $this->entityRepository = $this->prophesize(EntityRepositoryInterface::class); + $this->entityDisplayRepository = $this->prophesize(EntityDisplayRepositoryInterface::class); $this->entityLastInstalledSchemaRepository = $this->prophesize(EntityLastInstalledSchemaRepositoryInterface::class); $container = new ContainerBuilder(); @@ -89,6 +98,7 @@ protected function setUp() { $container->set('entity_type.bundle.info', $this->entityTypeBundleInfo->reveal()); $container->set('entity_field.manager', $this->entityFieldManager->reveal()); $container->set('entity.repository', $this->entityRepository->reveal()); + $container->set('entity_display.repository', $this->entityDisplayRepository->reveal()); $container->set('entity.last_installed_schema.repository', $this->entityLastInstalledSchemaRepository->reveal()); $this->entityManager = new EntityManager(); @@ -234,6 +244,114 @@ public function testGetLastInstalledFieldStorageDefinitions() { $this->assertEquals([], $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type_id)); } + /** + * Tests the getAllViewModes() method. + * + * @covers ::getAllViewModes + * + * @expectedDeprecation EntityManagerInterface::getAllViewModes() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getAllViewModes() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetAllViewModes() { + $this->entityDisplayRepository->getAllViewModes()->shouldBeCalled()->willReturn([]); + $this->assertEquals([], $this->entityManager->getAllViewModes()); + } + + /** + * Tests the getViewModes() method. + * + * @covers ::getViewModes + * + * @expectedDeprecation EntityManagerInterface::getViewModes() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewModes() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetViewModes() { + $this->entityDisplayRepository->getViewModes('entity_type')->shouldBeCalled()->willReturn([]); + $this->assertEquals([], $this->entityManager->getViewModes('entity_type')); + } + + /** + * Tests the getViewModeOptions() method. + * + * @covers ::getViewModeOptions + * + * @expectedDeprecation EntityManagerInterface::getViewModeOptions() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewModeOptions() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetViewModeOptions() { + $this->entityDisplayRepository->getViewModeOptions('entity_type')->shouldBeCalled()->willReturn([]); + $this->assertEquals([], $this->entityManager->getViewModeOptions('entity_type')); + } + + /** + * Tests the getViewModeOptionsByBundle() method. + * + * @covers ::getViewModeOptionsByBundle + * + * @expectedDeprecation EntityManagerInterface::getViewModeOptionsByBundle() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewModeOptionsByBundle() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetViewModeOptionsByBundle() { + $this->entityDisplayRepository->getViewModeOptionsByBundle('entity_type', 'bundle')->shouldBeCalled()->willReturn([]); + $this->assertEquals([], $this->entityManager->getViewModeOptionsByBundle('entity_type', 'bundle')); + } + + /** + * Tests the getAllFormModes() method. + * + * @covers ::getAllFormModes + * + * @expectedDeprecation EntityManagerInterface::getAllFormModes() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getAllFormModes() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetAllFormModes() { + $this->entityDisplayRepository->getAllFormModes()->shouldBeCalled()->willReturn([]); + $this->assertEquals([], $this->entityManager->getAllFormModes()); + } + + /** + * Tests the getFormModes() method. + * + * @covers ::getFormModes + * + * @expectedDeprecation EntityManagerInterface::getFormModes() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getFormModes() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetFormModes() { + $this->entityDisplayRepository->getFormModes('entity_type')->shouldBeCalled()->willReturn([]); + $this->assertEquals([], $this->entityManager->getFormModes('entity_type')); + } + + /** + * Tests the getFormModeOptions() method. + * + * @covers ::getFormModeOptions + * + * @expectedDeprecation EntityManagerInterface::getFormModeOptions() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getFormModeOptions() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetFormModeOptions() { + $this->entityDisplayRepository->getFormModeOptions('entity_type')->shouldBeCalled()->willReturn([]); + $this->assertEquals([], $this->entityManager->getFormModeOptions('entity_type')); + } + + /** + * Tests the getFormModeOptionsByBundle() method. + * + * @covers ::getFormModeOptionsByBundle + * + * @expectedDeprecation EntityManagerInterface::getFormModeOptionsByBundle() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getFormModeOptionsByBundle() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetFormModeOptionsByBundle() { + $this->entityDisplayRepository->getFormModeOptionsByBundle('entity_type', 'bundle')->shouldBeCalled()->willReturn([]); + $this->assertEquals([], $this->entityManager->getFormModeOptionsByBundle('entity_type', 'bundle')); + } + + /** + * Tests the clearDisplayModeInfo() method. + * + * @covers ::clearDisplayModeInfo + * + * @expectedDeprecation EntityManagerInterface::clearDisplayModeInfo() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDisplayRepositoryInterface::clearDisplayModeInfo() instead. See https://www.drupal.org/node/2549139. + */ + public function testClearDisplayModeInfo() { + $this->entityDisplayRepository->clearDisplayModeInfo()->shouldBeCalled()->willReturn([]); + $this->entityManager->clearDisplayModeInfo(); + } + /** * Tests the useCaches() method. * -- GitLab