class EntityController

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Controller/EntityController.php \Drupal\Core\Entity\Controller\EntityController
  2. 10 core/lib/Drupal/Core/Entity/Controller/EntityController.php \Drupal\Core\Entity\Controller\EntityController
  3. 11.x core/lib/Drupal/Core/Entity/Controller/EntityController.php \Drupal\Core\Entity\Controller\EntityController

Provides the add-page and title callbacks for entities.

It provides:

  • The add-page callback.
  • An add title callback for entity types.
  • An add title callback for entity types with bundles.
  • A view title callback.
  • An edit title callback.
  • A delete title callback.

Hierarchy

Expanded class hierarchy of EntityController

1 file declares its use of EntityController
DefaultHtmlRouteProvider.php in core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php

File

core/lib/Drupal/Core/Entity/Controller/EntityController.php, line 34

Namespace

Drupal\Core\Entity\Controller
View source
class EntityController implements ContainerInjectionInterface {
    use StringTranslationTrait;
    use UrlGeneratorTrait;
    
    /**
     * The entity manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The entity type bundle info.
     *
     * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
     */
    protected $entityTypeBundleInfo;
    
    /**
     * The entity repository.
     *
     * @var \Drupal\Core\Entity\EntityRepositoryInterface
     */
    protected $entityRepository;
    
    /**
     * The renderer.
     *
     * @var \Drupal\Core\Render\RendererInterface
     */
    protected $renderer;
    
