function LayoutPluginManager::processDefinition

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Layout/LayoutPluginManager.php \Drupal\Core\Layout\LayoutPluginManager::processDefinition()
  2. 10 core/lib/Drupal/Core/Layout/LayoutPluginManager.php \Drupal\Core\Layout\LayoutPluginManager::processDefinition()
  3. 11.x core/lib/Drupal/Core/Layout/LayoutPluginManager.php \Drupal\Core\Layout\LayoutPluginManager::processDefinition()

Overrides DefaultPluginManager::processDefinition

File

core/lib/Drupal/Core/Layout/LayoutPluginManager.php, line 89

Class

LayoutPluginManager
Provides a plugin manager for layouts.

Namespace

Drupal\Core\Layout

Code

public function processDefinition(&$definition, $plugin_id) {
    parent::processDefinition($definition, $plugin_id);
    if (!$definition instanceof LayoutDefinition) {
        throw new InvalidPluginDefinitionException($plugin_id, sprintf('The "%s" layout definition must extend %s', $plugin_id, LayoutDefinition::class));
    }
    // Add the module or theme path to the 'path'.
    $provider = $definition->getProvider();
    if ($this->moduleHandler
        ->moduleExists($provider)) {
        $base_path = $this->moduleHandler
            ->getModule($provider)
            ->getPath();
    }
    elseif ($this->themeHandler
        ->themeExists($provider)) {
        $base_path = $this->themeHandler
            ->getTheme($provider)
            ->getPath();
    }
    else {
        $base_path = '';
    }
    $path = $definition->getPath();
    $path = !empty($path) ? $base_path . '/' . $path : $base_path;
    $definition->setPath($path);
    // Add the base path to the icon path.
    if ($icon_path = $definition->getIconPath()) {
        $definition->setIconPath($path . '/' . $icon_path);
    }
    // Add a dependency on the provider of the library.
    if ($library = $definition->getLibrary()) {
        $config_dependencies = $definition->getConfigDependencies();
        [
            $library_provider,
        ] = explode('/', $library, 2);
        if ($this->moduleHandler
            ->moduleExists($library_provider)) {
            $config_dependencies['module'][] = $library_provider;
        }
        elseif ($this->themeHandler
            ->themeExists($library_provider)) {
            $config_dependencies['theme'][] = $library_provider;
        }
        $definition->setConfigDependencies($config_dependencies);
    }
    // If 'template' is set, then we'll derive 'template_path' and 'theme_hook'.
    $template = $definition->getTemplate();
    if (!empty($template)) {
        $template_parts = explode('/', $template);
        $template = array_pop($template_parts);
        $template_path = $path;
        if (count($template_parts) > 0) {
            $template_path .= '/' . implode('/', $template_parts);
        }
        $definition->setTemplate($template);
        $definition->setThemeHook(strtr($template, '-', '_'));
        $definition->setTemplatePath($template_path);
    }
    if (!$definition->getDefaultRegion()) {
        $definition->setDefaultRegion(key($definition->getRegions()));
    }
    // Makes sure region names are translatable.
    $regions = array_map(function ($region) {
        if (!$region['label'] instanceof TranslatableMarkup) {
            // Region labels from YAML discovery needs translation.
            $region['label'] = new TranslatableMarkup($region['label'], [], [
                'context' => 'layout_region',
            ]);
        }
        return $region;
    }, $definition->getRegions());
    $definition->setRegions($regions);
}

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