public function EntityForm::getEntityFromRouteMatch

public EntityForm::getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id)

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

File

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

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

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityForm.php/function/EntityForm::getEntityFromRouteMatch/8.1.x