You are here

class GridLayoutDiscovery in Paragraphs Collection 8

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.services.yml
paragraphs_collection.services.yml
1 service uses GridLayoutDiscovery
paragraphs_collection.grid_layout_discovery in ./paragraphs_collection.services.yml
\Drupal\paragraphs_collection\GridLayoutDiscovery

File

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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.