comparison core/lib/Drupal/Core/Entity/EntityViewBuilder.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
2 2
3 namespace Drupal\Core\Entity; 3 namespace Drupal\Core\Entity;
4 4
5 use Drupal\Component\Utility\Crypt; 5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Cache\Cache; 6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
7 use Drupal\Core\Entity\Display\EntityViewDisplayInterface; 8 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
8 use Drupal\Core\Entity\Entity\EntityViewDisplay; 9 use Drupal\Core\Entity\Entity\EntityViewDisplay;
9 use Drupal\Core\Field\FieldItemInterface; 10 use Drupal\Core\Field\FieldItemInterface;
10 use Drupal\Core\Field\FieldItemListInterface; 11 use Drupal\Core\Field\FieldItemListInterface;
11 use Drupal\Core\Language\LanguageManagerInterface; 12 use Drupal\Core\Language\LanguageManagerInterface;
18 * Base class for entity view builders. 19 * Base class for entity view builders.
19 * 20 *
20 * @ingroup entity_api 21 * @ingroup entity_api
21 */ 22 */
22 class EntityViewBuilder extends EntityHandlerBase implements EntityHandlerInterface, EntityViewBuilderInterface { 23 class EntityViewBuilder extends EntityHandlerBase implements EntityHandlerInterface, EntityViewBuilderInterface {
24 use DeprecatedServicePropertyTrait;
25
26 /**
27 * {@inheritdoc}
28 */
29 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
23 30
24 /** 31 /**
25 * The type of entities for which this view builder is instantiated. 32 * The type of entities for which this view builder is instantiated.
26 * 33 *
27 * @var string 34 * @var string
34 * @var \Drupal\Core\Entity\EntityTypeInterface 41 * @var \Drupal\Core\Entity\EntityTypeInterface
35 */ 42 */
36 protected $entityType; 43 protected $entityType;
37 44
38 /** 45 /**
39 * The entity manager service. 46 * The entity repository service.
40 * 47 *
41 * @var \Drupal\Core\Entity\EntityManagerInterface 48 * @var \Drupal\Core\Entity\EntityRepositoryInterface
42 */ 49 */
43 protected $entityManager; 50 protected $entityRepository;
51
52 /**
53 * The entity display repository.
54 *
55 * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
56 */
57 protected $entityDisplayRepository;
44 58
45 /** 59 /**
46 * The cache bin used to store the render cache. 60 * The cache bin used to store the render cache.
47 * 61 *
48 * @var string 62 * @var string
75 /** 89 /**
76 * Constructs a new EntityViewBuilder. 90 * Constructs a new EntityViewBuilder.
77 * 91 *
78 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type 92 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
79 * The entity type definition. 93 * The entity type definition.
80 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 94 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
81 * The entity manager service. 95 * The entity repository service.
82 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager 96 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
83 * The language manager. 97 * The language manager.
84 * @param \Drupal\Core\Theme\Registry $theme_registry 98 * @param \Drupal\Core\Theme\Registry $theme_registry
85 * The theme registry. 99 * The theme registry.
86 */ 100 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
87 public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, Registry $theme_registry = NULL) { 101 * The entity display repository.
102 */
103 public function __construct(EntityTypeInterface $entity_type, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, Registry $theme_registry = NULL, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
88 $this->entityTypeId = $entity_type->id(); 104 $this->entityTypeId = $entity_type->id();
89 $this->entityType = $entity_type; 105 $this->entityType = $entity_type;
90 $this->entityManager = $entity_manager; 106 $this->entityRepository = $entity_repository;
91 $this->languageManager = $language_manager; 107 $this->languageManager = $language_manager;
92 $this->themeRegistry = $theme_registry ?: \Drupal::service('theme.registry'); 108 $this->themeRegistry = $theme_registry ?: \Drupal::service('theme.registry');
109 if (!$entity_display_repository) {
110 @trigger_error('Calling EntityViewBuilder::__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);
111 $entity_display_repository = \Drupal::service('entity_display.repository');
112 }
113 $this->entityDisplayRepository = $entity_display_repository;
93 } 114 }
94 115
95 /** 116 /**
96 * {@inheritdoc} 117 * {@inheritdoc}
97 */ 118 */
98 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { 119 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
99 return new static( 120 return new static(
100 $entity_type, 121 $entity_type,
101 $container->get('entity.manager'), 122 $container->get('entity.repository'),
102 $container->get('language_manager'), 123 $container->get('language_manager'),
103 $container->get('theme.registry') 124 $container->get('theme.registry'),
125 $container->get('entity_display.repository')
104 ); 126 );
105 } 127 }
106 128
107 /** 129 /**
108 * {@inheritdoc} 130 * {@inheritdoc}
130 ]; 152 ];
131 $weight = 0; 153 $weight = 0;
132 foreach ($entities as $key => $entity) { 154 foreach ($entities as $key => $entity) {
133 // Ensure that from now on we are dealing with the proper translation 155 // Ensure that from now on we are dealing with the proper translation
134 // object. 156 // object.
135 $entity = $this->entityManager->getTranslationFromContext($entity, $langcode); 157 $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode);
136 158
137 // Set build defaults. 159 // Set build defaults.
138 $build_list[$key] = $this->getBuildDefaults($entity, $view_mode); 160 $build_list[$key] = $this->getBuildDefaults($entity, $view_mode);
139 $entityType = $this->entityTypeId; 161 $entityType = $this->entityTypeId;
140 $this->moduleHandler()->alter([$entityType . '_build_defaults', 'entity_build_defaults'], $build_list[$key], $entity, $view_mode); 162 $this->moduleHandler()->alter([$entityType . '_build_defaults', 'entity_build_defaults'], $build_list[$key], $entity, $view_mode);
416 protected function isViewModeCacheable($view_mode) { 438 protected function isViewModeCacheable($view_mode) {
417 if ($view_mode == 'default') { 439 if ($view_mode == 'default') {
418 // The 'default' is not an actual view mode. 440 // The 'default' is not an actual view mode.
419 return TRUE; 441 return TRUE;
420 } 442 }
421 $view_modes_info = $this->entityManager->getViewModes($this->entityTypeId); 443 $view_modes_info = $this->entityDisplayRepository->getViewModes($this->entityTypeId);
422 return !empty($view_modes_info[$view_mode]['cache']); 444 return !empty($view_modes_info[$view_mode]['cache']);
423 } 445 }
424 446
425 /** 447 /**
426 * {@inheritdoc} 448 * {@inheritdoc}