3 namespace Drupal\Core\Entity\Controller;
5 use Drupal\Core\Entity\EntityDescriptionInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityRepositoryInterface;
8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Drupal\Core\Entity\EntityTypeInterface;
12 use Drupal\Core\Render\RendererInterface;
13 use Drupal\Core\Routing\RouteMatchInterface;
14 use Drupal\Core\Routing\UrlGeneratorInterface;
15 use Drupal\Core\Routing\UrlGeneratorTrait;
16 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
17 use Drupal\Core\StringTranslation\StringTranslationTrait;
18 use Drupal\Core\StringTranslation\TranslationInterface;
19 use Symfony\Component\DependencyInjection\ContainerInterface;
22 * Provides the add-page and title callbacks for entities.
25 * - The add-page callback.
26 * - An add title callback for entity types.
27 * - An add title callback for entity types with bundles.
28 * - A view title callback.
29 * - An edit title callback.
30 * - A delete title callback.
32 class EntityController implements ContainerInjectionInterface {
34 use StringTranslationTrait;
35 use UrlGeneratorTrait;
40 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
42 protected $entityTypeManager;
45 * The entity type bundle info.
47 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
49 protected $entityTypeBundleInfo;
52 * The entity repository.
54 * @var \Drupal\Core\Entity\EntityRepositoryInterface
56 protected $entityRepository;
61 * @var \Drupal\Core\Render\RendererInterface
66 * Constructs a new EntityController.
68 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
69 * The entity type manager.
70 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
71 * The entity type bundle info.
72 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
73 * The entity repository.
74 * @param \Drupal\Core\Render\RendererInterface $renderer
76 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
77 * The string translation.
78 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
81 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityRepositoryInterface $entity_repository, RendererInterface $renderer, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator) {
82 $this->entityTypeManager = $entity_type_manager;
83 $this->entityTypeBundleInfo = $entity_type_bundle_info;
84 $this->entityRepository = $entity_repository;
85 $this->renderer = $renderer;
86 $this->stringTranslation = $string_translation;
87 $this->urlGenerator = $url_generator;
93 public static function create(ContainerInterface $container) {
95 $container->get('entity_type.manager'),
96 $container->get('entity_type.bundle.info'),
97 $container->get('entity.repository'),
98 $container->get('renderer'),
99 $container->get('string_translation'),
100 $container->get('url_generator')
105 * Displays add links for the available bundles.
107 * Redirects to the add form if there's only one bundle available.
109 * @param string $entity_type_id
110 * The entity type ID.
112 * @return \Symfony\Component\HttpFoundation\RedirectResponse|array
113 * If there's only one available bundle, a redirect response.
114 * Otherwise, a render array with the add links for each bundle.
116 public function addPage($entity_type_id) {
117 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
118 $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
119 $bundle_key = $entity_type->getKey('bundle');
120 $bundle_entity_type_id = $entity_type->getBundleEntityType();
122 '#theme' => 'entity_add_list',
125 if ($bundle_entity_type_id) {
126 $bundle_argument = $bundle_entity_type_id;
127 $bundle_entity_type = $this->entityTypeManager->getDefinition($bundle_entity_type_id);
128 $bundle_entity_type_label = $bundle_entity_type->getLowercaseLabel();
129 $build['#cache']['tags'] = $bundle_entity_type->getListCacheTags();
131 // Build the message shown when there are no bundles.
132 $link_text = $this->t('Add a new @entity_type.', ['@entity_type' => $bundle_entity_type_label]);
133 $link_route_name = 'entity.' . $bundle_entity_type->id() . '.add_form';
134 $build['#add_bundle_message'] = $this->t('There is no @entity_type yet. @add_link', [
135 '@entity_type' => $bundle_entity_type_label,
136 '@add_link' => Link::createFromRoute($link_text, $link_route_name)->toString(),
138 // Filter out the bundles the user doesn't have access to.
139 $access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
140 foreach ($bundles as $bundle_name => $bundle_info) {
141 $access = $access_control_handler->createAccess($bundle_name, NULL, [], TRUE);
142 if (!$access->isAllowed()) {
143 unset($bundles[$bundle_name]);
145 $this->renderer->addCacheableDependency($build, $access);
147 // Add descriptions from the bundle entities.
148 $bundles = $this->loadBundleDescriptions($bundles, $bundle_entity_type);
151 $bundle_argument = $bundle_key;
154 $form_route_name = 'entity.' . $entity_type_id . '.add_form';
155 // Redirect if there's only one bundle available.
156 if (count($bundles) == 1) {
157 $bundle_names = array_keys($bundles);
158 $bundle_name = reset($bundle_names);
159 return $this->redirect($form_route_name, [$bundle_argument => $bundle_name]);
161 // Prepare the #bundles array for the template.
162 foreach ($bundles as $bundle_name => $bundle_info) {
163 $build['#bundles'][$bundle_name] = [
164 'label' => $bundle_info['label'],
165 'description' => isset($bundle_info['description']) ? $bundle_info['description'] : '',
166 'add_link' => Link::createFromRoute($bundle_info['label'], $form_route_name, [$bundle_argument => $bundle_name]),
174 * Provides a generic add title callback for an entity type.
176 * @param string $entity_type_id
177 * The entity type ID.
180 * The title for the entity add page.
182 public function addTitle($entity_type_id) {
183 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
184 return $this->t('Add @entity-type', ['@entity-type' => $entity_type->getLowercaseLabel()]);
188 * Provides a generic add title callback for entities with bundles.
190 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
192 * @param string $entity_type_id
193 * The entity type ID.
194 * @param string $bundle_parameter
195 * The name of the route parameter that holds the bundle.
198 * The title for the entity add page, if the bundle was found.
200 public function addBundleTitle(RouteMatchInterface $route_match, $entity_type_id, $bundle_parameter) {
201 $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
202 // If the entity has bundle entities, the parameter might have been upcasted
203 // so fetch the raw parameter.
204 $bundle = $route_match->getRawParameter($bundle_parameter);
205 if ((count($bundles) > 1) && isset($bundles[$bundle])) {
206 return $this->t('Add @bundle', ['@bundle' => $bundles[$bundle]['label']]);
208 // If the entity supports bundles generally, but only has a single bundle,
209 // the bundle is probably something like 'Default' so that it preferable to
210 // use the entity type label.
212 return $this->addTitle($entity_type_id);
217 * Provides a generic title callback for a single entity.
219 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
221 * @param \Drupal\Core\Entity\EntityInterface $_entity
222 * (optional) An entity, passed in directly from the request attributes.
224 * @return string|null
225 * The title for the entity view page, if an entity was found.
227 public function title(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
228 if ($entity = $this->doGetEntity($route_match, $_entity)) {
229 return $entity->label();
234 * Provides a generic edit title callback.
236 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
238 * @param \Drupal\Core\Entity\EntityInterface $_entity
239 * (optional) An entity, passed in directly from the request attributes.
241 * @return string|null
242 * The title for the entity edit page, if an entity was found.
244 public function editTitle(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
245 if ($entity = $this->doGetEntity($route_match, $_entity)) {
246 return $this->t('Edit %label', ['%label' => $entity->label()]);
251 * Provides a generic delete title callback.
253 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
255 * @param \Drupal\Core\Entity\EntityInterface $_entity
256 * (optional) An entity, passed in directly from the request attributes, and
257 * set in \Drupal\Core\Entity\Enhancer\EntityRouteEnhancer.
260 * The title for the entity delete page.
262 public function deleteTitle(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
263 if ($entity = $this->doGetEntity($route_match, $_entity)) {
264 return $this->t('Delete %label', ['%label' => $entity->label()]);
269 * Determines the entity.
271 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
273 * @param \Drupal\Core\Entity\EntityInterface $_entity
274 * (optional) The entity, set in
275 * \Drupal\Core\Entity\Enhancer\EntityRouteEnhancer.
277 * @return \Drupal\Core\Entity\EntityInterface|null
278 * The entity, if it is passed in directly or if the first parameter of the
279 * active route is an entity; otherwise, NULL.
281 protected function doGetEntity(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
286 // Let's look up in the route object for the name of upcasted values.
287 foreach ($route_match->getParameters() as $parameter) {
288 if ($parameter instanceof EntityInterface) {
289 $entity = $parameter;
294 if (isset($entity)) {
295 return $this->entityRepository->getTranslationFromContext($entity);
300 * Expands the bundle information with descriptions, if known.
302 * @param array $bundles
303 * An array of bundle information.
304 * @param \Drupal\Core\Entity\EntityTypeInterface $bundle_entity_type
305 * The bundle entity type definition.
308 * The expanded array of bundle information.
310 protected function loadBundleDescriptions(array $bundles, EntityTypeInterface $bundle_entity_type) {
311 if (!$bundle_entity_type->entityClassImplements(EntityDescriptionInterface::class)) {
314 $bundle_names = array_keys($bundles);
315 $storage = $this->entityTypeManager->getStorage($bundle_entity_type->id());
316 /** @var \Drupal\Core\Entity\EntityDescriptionInterface[] $bundle_entities */
317 $bundle_entities = $storage->loadMultiple($bundle_names);
318 foreach ($bundles as $bundle_name => &$bundle_info) {
319 if (isset($bundle_entities[$bundle_name])) {
320 $bundle_info['description'] = $bundle_entities[$bundle_name]->getDescription();