function LayoutPluginManager::processDefinition

Performs extra processing on plugin definitions.

By default we add defaults for the type to the definition. If a type has additional processing logic they can do that by replacing or extending the method.

Overrides DefaultPluginManager::processDefinition

File

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

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));
  }
  if (empty($definition->getLabel())) {
    @trigger_error('A layout plugin not having a label is deprecated in drupal:11.4.0 and having a label will be enforced in drupal:12.0.0. See https://www.drupal.org/node/3464076', E_USER_DEPRECATED);
  }
  // Ensure that every plugin has a category.
  $provider = $definition->getProvider();
  if ($this->moduleHandler
    ->moduleExists($provider)) {
    $extension = $this->moduleHandler
      ->getModule($provider);
  }
  elseif ($this->themeHandler
    ->themeExists($provider)) {
    $extension = $this->themeHandler
      ->getTheme($provider);
  }
  else {
    $extension = NULL;
  }
  if (empty($definition->getCategory())) {
    // Default to the human-readable name if the provider is a module or
    // theme; otherwise the provider machine name is used.
    $category = $extension ? $extension->getName() : $provider;
    $definition->setCategory($category);
  }
  // Add the module or theme path to the 'path'.
  $base_path = $extension ? $extension->getPath() : '';
  $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.
      // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
      $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.