You are here

BlockStyleManager.php in Block Style Plugins 8.2

Same filename and directory in other branches
  1. 8 src/Plugin/BlockStyleManager.php

File

src/Plugin/BlockStyleManager.php
View source
<?php

namespace Drupal\block_style_plugins\Plugin;

use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator;
use Drupal\Core\Theme\ThemeManagerInterface;

/**
 * Provides the Block style plugin manager.
 */
class BlockStyleManager extends DefaultPluginManager {

  /**
   * A set of defaults to be referenced by $this->processDefinition().
   *
   * @var array
   */
  protected $defaults = [
    'class' => 'Drupal\\block_style_plugins\\Plugin\\BlockStyle',
  ];

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

  /**
   * The theme manager.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface
   */
  protected $themeManager;

  /**
   * Constructor for BlockStyleManager objects.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke the alter hook with.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler to get installation status.
   * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
   *   The theme manager to invoke the alter hook with.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, ThemeManagerInterface $theme_manager) {
    parent::__construct('Plugin/BlockStyle', $namespaces, $module_handler, 'Drupal\\block_style_plugins\\Plugin\\BlockStyleInterface', 'Drupal\\block_style_plugins\\Annotation\\BlockStyle');

    // Add in the Theme directory Discovery via Yaml file.
    $discovery = $this
      ->getDiscovery();
    $this->discovery = new YamlDiscoveryDecorator($discovery, 'blockstyle', $module_handler
      ->getModuleDirectories() + $theme_handler
      ->getThemeDirectories());
    $this->themeHandler = $theme_handler;
    $this->themeManager = $theme_manager;
    $this
      ->alterInfo('block_style_plugins_info');
    $this
      ->setCacheBackend($cache_backend, 'block_style_plugins');
  }

  /**
   * {@inheritdoc}
   */
  protected function providerExists($provider) {
    return $this->moduleHandler
      ->moduleExists($provider) || $this->themeHandler
      ->themeExists($provider);
  }

  /**
   * {@inheritdoc}
   */
  protected function alterDefinitions(&$definitions) {
    if ($this->alterHook) {
      $this->moduleHandler
        ->alter($this->alterHook, $definitions);
      $this->themeManager
        ->alter($this->alterHook, $definitions);
    }
  }

  /**
   * Gets the definition of all plugins for the Block type.
   *
   * @return mixed[]
   *   An array of block plugin definitions (empty array if no definitions were
   *   found). Keys are plugin IDs.
   */
  public function getBlockDefinitions() {
    $definitions = $this
      ->getDefinitions();
    $block_definitions = array_filter($definitions, function ($def) {
      return empty($def['type']) || $def['type'] == 'block' || $def['type'] == 'all';
    });
    return $block_definitions;
  }

  /**
   * Gets the definition of all plugins for the Section type.
   *
   * @return mixed[]
   *   An array of section plugin definitions (empty array if no definitions
   *   were found). Keys are plugin IDs.
   */
  public function getSectionDefinitions() {
    $definitions = $this
      ->getDefinitions();
    $section_definitions = array_filter($definitions, function ($def) {
      return isset($def['type']) && ($def['type'] == 'section' || $def['type'] == 'all');
    });
    return $section_definitions;
  }

}

Classes

Namesort descending Description
BlockStyleManager Provides the Block style plugin manager.