You are here

class LibraryPattern in Gin Layout Builder 8.2

Detect Wingsuit (*.wingsuit.yml) Pattern definitions.

Plugin annotation


@UiPattern(
  id = "yaml",
  label = @Translation("Wingsuit Library Pattern"),
  description = @Translation("Pattern defined using a *.wingsuit.yml file."),
  deriver = "\Drupal\wingsuit_ui_patterns\Plugin\Deriver\LibraryDeriver"
)

Hierarchy

Expanded class hierarchy of LibraryPattern

File

modules/wingsuit_ui_patterns/src/Plugin/UiPatterns/Pattern/LibraryPattern.php, line 21

Namespace

Drupal\wingsuit_ui_patterns\Plugin\UiPatterns\Pattern
View source
class LibraryPattern extends PatternBase {

  /**
   * Theme handler.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  protected $themeHandler;

  /**
   * UiPatternsManager constructor.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $root, $module_handler);
    $this->themeHandler = $theme_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'), $container
      ->get('theme_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function getThemeImplementation() {
    $item = parent::getThemeImplementation();
    $definition = $this
      ->getPluginDefinition();
    $item[$definition['theme hook']] += $this
      ->processTemplateProperty($definition);
    $item[$definition['theme hook']] += $this
      ->processCustomThemeHookProperty($definition);
    return $item;
  }

  /**
   * Process 'custom hook theme' definition property.
   *
   * @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
   *   Pattern definition array.
   *
   * @return array
   *   Processed hook definition portion.
   */
  protected function processCustomThemeHookProperty(PatternDefinition $definition) {

    /** @var \Drupal\Core\Extension\Extension $module */
    $return = [];
    if (!$definition
      ->hasCustomThemeHook() && $this->moduleHandler
      ->moduleExists($definition
      ->getProvider())) {
      $module = $this->moduleHandler
        ->getModule($definition
        ->getProvider());
      $return['path'] = $module
        ->getPath() . '/templates';
      if ($this
        ->templateExists($definition
        ->getBasePath(), $definition
        ->getTemplate())) {
        $return['path'] = str_replace($this->root, '', $definition
          ->getBasePath());
      }
    }
    return $return;
  }

  /**
   * Weather template exists in given directory.
   *
   * @param string $directory
   *   Directory full path.
   * @param string $template
   *   Template name, without default Twig extension.
   *
   * @return bool
   *   Weather template exists in given directory.
   */
  protected function templateExists($directory, $template) {
    return file_exists($directory . DIRECTORY_SEPARATOR . $template . '.html.twig');
  }

  /**
   * Process 'template' definition property.
   *
   * @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
   *   Pattern definition array.
   *
   * @return array
   *   Processed hook definition portion.
   */
  protected function processTemplateProperty(PatternDefinition $definition) {
    $return = [];
    if ($definition
      ->hasTemplate()) {
      $return = [
        'template' => $definition
          ->getTemplate(),
      ];
    }
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LibraryPattern::$themeHandler protected property Theme handler.
LibraryPattern::create public static function Creates an instance of the plugin. Overrides PatternBase::create
LibraryPattern::getThemeImplementation public function Get theme implementation for current pattern. Overrides PatternBase::getThemeImplementation
LibraryPattern::processCustomThemeHookProperty protected function Process 'custom hook theme' definition property.
LibraryPattern::processTemplateProperty protected function Process 'template' definition property.
LibraryPattern::templateExists protected function Weather template exists in given directory.
LibraryPattern::__construct public function UiPatternsManager constructor. Overrides PatternBase::__construct
PatternBase::$moduleHandler protected property Module handler.
PatternBase::$root protected property The app root.
PatternBase::getLibraryDefinitions public function Get library definitions for current pattern. Overrides PatternInterface::getLibraryDefinitions
PatternBase::processLibraries protected function Process libraries.
PatternBase::processUseProperty protected function Process 'use' definition property.
PatternBase::processVariables protected function Process theme variables.
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.