You are here

public function EntityForm::getEntityFromRouteMatch in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/EntityForm.php \Drupal\Core\Entity\EntityForm::getEntityFromRouteMatch()
  2. 9 core/lib/Drupal/Core/Entity/EntityForm.php \Drupal\Core\Entity\EntityForm::getEntityFromRouteMatch()

Determines which entity will be used by this form from a RouteMatch object.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match.

string $entity_type_id: The entity type identifier.

Return value

\Drupal\Core\Entity\EntityInterface The entity object as determined from the passed-in route match.

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 353

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;
}