class GridLayoutDiscovery

Provides common helper methods for style discovery.

@todo Add documentation in the api file.

Hierarchy

Expanded class hierarchy of GridLayoutDiscovery

1 string reference to 'GridLayoutDiscovery'
paragraphs_collection.services.yml in paragraphs_collection/paragraphs_collection.services.yml
paragraphs_collection/paragraphs_collection.services.yml

File

paragraphs_collection/src/GridLayoutDiscovery.php, line 16

Namespace

Drupal\paragraphs_collection
View source
class GridLayoutDiscovery implements GridLayoutDiscoveryInterface {
  use StringTranslationTrait;

  /**
   * Collection of styles with its definition.
   *
   * @var array
   */
  protected $gridLayoutsCollection = [];

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

  /**
   * The controller resolver.
   *
   * @var \Drupal\Core\Controller\ControllerResolverInterface
   */
  protected $controllerResolver;

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

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

  /**
   * Constructs a new YamlStyleDiscovery.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   The cache backend.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler.
   */
  public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, ThemeHandlerInterface $theme_handler) {
    $this->moduleHandler = $module_handler;
    $this->themeHandler = $theme_handler;
    $this->cache = $cache_backend;
  }

  /**
   * {@inheritdoc}
   */
  public function getLibraries($layout) {
    $collection = $this
      ->getGridLayouts();
    return isset($collection[$layout]['libraries']) ? $collection[$layout]['libraries'] : [];
  }

  /**
   * {@inheritdoc}
   */
  public function getGridLayouts() {
    $cid = 'paragraphs_collection_grid_layouts';
    if ($this->gridLayoutsCollection) {
      return $this->gridLayoutsCollection;
    }
    else {
      if ($cached = $this->cache
        ->get($cid)) {
        $this->gridLayoutsCollection = $cached->data;
      }
      else {
        $yaml_discovery = $this
          ->getYamlDiscovery();
        $this->gridLayoutsCollection = [];
        foreach ($yaml_discovery
          ->findAll() as $provider => $layouts) {
          foreach ($layouts as $layout => $definition) {
            if (empty($definition['title'])) {
              throw new InvalidGridLayoutException('The "title" of "' . $layout . '" must be non-empty.');
            }
            $definition['title'] = $this
              ->t($definition['title']);
            if (!empty($definition['description'])) {
              $definition['description'] = $this
                ->t($definition['description']);
            }
            $definition['provider'] = $provider;
            $this->gridLayoutsCollection[$layout] = $definition;
          }
        }
        $this->cache
          ->set($cid, $this->gridLayoutsCollection);
      }
    }
    return $this->gridLayoutsCollection;
  }

  /**
   * {@inheritdoc}
   */
  public function getLayoutOptions() {
    $layout_options = [];
    $layouts = $this
      ->getGridLayouts();
    foreach ($layouts as $name => $layout) {
      $layout_options[$name] = $layout['title'];
    }
    uasort($layout_options, 'strcasecmp');
    return $layout_options;
  }

  /**
   * {@inheritdoc}
   */
  public function getLayout($layout) {
    $layouts = $this
      ->getGridLayouts();
    if (isset($layouts[$layout])) {
      return $layouts[$layout];
    }
    return [];
  }

  /**
   * Gets the intitiated YAML discovery.
   *
   * @return \Drupal\Core\Discovery\YamlDiscovery
   *   The YAML discovery object.
   */
  protected function getYamlDiscovery() {
    return new YamlDiscovery('paragraphs.grid_layouts', $this->moduleHandler
      ->getModuleDirectories() + $this->themeHandler
      ->getThemeDirectories());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GridLayoutDiscovery::$cache protected property The cache backend.
GridLayoutDiscovery::$controllerResolver protected property The controller resolver.
GridLayoutDiscovery::$gridLayoutsCollection protected property Collection of styles with its definition.
GridLayoutDiscovery::$moduleHandler protected property The module handler.
GridLayoutDiscovery::$themeHandler protected property The theme handler.
GridLayoutDiscovery::getGridLayouts public function Get defined grid layouts. Overrides GridLayoutDiscoveryInterface::getGridLayouts
GridLayoutDiscovery::getLayout public function Get layout by layout name. Overrides GridLayoutDiscoveryInterface::getLayout
GridLayoutDiscovery::getLayoutOptions public function Gets sorted grid layout titles keyed by their machine names. Overrides GridLayoutDiscoveryInterface::getLayoutOptions
GridLayoutDiscovery::getLibraries public function Get a list of library names for the given layout. Overrides GridLayoutDiscoveryInterface::getLibraries
GridLayoutDiscovery::getYamlDiscovery protected function Gets the intitiated YAML discovery.
GridLayoutDiscovery::__construct public function Constructs a new YamlStyleDiscovery.