You are here

public function ComplexEntityFormModes::getEntity in Form mode manager 8.2

Return value

\Drupal\Core\Entity\EntityInterface The entity loaded form route_match.

Throws

\Exception If an invalid entity is retrieving from the route object.

Overrides AbstractEntityFormModesFactory::getEntity

File

src/ComplexEntityFormModes.php, line 113

Class

ComplexEntityFormModes
Route controller factory specific for each entities using bundles.

Namespace

Drupal\form_mode_manager

Code

public function getEntity(RouteMatchInterface $route_match) {

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $this
    ->getEntityFromRouteMatch($route_match);

  // If we can't retrieve the entity from the route match get load,
  // it by their storage with correct route bundle key.
  if (empty($entity)) {
    $route_entity_type_info = $this
      ->getEntityTypeFromRouteMatch($route_match);

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this->entityTypeManager
      ->getStorage($route_entity_type_info['entity_type_id'])
      ->create([
      $route_entity_type_info['entity_key'] => $route_entity_type_info['bundle'],
    ]);
  }
  return $entity;
}