function DefaultsSectionStorage::extractEntityFromRoute

Same name in other branches
  1. 9 core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage::extractEntityFromRoute()
  2. 8.9.x core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage::extractEntityFromRoute()
  3. 11.x core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage::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.

See also

\Drupal\layout_builder\SectionStorageInterface::deriveContextsFromRoute()

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

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

File

core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php, line 267

Class

DefaultsSectionStorage
Defines the 'defaults' section storage type.

Namespace

Drupal\layout_builder\Plugin\SectionStorage

Code

private function extractEntityFromRoute($value, array $defaults) {
    // If a bundle is not provided but a value corresponding to the bundle key
    // is, use that for the bundle value.
    if (empty($defaults['bundle']) && isset($defaults['bundle_key']) && !empty($defaults[$defaults['bundle_key']])) {
        $defaults['bundle'] = $defaults[$defaults['bundle_key']];
    }
    if (is_string($value) && str_contains($value, '.')) {
        [
            $entity_type_id,
            $bundle,
            $view_mode,
        ] = explode('.', $value, 3);
    }
    elseif (!empty($defaults['entity_type_id']) && !empty($defaults['bundle']) && !empty($defaults['view_mode_name'])) {
        $entity_type_id = $defaults['entity_type_id'];
        $bundle = $defaults['bundle'];
        $view_mode = $defaults['view_mode_name'];
        $value = "{$entity_type_id}.{$bundle}.{$view_mode}";
    }
    else {
        return NULL;
    }
    $storage = $this->entityTypeManager
        ->getStorage('entity_view_display');
    // If the display does not exist, create a new one.
    if (!($display = $storage->load($value))) {
        $display = $storage->create([
            'targetEntityType' => $entity_type_id,
            'bundle' => $bundle,
            'mode' => $view_mode,
            'status' => TRUE,
        ]);
    }
    return $display;
}

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