function OverridesSectionStorage::extractEntityFromRoute

Same name in other branches
  1. 9 core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::extractEntityFromRoute()
  2. 8.9.x core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::extractEntityFromRoute()
  3. 11.x core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::extractEntityFromRoute()

Extracts an entity from the route values.

Parameters

mixed $value: The raw value from the route.

array $defaults: The route defaults array.

Return value

\Drupal\Core\Entity\EntityInterface|null The entity for the route, or NULL if none exist. The entity is not guaranteed to be fieldable, or contain the necessary field for this section storage plugin.

See also

\Drupal\layout_builder\SectionStorageInterface::deriveContextsFromRoute()

\Drupal\Core\ParamConverter\ParamConverterInterface::convert()

1 call to OverridesSectionStorage::extractEntityFromRoute()
OverridesSectionStorage::deriveContextsFromRoute in core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php
Derives the available plugin contexts from route values.

File

core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php, line 207

Class

OverridesSectionStorage
Defines the 'overrides' section storage type.

Namespace

Drupal\layout_builder\Plugin\SectionStorage

Code

private function extractEntityFromRoute($value, array $defaults) {
    if (str_contains($value, '.')) {
        [
            $entity_type_id,
            $entity_id,
        ] = explode('.', $value, 2);
    }
    elseif (isset($defaults['entity_type_id']) && !empty($defaults[$defaults['entity_type_id']])) {
        $entity_type_id = $defaults['entity_type_id'];
        $entity_id = $defaults[$entity_type_id];
    }
    else {
        return NULL;
    }
    $entity = $this->entityRepository
        ->getActive($entity_type_id, $entity_id);
    return $entity instanceof FieldableEntityInterface ? $entity : NULL;
}

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