Issue #2691675 by Berdir, Mile23, alexpott: Replace deprecated entityManager() in ControllerBase descendents

8.7.x
Alex Pott 2019-01-08 09:35:04 +00:00
parent 2a8e1e0a3c
commit c23c05c90f
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
26 changed files with 267 additions and 95 deletions

View File

@ -129,6 +129,7 @@ abstract class ControllerBase implements ContainerInjectionInterface {
* instead.
*/
protected function entityManager() {
@trigger_error('ControllerBase::getEntityManager() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use ::getEntityTypeManager() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
if (!$this->entityManager) {
$this->entityManager = $this->container()->get('entity.manager');
}

View File

@ -20,7 +20,7 @@ class EntityListController extends ControllerBase {
* \Drupal\Core\Render\RendererInterface::render().
*/
public function listing($entity_type) {
return $this->entityManager()->getListBuilder($entity_type)->render();
return $this->entityTypeManager()->getListBuilder($entity_type)->render();
}
}

View File

@ -3,9 +3,10 @@
namespace Drupal\Core\Entity\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -13,13 +14,19 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* Defines a generic controller to render a single entity.
*/
class EntityViewController implements ContainerInjectionInterface {
use DeprecatedServicePropertyTrait;
/**
* The entity manager
* {@inheritdoc}
*/
protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
protected $entityTypeManager;
/**
* The renderer service.
@ -31,13 +38,13 @@ class EntityViewController implements ContainerInjectionInterface {
/**
* Creates an EntityViewController object.
*
* @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\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer) {
$this->entityManager = $entity_manager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) {
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
}
@ -46,7 +53,7 @@ class EntityViewController implements ContainerInjectionInterface {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('entity_type.manager'),
$container->get('renderer')
);
}
@ -92,7 +99,7 @@ class EntityViewController implements ContainerInjectionInterface {
* \Drupal\Core\Render\RendererInterface::render().
*/
public function view(EntityInterface $_entity, $view_mode = 'full') {
$page = $this->entityManager
$page = $this->entityTypeManager
->getViewBuilder($_entity->getEntityTypeId())
->view($_entity, $view_mode);

View File

@ -48,7 +48,7 @@ class AggregatorController extends ControllerBase {
* \Drupal\Core\Render\RendererInterface::render().
*/
public function feedAdd() {
$feed = $this->entityManager()->getStorage('aggregator_feed')->create();
$feed = $this->entityTypeManager()->getStorage('aggregator_feed')->create();
return $this->entityFormBuilder()->getForm($feed);
}
@ -71,7 +71,7 @@ class AggregatorController extends ControllerBase {
];
$build['feed_source'] = is_array($feed_source) ? $feed_source : ['#markup' => $feed_source];
if ($items) {
$build['items'] = $this->entityManager()->getViewBuilder('aggregator_item')
$build['items'] = $this->entityTypeManager()->getViewBuilder('aggregator_item')
->viewMultiple($items, 'default');
$build['pager'] = ['#type' => 'pager'];
}
@ -106,8 +106,8 @@ class AggregatorController extends ControllerBase {
* \Drupal\Core\Render\RendererInterface::render().
*/
public function adminOverview() {
$entity_manager = $this->entityManager();
$feeds = $entity_manager->getStorage('aggregator_feed')
$entity_type_manager = $this->entityTypeManager();
$feeds = $entity_type_manager->getStorage('aggregator_feed')
->loadMultiple();
$header = [$this->t('Title'), $this->t('Items'), $this->t('Last update'), $this->t('Next update'), $this->t('Operations')];
@ -116,7 +116,7 @@ class AggregatorController extends ControllerBase {
foreach ($feeds as $feed) {
$row = [];
$row[] = $feed->toLink()->toString();
$row[] = $this->formatPlural($entity_manager->getStorage('aggregator_item')->getItemCount($feed), '1 item', '@count items');
$row[] = $this->formatPlural($entity_type_manager->getStorage('aggregator_item')->getItemCount($feed), '1 item', '@count items');
$last_checked = $feed->getLastCheckedTime();
$refresh_rate = $feed->getRefreshRate();
@ -173,7 +173,7 @@ class AggregatorController extends ControllerBase {
* The rendered list of items for the feed.
*/
public function pageLast() {
$items = $this->entityManager()->getStorage('aggregator_item')->loadAll(20);
$items = $this->entityTypeManager()->getStorage('aggregator_item')->loadAll(20);
$build = $this->buildPageList($items);
$build['#attached']['feed'][] = ['aggregator/rss', $this->config('system.site')->get('name') . ' ' . $this->t('aggregator')];
return $build;

View File

@ -22,7 +22,7 @@ class BlockAddController extends ControllerBase {
*/
public function blockAddConfigureForm($plugin_id, $theme) {
// Create a block entity.
$entity = $this->entityManager()->getStorage('block')->create(['plugin' => $plugin_id, 'theme' => $theme]);
$entity = $this->entityTypeManager()->getStorage('block')->create(['plugin' => $plugin_id, 'theme' => $theme]);
return $this->entityFormBuilder()->getForm($entity);
}

View File

@ -57,7 +57,7 @@ class BlockListController extends EntityListController {
throw new NotFoundHttpException();
}
return $this->entityManager()->getListBuilder('block')->render($theme, $request);
return $this->entityTypeManager()->getListBuilder('block')->render($theme, $request);
}
}

View File

@ -122,7 +122,7 @@ class BookController extends ControllerBase {
'#theme' => 'item_list',
'#items' => $book_list,
'#cache' => [
'tags' => \Drupal::entityManager()->getDefinition('node')->getListCacheTags(),
'tags' => $this->entityTypeManager()->getDefinition('node')->getListCacheTags(),
],
];
}

View File

@ -8,8 +8,10 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
@ -40,11 +42,18 @@ class CommentController extends ControllerBase {
protected $commentManager;
/**
* The entity manager.
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityManager;
protected $entityFieldManager;
/**
* The entity repository.
*
* @var Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a CommentController object.
@ -53,13 +62,27 @@ class CommentController extends ControllerBase {
* HTTP kernel to handle requests.
* @param \Drupal\comment\CommentManagerInterface $comment_manager
* The comment manager service.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
*/
public function __construct(HttpKernelInterface $http_kernel, CommentManagerInterface $comment_manager, EntityManagerInterface $entity_manager) {
public function __construct(HttpKernelInterface $http_kernel, CommentManagerInterface $comment_manager, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager = NULL, EntityRepositoryInterface $entity_repository = NULL) {
$this->httpKernel = $http_kernel;
$this->commentManager = $comment_manager;
$this->entityManager = $entity_manager;
$this->entityTypeManager = $entity_type_manager;
if (!$entity_field_manager) {
@trigger_error('The entity_field.manager service must be passed to CommentController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_field_manager = \Drupal::service('entity_field.manager');
}
$this->entityFieldManager = $entity_field_manager;
if (!$entity_repository) {
@trigger_error('The entity.repository service must be passed to CommentController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$this->entityRepository = $entity_repository;
}
/**
@ -69,7 +92,9 @@ class CommentController extends ControllerBase {
return new static(
$container->get('http_kernel'),
$container->get('comment.manager'),
$container->get('entity.manager')
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('entity.repository')
);
}
@ -119,10 +144,10 @@ class CommentController extends ControllerBase {
if (!$entity->access('view')) {
throw new AccessDeniedHttpException();
}
$field_definition = $this->entityManager()->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];
$field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];
// Find the current display page for this comment.
$page = $this->entityManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page'));
$page = $this->entityTypeManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page'));
// @todo: Cleaner sub request handling.
$subrequest_url = $entity->toUrl()->setOption('query', ['page' => $page])->toString(TRUE);
$redirect_request = Request::create($subrequest_url->getGeneratedUrl(), 'GET', $request->query->all(), $request->cookies->all(), [], $request->server->all());
@ -155,7 +180,7 @@ class CommentController extends ControllerBase {
* The translated comment subject.
*/
public function commentPermalinkTitle(CommentInterface $comment) {
return $this->entityManager()->getTranslationFromContext($comment)->label();
return $this->entityRepository->getTranslationFromContext($comment)->label();
}
/**
@ -219,9 +244,9 @@ class CommentController extends ControllerBase {
// $pid indicates that this is a reply to a comment.
if ($pid) {
// Load the parent comment.
$comment = $this->entityManager()->getStorage('comment')->load($pid);
$comment = $this->entityTypeManager()->getStorage('comment')->load($pid);
// Display the parent comment.
$build['comment_parent'] = $this->entityManager()->getViewBuilder('comment')->view($comment);
$build['comment_parent'] = $this->entityTypeManager()->getViewBuilder('comment')->view($comment);
}
// The comment is in response to a entity.
@ -231,7 +256,7 @@ class CommentController extends ControllerBase {
$entity = clone $entity;
$entity->{$field_name}->status = CommentItemInterface::HIDDEN;
// Render array of the entity full view mode.
$build['commented_entity'] = $this->entityManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'full');
$build['commented_entity'] = $this->entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'full');
unset($build['commented_entity']['#cache']);
}
}
@ -240,7 +265,7 @@ class CommentController extends ControllerBase {
}
// Show the actual reply box.
$comment = $this->entityManager()->getStorage('comment')->create([
$comment = $this->entityTypeManager()->getStorage('comment')->create([
'entity_id' => $entity->id(),
'pid' => $pid,
'entity_type' => $entity->getEntityTypeId(),
@ -292,7 +317,7 @@ class CommentController extends ControllerBase {
$access = $access->andIf(AccessResult::allowedIfHasPermission($account, 'access comments'));
// Load the parent comment.
$comment = $this->entityManager()->getStorage('comment')->load($pid);
$comment = $this->entityTypeManager()->getStorage('comment')->load($pid);
// Check if the parent comment is published and belongs to the entity.
$access = $access->andIf(AccessResult::allowedIf($comment && $comment->isPublished() && $comment->getCommentedEntityId() == $entity->id()));
if ($comment) {
@ -329,9 +354,9 @@ class CommentController extends ControllerBase {
$links = [];
foreach ($nids as $nid) {
$node = $this->entityManager->getStorage('node')->load($nid);
$node = $this->entityTypeManager()->getStorage('node')->load($nid);
$new = $this->commentManager->getCountNewComments($node);
$page_number = $this->entityManager()->getStorage('comment')
$page_number = $this->entityTypeManager()->getStorage('comment')
->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name);
$query = $page_number ? ['page' => $page_number] : NULL;
$links[$nid] = [

View File

@ -63,7 +63,7 @@ class ConfigTranslationListController extends ControllerBase {
// controller defined, use it. Other mappers, for examples the ones for
// node_type and block, fallback to the generic configuration translation
// list controller.
$build = $this->entityManager()
$build = $this->entityTypeManager()
->getHandler($entity_type, 'config_translation_list')
->setMapperDefinition($mapper_definition)
->render();

View File

@ -59,7 +59,7 @@ class ContactController extends ControllerBase {
// Use the default form if no form has been passed.
if (empty($contact_form)) {
$contact_form = $this->entityManager()
$contact_form = $this->entityTypeManager()
->getStorage('contact_form')
->load($config->get('default_form'));
// If there are no forms, do not display the form.
@ -76,7 +76,7 @@ class ContactController extends ControllerBase {
}
}
$message = $this->entityManager()
$message = $this->entityTypeManager()
->getStorage('contact_message')
->create([
'contact_form' => $contact_form->id(),
@ -109,7 +109,7 @@ class ContactController extends ControllerBase {
throw new NotFoundHttpException();
}
$message = $this->entityManager()->getStorage('contact_message')->create([
$message = $this->entityTypeManager()->getStorage('contact_message')->create([
'contact_form' => 'personal',
'recipient' => $user->id(),
]);

View File

@ -7,6 +7,7 @@ use Drupal\content_translation\ContentTranslationManagerInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
@ -24,21 +25,38 @@ class ContentTranslationController extends ControllerBase {
*/
protected $manager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* Initializes a content translation controller.
*
* @param \Drupal\content_translation\ContentTranslationManagerInterface $manager
* A content translation manager instance.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
*/
public function __construct(ContentTranslationManagerInterface $manager) {
public function __construct(ContentTranslationManagerInterface $manager, EntityFieldManagerInterface $entity_field_manager = NULL) {
$this->manager = $manager;
if (!$entity_field_manager) {
@trigger_error('The entity_field.manager service must be passed to ContentTranslationController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_field_manager = \Drupal::service('entity_field.manager');
}
$this->entityFieldManager = $entity_field_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('content_translation.manager'));
return new static(
$container->get('content_translation.manager'),
$container->get('entity_field.manager')
);
}
/**
@ -62,7 +80,7 @@ class ContentTranslationController extends ControllerBase {
}
/** @var \Drupal\user\UserInterface $user */
$user = $this->entityManager()->getStorage('user')->load($this->currentUser()->id());
$user = $this->entityTypeManager()->getStorage('user')->load($this->currentUser()->id());
$metadata = $this->manager->getTranslationMetadata($target_translation);
// Update the translation author to current user, as well the translation
@ -85,7 +103,7 @@ class ContentTranslationController extends ControllerBase {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $route_match->getParameter($entity_type_id);
$account = $this->currentUser();
$handler = $this->entityManager()->getHandler($entity_type_id, 'translation');
$handler = $this->entityTypeManager()->getHandler($entity_type_id, 'translation');
$manager = $this->manager;
$entity_type = $entity->getEntityType();
$use_latest_revisions = $entity_type->isRevisionable() && ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $entity->bundle());
@ -108,7 +126,7 @@ class ContentTranslationController extends ControllerBase {
if ($this->languageManager()->isMultilingual()) {
// Determine whether the current entity is translatable.
$translatable = FALSE;
foreach ($this->entityManager->getFieldDefinitions($entity_type_id, $entity->bundle()) as $instance) {
foreach ($this->entityFieldManager->getFieldDefinitions($entity_type_id, $entity->bundle()) as $instance) {
if ($instance->isTranslatable()) {
$translatable = TRUE;
break;

View File

@ -84,7 +84,7 @@ class DbLogController extends ControllerBase {
$this->moduleHandler = $module_handler;
$this->dateFormatter = $date_formatter;
$this->formBuilder = $form_builder;
$this->userStorage = $this->entityManager()->getStorage('user');
$this->userStorage = $this->entityTypeManager()->getStorage('user');
}
/**

View File

@ -70,7 +70,7 @@ class EditorController extends ControllerBase {
$original_format_id = $request->request->get('original_format_id');
$original_format = NULL;
if (isset($original_format_id)) {
$original_format = $this->entityManager()
$original_format = $this->entityTypeManager()
->getStorage('filter_format')
->load($original_format_id);
}

View File

@ -18,7 +18,7 @@ class EntityDisplayModeController extends ControllerBase {
*/
public function viewModeTypeSelection() {
$entity_types = [];
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') && $entity_type->hasViewBuilderClass()) {
$entity_types[$entity_type_id] = [
'title' => $entity_type->getLabel(),
@ -41,7 +41,7 @@ class EntityDisplayModeController extends ControllerBase {
*/
public function formModeTypeSelection() {
$entity_types = [];
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') && $entity_type->hasFormClasses()) {
$entity_types[$entity_type_id] = [
'title' => $entity_type->getLabel(),

View File

@ -25,7 +25,7 @@ class FieldConfigListController extends EntityListController {
* \Drupal\Core\Render\RendererInterface::render().
*/
public function listing($entity_type_id = NULL, $bundle = NULL, RouteMatchInterface $route_match = NULL) {
return $this->entityManager()->getListBuilder('field_config')->render($entity_type_id, $bundle);
return $this->entityTypeManager()->getListBuilder('field_config')->render($entity_type_id, $bundle);
}
}

View File

@ -5,6 +5,7 @@ namespace Drupal\image\Controller;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Render\Element\StatusMessages;
@ -41,6 +42,13 @@ class QuickEditImageController extends ControllerBase {
*/
protected $imageFactory;
/**
* The entity display repository service.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* Constructs a new QuickEditImageController.
*
@ -50,11 +58,18 @@ class QuickEditImageController extends ControllerBase {
* The image factory.
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository service.
*/
public function __construct(RendererInterface $renderer, ImageFactory $image_factory, PrivateTempStoreFactory $temp_store_factory) {
public function __construct(RendererInterface $renderer, ImageFactory $image_factory, PrivateTempStoreFactory $temp_store_factory, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
$this->renderer = $renderer;
$this->imageFactory = $image_factory;
$this->tempStore = $temp_store_factory->get('quickedit');
if (!$entity_display_repository) {
@trigger_error('The entity_display.repository service must be passed to QuickEditImageController::__construct(), it is 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;
}
/**
@ -64,7 +79,8 @@ class QuickEditImageController extends ControllerBase {
return new static(
$container->get('renderer'),
$container->get('image.factory'),
$container->get('tempstore.private')
$container->get('tempstore.private'),
$container->get('entity_display.repository')
);
}
@ -115,7 +131,7 @@ class QuickEditImageController extends ControllerBase {
$entity->$field_name->setValue($value);
// Render the new image using the correct formatter settings.
$entity_view_mode_ids = array_keys($this->entityManager()->getViewModes($entity->getEntityTypeId()));
$entity_view_mode_ids = array_keys($this->entityDisplayRepository->getViewModes($entity->getEntityTypeId()));
if (in_array($view_mode_id, $entity_view_mode_ids, TRUE)) {
$output = $entity->$field_name->view($view_mode_id);
}

View File

@ -6,6 +6,7 @@ use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\node\NodeStorageInterface;
@ -32,6 +33,13 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
*/
protected $renderer;
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a NodeController object.
*
@ -39,10 +47,17 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
* The date formatter service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer) {
public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer, EntityRepositoryInterface $entity_repository = NULL) {
$this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
if (!$entity_repository) {
@trigger_error('The entity.repository service must be passed to NodeController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$this->entityRepository = $entity_repository;
}
/**
@ -51,7 +66,8 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
public static function create(ContainerInterface $container) {
return new static(
$container->get('date.formatter'),
$container->get('renderer')
$container->get('renderer'),
$container->get('entity.repository')
);
}
@ -70,15 +86,15 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
$build = [
'#theme' => 'node_add_list',
'#cache' => [
'tags' => $this->entityManager()->getDefinition('node_type')->getListCacheTags(),
'tags' => $this->entityTypeManager()->getDefinition('node_type')->getListCacheTags(),
],
];
$content = [];
// Only use node types the user has access to.
foreach ($this->entityManager()->getStorage('node_type')->loadMultiple() as $type) {
$access = $this->entityManager()->getAccessControlHandler('node')->createAccess($type->id(), NULL, [], TRUE);
foreach ($this->entityTypeManager()->getStorage('node_type')->loadMultiple() as $type) {
$access = $this->entityTypeManager()->getAccessControlHandler('node')->createAccess($type->id(), NULL, [], TRUE);
if ($access->isAllowed()) {
$content[$type->id()] = $type;
}
@ -106,7 +122,7 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
* A node submission form.
*/
public function add(NodeTypeInterface $node_type) {
$node = $this->entityManager()->getStorage('node')->create([
$node = $this->entityTypeManager()->getStorage('node')->create([
'type' => $node_type->id(),
]);
@ -125,9 +141,9 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
* An array suitable for \Drupal\Core\Render\RendererInterface::render().
*/
public function revisionShow($node_revision) {
$node = $this->entityManager()->getStorage('node')->loadRevision($node_revision);
$node = $this->entityManager()->getTranslationFromContext($node);
$node_view_controller = new NodeViewController($this->entityManager, $this->renderer, $this->currentUser());
$node = $this->entityTypeManager()->getStorage('node')->loadRevision($node_revision);
$node = $this->entityRepository->getTranslationFromContext($node);
$node_view_controller = new NodeViewController($this->entityTypeManager(), $this->renderer, $this->currentUser(), $this->entityRepository);
$page = $node_view_controller->view($node);
unset($page['nodes'][$node->id()]['#cache']);
return $page;
@ -143,7 +159,7 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
* The page title.
*/
public function revisionPageTitle($node_revision) {
$node = $this->entityManager()->getStorage('node')->loadRevision($node_revision);
$node = $this->entityTypeManager()->getStorage('node')->loadRevision($node_revision);
return $this->t('Revision of %title from %date', ['%title' => $node->label(), '%date' => $this->dateFormatter->format($node->getRevisionCreationTime())]);
}
@ -162,7 +178,7 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
$langname = $node->language()->getName();
$languages = $node->getTranslationLanguages();
$has_translations = (count($languages) > 1);
$node_storage = $this->entityManager()->getStorage('node');
$node_storage = $this->entityTypeManager()->getStorage('node');
$type = $node->getType();
$build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $node->label()]) : $this->t('Revisions for %title', ['%title' => $node->label()]);

View File

@ -2,14 +2,55 @@
namespace Drupal\node\Controller;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Controller\EntityViewController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a controller to render a single node in preview.
*/
class NodePreviewController extends EntityViewController {
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Creates an NodeViewController object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, EntityRepositoryInterface $entity_repository = NULL) {
parent::__construct($entity_type_manager, $renderer);
if (!$entity_repository) {
@trigger_error('The entity.repository service must be passed to NodePreviewController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('renderer'),
$container->get('entity.repository')
);
}
/**
* {@inheritdoc}
*/
@ -35,7 +76,7 @@ class NodePreviewController extends EntityViewController {
* The page title.
*/
public function title(EntityInterface $node_preview) {
return $this->entityManager->getTranslationFromContext($node_preview)->label();
return $this->entityRepository->getTranslationFromContext($node_preview)->label();
}
}

View File

@ -4,7 +4,8 @@ namespace Drupal\node\Controller;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Controller\EntityViewController;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -21,20 +22,34 @@ class NodeViewController extends EntityViewController {
*/
protected $currentUser;
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Creates an NodeViewController object.
*
* @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\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user. For backwards compatibility this is optional, however
* this will be removed before Drupal 9.0.0.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer, AccountInterface $current_user = NULL) {
parent::__construct($entity_manager, $renderer);
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, AccountInterface $current_user = NULL, EntityRepositoryInterface $entity_repository = NULL) {
parent::__construct($entity_type_manager, $renderer);
$this->currentUser = $current_user ?: \Drupal::currentUser();
if (!$entity_repository) {
@trigger_error('The entity.repository service must be passed to NodeViewController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$this->entityRepository = $entity_repository;
}
/**
@ -42,9 +57,10 @@ class NodeViewController extends EntityViewController {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('entity_type.manager'),
$container->get('renderer'),
$container->get('current_user')
$container->get('current_user'),
$container->get('entity.repository')
);
}
@ -109,7 +125,7 @@ class NodeViewController extends EntityViewController {
* The page title.
*/
public function title(EntityInterface $node) {
return $this->entityManager->getTranslationFromContext($node)->label();
return $this->entityRepository->getTranslationFromContext($node)->label();
}
}

View File

@ -3,6 +3,7 @@
namespace Drupal\quickedit;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
@ -12,6 +13,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\quickedit\Ajax\FieldFormCommand;
use Drupal\quickedit\Ajax\FieldFormSavedCommand;
use Drupal\quickedit\Ajax\FieldFormValidationErrorsCommand;
@ -50,6 +52,20 @@ class QuickEditController extends ControllerBase {
*/
protected $renderer;
/**
* The entity display repository service.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a new QuickEditController.
*
@ -61,12 +77,26 @@ class QuickEditController extends ControllerBase {
* The in-place editor selector.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(PrivateTempStoreFactory $temp_store_factory, MetadataGeneratorInterface $metadata_generator, EditorSelectorInterface $editor_selector, RendererInterface $renderer) {
public function __construct(PrivateTempStoreFactory $temp_store_factory, MetadataGeneratorInterface $metadata_generator, EditorSelectorInterface $editor_selector, RendererInterface $renderer, EntityDisplayRepositoryInterface $entity_display_repository, EntityRepositoryInterface $entity_repository) {
$this->tempStoreFactory = $temp_store_factory;
$this->metadataGenerator = $metadata_generator;
$this->editorSelector = $editor_selector;
$this->renderer = $renderer;
if (!$entity_display_repository) {
@trigger_error('The entity_display.repository service must be passed to QuickEditController::__construct(), it is 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;
if (!$entity_repository) {
@trigger_error('The entity.repository service must be passed to QuickEditController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$this->entityRepository = $entity_repository;
}
/**
@ -77,7 +107,9 @@ class QuickEditController extends ControllerBase {
$container->get('tempstore.private'),
$container->get('quickedit.metadata.generator'),
$container->get('quickedit.editor.selector'),
$container->get('renderer')
$container->get('renderer'),
$container->get('entity_display.repository'),
$container->get('entity.repository')
);
}
@ -103,10 +135,10 @@ class QuickEditController extends ControllerBase {
list($entity_type, $entity_id, $field_name, $langcode, $view_mode) = explode('/', $field);
// Load the entity.
if (!$entity_type || !$this->entityManager()->getDefinition($entity_type)) {
if (!$entity_type || !$this->entityTypeManager()->getDefinition($entity_type)) {
throw new NotFoundHttpException();
}
$entity = $this->entityManager()->getStorage($entity_type)->load($entity_id);
$entity = $this->entityTypeManager()->getStorage($entity_type)->load($entity_id);
if (!$entity) {
throw new NotFoundHttpException();
}
@ -256,9 +288,9 @@ class QuickEditController extends ControllerBase {
* @see hook_quickedit_render_field()
*/
protected function renderField(EntityInterface $entity, $field_name, $langcode, $view_mode_id) {
$entity_view_mode_ids = array_keys($this->entityManager()->getViewModes($entity->getEntityTypeId()));
$entity_view_mode_ids = array_keys($this->entityDisplayRepository->getViewModes($entity->getEntityTypeId()));
if (in_array($view_mode_id, $entity_view_mode_ids)) {
$entity = \Drupal::entityManager()->getTranslationFromContext($entity, $langcode);
$entity = $this->entityRepository->getTranslationFromContext($entity, $langcode);
$output = $entity->get($field_name)->view($view_mode_id);
}
else {

View File

@ -21,7 +21,7 @@ class ShortcutController extends ControllerBase {
* The shortcut add form.
*/
public function addForm(ShortcutSetInterface $shortcut_set) {
$shortcut = $this->entityManager()->getStorage('shortcut')->create(['shortcut_set' => $shortcut_set->id()]);
$shortcut = $this->entityTypeManager()->getStorage('shortcut')->create(['shortcut_set' => $shortcut_set->id()]);
return $this->entityFormBuilder()->getForm($shortcut, 'add');
}

View File

@ -55,7 +55,7 @@ class ShortcutSetController extends ControllerBase {
$link = $request->query->get('link');
$name = $request->query->get('name');
if (parse_url($link, PHP_URL_SCHEME) === NULL && $this->pathValidator->isValid($link)) {
$shortcut = $this->entityManager()->getStorage('shortcut')->create([
$shortcut = $this->entityTypeManager()->getStorage('shortcut')->create([
'title' => $name,
'shortcut_set' => $shortcut_set->id(),
'link' => [

View File

@ -34,7 +34,7 @@ class EntityTestController extends ControllerBase {
*/
public function listReferencingEntities($entity_reference_field_name, $referenced_entity_type, $referenced_entity_id) {
// Early return if the referenced entity does not exist (or is deleted).
$referenced_entity = $this->entityManager()
$referenced_entity = $this->entityTypeManager()
->getStorage($referenced_entity_type)
->load($referenced_entity_id);
if ($referenced_entity === NULL) {
@ -43,10 +43,10 @@ class EntityTestController extends ControllerBase {
$query = $this->entityTypeManager()->getStorage('entity_test')->getQuery()
->condition($entity_reference_field_name . '.target_id', $referenced_entity_id);
$entities = $this->entityManager()
$entities = $this->entityTypeManager()
->getStorage('entity_test')
->loadMultiple($query->execute());
return $this->entityManager()
return $this->entityTypeManager()
->getViewBuilder('entity_test')
->viewMultiple($entities, 'full');
}
@ -61,7 +61,7 @@ class EntityTestController extends ControllerBase {
* A renderable array.
*/
public function listEntitiesAlphabetically($entity_type_id) {
$entity_type_definition = $this->entityManager()->getDefinition($entity_type_id);
$entity_type_definition = $this->entityTypeManager()->getDefinition($entity_type_id);
$query = $this->entityTypeManager()->getStorage($entity_type_id)->getQuery();
// Sort by label field, if any.
@ -69,7 +69,7 @@ class EntityTestController extends ControllerBase {
$query->sort($label_field);
}
$entities = $this->entityManager()
$entities = $this->entityTypeManager()
->getStorage($entity_type_id)
->loadMultiple($query->execute());
@ -109,7 +109,7 @@ class EntityTestController extends ControllerBase {
* A renderable array.
*/
public function listEntitiesEmpty($entity_type_id) {
$entity_type_definition = $this->entityManager()->getDefinition($entity_type_id);
$entity_type_definition = $this->entityTypeManager()->getDefinition($entity_type_id);
return [
'#theme' => 'item_list',
'#items' => [],

View File

@ -26,7 +26,7 @@ class FormTestController extends ControllerBase {
'type' => 'page',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
$node1 = $this->entityManager()->getStorage('node')->create($values);
$node1 = $this->entityTypeManager()->getStorage('node')->create($values);
$node2 = clone($node1);
$return['node_form_1'] = $this->entityFormBuilder()->getForm($node1);
$return['node_form_2'] = $this->entityFormBuilder()->getForm($node2);

View File

@ -22,7 +22,7 @@ class TaxonomyController extends ControllerBase {
* The taxonomy term add form.
*/
public function addForm(VocabularyInterface $taxonomy_vocabulary) {
$term = $this->entityManager()->getStorage('taxonomy_term')->create(['vid' => $taxonomy_vocabulary->id()]);
$term = $this->entityTypeManager()->getStorage('taxonomy_term')->create(['vid' => $taxonomy_vocabulary->id()]);
return $this->entityFormBuilder()->getForm($term);
}

View File

@ -54,7 +54,7 @@ class ViewsUIController extends ControllerBase {
* The Views fields report page.
*/
public function reportFields() {
$views = $this->entityManager()->getStorage('view')->loadMultiple();
$views = $this->entityTypeManager()->getStorage('view')->loadMultiple();
// Fetch all fieldapi fields which are used in views
// Therefore search in all views, displays and handler-types.
@ -160,7 +160,7 @@ class ViewsUIController extends ControllerBase {
// If the request is via AJAX, return the rendered list as JSON.
if ($request->request->get('js')) {
$list = $this->entityManager()->getListBuilder('view')->render();
$list = $this->entityTypeManager()->getListBuilder('view')->render();
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#views-entity-list', $list));
return $response;
@ -183,7 +183,7 @@ class ViewsUIController extends ControllerBase {
$matches = [];
$string = $request->query->get('q');
// Get matches from default views.
$views = $this->entityManager()->getStorage('view')->loadMultiple();
$views = $this->entityTypeManager()->getStorage('view')->loadMultiple();
// Keep track of previously processed tags so they can be skipped.
$tags = [];
foreach ($views as $view) {