    /**
     * Constructs a new EntityController.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
     *   The entity type bundle info.
     * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
     *   The entity repository.
     * @param \Drupal\Core\Render\RendererInterface $renderer
     *   The renderer.
     * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
     *   The string translation.
     * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
     *   The url generator.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityRepositoryInterface $entity_repository, RendererInterface $renderer, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator) {
        $this->entityTypeManager = $entity_type_manager;
        $this->entityTypeBundleInfo = $entity_type_bundle_info;
        $this->entityRepository = $entity_repository;
        $this->renderer = $renderer;
        $this->stringTranslation = $string_translation;
        $this->urlGenerator = $url_generator;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('entity_type.manager'), $container->get('entity_type.bundle.info'), $container->get('entity.repository'), $container->get('renderer'), $container->get('string_translation'), $container->get('url_generator'));
    }
    
    /**
     * Returns a redirect response object for the specified route.
     *
     * @param string $route_name
     *   The name of the route to which to redirect.
     * @param array $route_parameters
     *   (optional) Parameters for the route.
     * @param array $options
     *   (optional) An associative array of additional options.
     * @param int $status
     *   (optional) The HTTP redirect status code for the redirect. The default is
     *   302 Found.
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     *   A redirect response object that may be returned by the controller.
     */
    protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
        $options['absolute'] = TRUE;
        return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options)->toString(), $status);
    }
    
    /**
     * Displays add links for the available bundles.
     *
     * Redirects to the add form if there's only one bundle available.
     *
     * @param string $entity_type_id
     *   The entity type ID.
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|array
     *   If there's only one available bundle, a redirect response.
     *   Otherwise, a render array with the add links for each bundle.
     */
    public function addPage($entity_type_id) {
        $entity_type = $this->entityTypeManager
            ->getDefinition($entity_type_id);
        $bundles = $this->entityTypeBundleInfo
            ->getBundleInfo($entity_type_id);
        $bundle_key = $entity_type->getKey('bundle');
        $bundle_entity_type_id = $entity_type->getBundleEntityType();
        $build = [
            '#theme' => 'entity_add_list',
            '#bundles' => [],
        ];
        if ($bundle_entity_type_id) {
            $bundle_argument = $bundle_entity_type_id;
            $bundle_entity_type = $this->entityTypeManager
                ->getDefinition($bundle_entity_type_id);
            $bundle_entity_type_label = $bundle_entity_type->getSingularLabel();
            $build['#cache']['tags'] = $bundle_entity_type->getListCacheTags();
            // Build the message shown when there are no bundles.
            $link_text = $this->t('Add a new @entity_type.', [
                '@entity_type' => $bundle_entity_type_label,
            ]);
            $link_route_name = 'entity.' . $bundle_entity_type->id() . '.add_form';
            $build['#add_bundle_message'] = $this->t('There is no @entity_type yet. @add_link', [
                '@entity_type' => $bundle_entity_type_label,
                '@add_link' => Link::createFromRoute($link_text, $link_route_name)->toString(),
            ]);
            // Filter out the bundles the user doesn't have access to.
            $access_control_handler = $this->entityTypeManager
                ->getAccessControlHandler($entity_type_id);
            foreach ($bundles as $bundle_name => $bundle_info) {
                $access = $access_control_handler->createAccess($bundle_name, NULL, [], TRUE);
                if (!$access->isAllowed()) {
                    unset($bundles[$bundle_name]);
                }
                $this->renderer
                    ->addCacheableDependency($build, $access);
            }
            // Add descriptions from the bundle entities.
            $bundles = $this->loadBundleDescriptions($bundles, $bundle_entity_type);
        }
        else {
            $bundle_argument = $bundle_key;
        }
        $form_route_name = 'entity.' . $entity_type_id . '.add_form';
        // Redirect if there's only one bundle available.
        if (count($bundles) == 1) {
            $bundle_names = array_keys($bundles);
            $bundle_name = reset($bundle_names);
            return $this->redirect($form_route_name, [
                $bundle_argument => $bundle_name,
            ]);
        }
        // Prepare the #bundles array for the template.
        foreach ($bundles as $bundle_name => $bundle_info) {
            $build['#bundles'][$bundle_name] = [
                'label' => $bundle_info['label'],
                'description' => isset($bundle_info['description']) ? $bundle_info['description'] : '',
                'add_link' => Link::createFromRoute($bundle_info['label'], $form_route_name, [
                    $bundle_argument => $bundle_name,
                ]),
            ];
        }
        return $build;
    }
    
    /**
     * Provides a generic add title callback for an entity type.
     *
     * @param string $entity_type_id
     *   The entity type ID.
     *
     * @return string
     *   The title for the entity add page.
     */
    public function addTitle($entity_type_id) {
        $entity_type = $this->entityTypeManager
            ->getDefinition($entity_type_id);
        return $this->t('Add @entity-type', [
            '@entity-type' => $entity_type->getSingularLabel(),
        ]);
    }
    
    /**
     * Provides a generic add title callback for entities with bundles.
     *
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match.
     * @param string $entity_type_id
     *   The entity type ID.
     * @param string $bundle_parameter
     *   The name of the route parameter that holds the bundle.
     *
     * @return string
     *   The title for the entity add page, if the bundle was found.
     */
    public function addBundleTitle(RouteMatchInterface $route_match, $entity_type_id, $bundle_parameter) {
        $bundles = $this->entityTypeBundleInfo
            ->getBundleInfo($entity_type_id);
        // If the entity has bundle entities, the parameter might have been upcasted
        // so fetch the raw parameter.
        $bundle = $route_match->getRawParameter($bundle_parameter);
        if (count($bundles) > 1 && isset($bundles[$bundle])) {
            return $this->t('Add @bundle', [
                '@bundle' => $bundles[$bundle]['label'],
            ]);
        }
        else {
            return $this->addTitle($entity_type_id);
        }
    }
    
    /**
     * Provides a generic title callback for a single entity.
     *
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match.
     * @param \Drupal\Core\Entity\EntityInterface $_entity
     *   (optional) An entity, passed in directly from the request attributes.
     *
     * @return string|null
     *   The title for the entity view page, if an entity was found.
     */
    public function title(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
        if ($entity = $this->doGetEntity($route_match, $_entity)) {
            return $entity->label();
        }
    }
    
    /**
     * Provides a generic edit title callback.
     *
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match.
     * @param \Drupal\Core\Entity\EntityInterface $_entity
     *   (optional) An entity, passed in directly from the request attributes.
     *
     * @return string|null
     *   The title for the entity edit page, if an entity was found.
     */
    public function editTitle(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
        if ($entity = $this->doGetEntity($route_match, $_entity)) {
            return $this->t('Edit %label', [
                '%label' => $entity->label(),
            ]);
        }
    }
    
    /**
     * Provides a generic delete title callback.
     *
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match.
     * @param \Drupal\Core\Entity\EntityInterface $_entity
     *   (optional) An entity, passed in directly from the request attributes, and
     *   set in \Drupal\Core\Entity\Enhancer\EntityRouteEnhancer.
     *
     * @return string
     *   The title for the entity delete page.
     */
    public function deleteTitle(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
        if ($entity = $this->doGetEntity($route_match, $_entity)) {
            return $this->t('Delete %label', [
                '%label' => $entity->label(),
            ]);
        }
    }
    
    /**
     * Determines the entity.
     *
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match.
     * @param \Drupal\Core\Entity\EntityInterface $_entity
     *   (optional) The entity, set in
     *   \Drupal\Core\Entity\Enhancer\EntityRouteEnhancer.
     *
     * @return \Drupal\Core\Entity\EntityInterface|null
     *   The entity, if it is passed in directly or if the first parameter of the
     *   active route is an entity; otherwise, NULL.
     */
    protected function doGetEntity(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
        if ($_entity) {
            $entity = $_entity;
        }
        else {
            // Let's look up in the route object for the name of upcasted values.
            foreach ($route_match->getParameters() as $parameter) {
                if ($parameter instanceof EntityInterface) {
                    $entity = $parameter;
                    break;
                }
            }
        }
        if (isset($entity)) {
            return $this->entityRepository
                ->getTranslationFromContext($entity);
        }
    }
    
    /**
     * Expands the bundle information with descriptions, if known.
     *
     * @param array $bundles
     *   An array of bundle information.
     * @param \Drupal\Core\Entity\EntityTypeInterface $bundle_entity_type
     *   The bundle entity type definition.
     *
     * @return array
     *   The expanded array of bundle information.
     */
    protected function loadBundleDescriptions(array $bundles, EntityTypeInterface $bundle_entity_type) {
        if (!$bundle_entity_type->entityClassImplements(EntityDescriptionInterface::class)) {
            return $bundles;
        }
        $bundle_names = array_keys($bundles);
        $storage = $this->entityTypeManager
            ->getStorage($bundle_entity_type->id());
        
        /** @var \Drupal\Core\Entity\EntityDescriptionInterface[] $bundle_entities */
        $bundle_entities = $storage->loadMultiple($bundle_names);
        foreach ($bundles as $bundle_name => &$bundle_info) {
            if (isset($bundle_entities[$bundle_name])) {
                $bundle_info['description'] = $bundle_entities[$bundle_name]->getDescription();
            }
        }
        return $bundles;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityController::$entityRepository protected property The entity repository.
EntityController::$entityTypeBundleInfo protected property The entity type bundle info.
EntityController::$entityTypeManager protected property The entity manager.
EntityController::$renderer protected property The renderer.
EntityController::addBundleTitle public function Provides a generic add title callback for entities with bundles.
EntityController::addPage public function Displays add links for the available bundles.
EntityController::addTitle public function Provides a generic add title callback for an entity type.
EntityController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityController::deleteTitle public function Provides a generic delete title callback.
EntityController::doGetEntity protected function Determines the entity.
EntityController::editTitle public function Provides a generic edit title callback.
EntityController::loadBundleDescriptions protected function Expands the bundle information with descriptions, if known.
EntityController::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
EntityController::title public function Provides a generic title callback for a single entity.
EntityController::__construct public function Constructs a new EntityController.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.