You are here

abstract class PatternBase in UI Patterns 8

Class PatternBase.

@package Drupal\ui_patterns\Plugin

Hierarchy

Expanded class hierarchy of PatternBase

2 files declare their use of PatternBase
LibraryPattern.php in modules/ui_patterns_library/src/Plugin/UiPatterns/Pattern/LibraryPattern.php
PatternBaseTest.php in tests/src/Kernel/Plugin/PatternBaseTest.php

File

src/Plugin/PatternBase.php, line 16

Namespace

Drupal\ui_patterns\Plugin
View source
abstract class PatternBase extends PluginBase implements PatternInterface, ContainerFactoryPluginInterface {

  /**
   * The app root.
   *
   * @var string
   */
  protected $root;

  /**
   * Module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * UiPatternsManager constructor.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, ModuleHandlerInterface $module_handler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->root = $root;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('app.root'), $container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function getThemeImplementation() {
    $definition = $this
      ->getPluginDefinition();
    $item = [];
    $item += $this
      ->processVariables($definition);
    $item += $this
      ->processUseProperty($definition);
    return [
      $definition['theme hook'] => $item,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getLibraryDefinitions() {

    // @codingStandardsIgnoreStart
    $libraries = [];
    $definition = $this
      ->getPluginDefinition();

    // Get only locally defined libraries.
    $items = array_filter($definition['libraries'], function ($library) {
      return is_array($library);
    });

    // Attach pattern base path to assets.
    if (!empty($definition['base path'])) {
      $base_path = str_replace($this->root, '', $definition['base path']);
      $this
        ->processLibraries($items, $base_path);
    }

    // Produce final libraries array.
    $id = $definition['id'];
    array_walk($items, function ($value) use (&$libraries, $id) {
      $libraries[$id . '.' . key($value)] = reset($value);
    });

    // @codingStandardsIgnoreEnd
    return $libraries;
  }

  /**
   * Process libraries.
   *
   * @param array|string $libraries
   *   List of dependencies or "dependencies:" root property.
   * @param string $base_path
   *   Pattern base path.
   * @param string $parent
   *   Item parent set in previous recursive iteration, if any.
   */
  protected function processLibraries(&$libraries, $base_path, $parent = '') {
    if (!is_string($libraries)) {
      $parents = [
        'js',
        'base',
        'layout',
        'component',
        'state',
        'theme',
      ];
      $_libraries = $libraries;
      foreach ($_libraries as $name => $values) {
        $is_asset = in_array($parent, $parents, TRUE);
        $is_external = isset($values['type']) && $values['type'] == 'external';
        if ($is_asset && !$is_external) {
          $libraries[$base_path . DIRECTORY_SEPARATOR . $name] = $values;
          unset($libraries[$name]);
        }
        elseif (!$is_asset) {
          $this
            ->processLibraries($libraries[$name], $base_path, $name);
        }
      }
    }
  }

  /**
   * Process 'use' definition property.
   *
   * @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
   *   Pattern definition array.
   *
   * @return array
   *   Processed hook definition portion.
   */
  protected function processUseProperty(PatternDefinition $definition) {
    $return = [];
    if ($definition
      ->hasUse()) {
      $return = [
        'path' => $this->moduleHandler
          ->getModule('ui_patterns')
          ->getPath() . '/templates',
        'template' => 'patterns-use-wrapper',
      ];
    }
    return $return;
  }

  /**
   * Process theme variables.
   *
   * @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
   *   Pattern definition array.
   *
   * @return array
   *   Processed hook definition portion.
   */
  protected function processVariables(PatternDefinition $definition) {
    $return = [];
    foreach ($definition
      ->getFields() as $field) {
      $return['variables'][$field
        ->getName()] = NULL;
    }
    $return['variables']['attributes'] = [];
    $return['variables']['context'] = [];
    $return['variables']['variant'] = '';
    $return['variables']['use'] = '';
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PatternBase::$moduleHandler protected property Module handler.
PatternBase::$root protected property The app root.
PatternBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
PatternBase::getLibraryDefinitions public function Get library definitions for current pattern. Overrides PatternInterface::getLibraryDefinitions
PatternBase::getThemeImplementation public function Get theme implementation for current pattern. Overrides PatternInterface::getThemeImplementation 1
PatternBase::processLibraries protected function Process libraries.
PatternBase::processUseProperty protected function Process 'use' definition property.
PatternBase::processVariables protected function Process theme variables.
PatternBase::__construct public function UiPatternsManager constructor. Overrides PluginBase::__construct 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.