You are here

public function LayoutPluginManager::processDefinition in Drupal 8

Same name and namespace in other branches
  1. 9 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()

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 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();
    list($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);
}