function EntityForm::getEntityFromRouteMatch

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityForm.php \Drupal\Core\Entity\EntityForm::getEntityFromRouteMatch()
  2. 8.9.x core/lib/Drupal/Core/Entity/EntityForm.php \Drupal\Core\Entity\EntityForm::getEntityFromRouteMatch()
  3. 11.x core/lib/Drupal/Core/Entity/EntityForm.php \Drupal\Core\Entity\EntityForm::getEntityFromRouteMatch()

Overrides EntityFormInterface::getEntityFromRouteMatch

3 methods override EntityForm::getEntityFromRouteMatch()
DefaultsEntityForm::getEntityFromRouteMatch in core/modules/layout_builder/src/Form/DefaultsEntityForm.php
Determines which entity will be used by this form from a RouteMatch object.
EntityDisplayFormBase::getEntityFromRouteMatch in core/modules/field_ui/src/Form/EntityDisplayFormBase.php
Determines which entity will be used by this form from a RouteMatch object.
FieldStorageConfigEditForm::getEntityFromRouteMatch in core/modules/field_ui/src/Form/FieldStorageConfigEditForm.php
Determines which entity will be used by this form from a RouteMatch object.

File

core/lib/Drupal/Core/Entity/EntityForm.php, line 363

Class

EntityForm
Base class for entity forms.

Namespace

Drupal\Core\Entity

Code

public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
    if ($route_match->getRawParameter($entity_type_id) !== NULL) {
        $entity = $route_match->getParameter($entity_type_id);
    }
    else {
        $values = [];
        // If the entity has bundles, fetch it from the route match.
        $entity_type = $this->entityTypeManager
            ->getDefinition($entity_type_id);
        if ($bundle_key = $entity_type->getKey('bundle')) {
            if (($bundle_entity_type_id = $entity_type->getBundleEntityType()) && $route_match->getRawParameter($bundle_entity_type_id)) {
                $values[$bundle_key] = $route_match->getParameter($bundle_entity_type_id)
                    ->id();
            }
            elseif ($route_match->getRawParameter($bundle_key)) {
                $values[$bundle_key] = $route_match->getParameter($bundle_key);
            }
        }
        $entity = $this->entityTypeManager
            ->getStorage($entity_type_id)
            ->create($values);
    }
    return $entity;
}

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