You are here

class FacetBlockDeriver in Facets 8

This deriver creates a block for every facet that has been created.

Hierarchy

Expanded class hierarchy of FacetBlockDeriver

File

src/Plugin/Block/FacetBlockDeriver.php, line 13

Namespace

Drupal\facets\Plugin\Block
View source
class FacetBlockDeriver implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * List of derivative definitions.
   *
   * @var array
   */
  protected $derivatives = [];

  /**
   * The entity storage used for facets.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $facetStorage;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    $deriver = new static($container, $base_plugin_id);
    $deriver->facetStorage = $container
      ->get('entity_type.manager')
      ->getStorage('facets_facet');
    return $deriver;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
    $derivatives = $this
      ->getDerivativeDefinitions($base_plugin_definition);
    return isset($derivatives[$derivative_id]) ? $derivatives[$derivative_id] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $base_plugin_id = $base_plugin_definition['id'];
    if (!isset($this->derivatives[$base_plugin_id])) {
      $plugin_derivatives = [];

      /** @var \Drupal\facets\FacetInterface[] $all_facets */
      $all_facets = $this->facetStorage
        ->loadMultiple();
      foreach ($all_facets as $facet) {
        $machine_name = $facet
          ->id();
        $plugin_derivatives[$machine_name] = [
          'id' => $base_plugin_id . PluginBase::DERIVATIVE_SEPARATOR . $machine_name,
          'label' => $this
            ->t('Facet: :facet', [
            ':facet' => $facet
              ->getName(),
          ]),
          'admin_label' => $facet
            ->getName(),
          'description' => $this
            ->t('Facet'),
        ] + $base_plugin_definition;
      }
      $this->derivatives[$base_plugin_id] = $plugin_derivatives;
    }
    return $this->derivatives[$base_plugin_id];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacetBlockDeriver::$derivatives protected property List of derivative definitions.
FacetBlockDeriver::$facetStorage protected property The entity storage used for facets.
FacetBlockDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
FacetBlockDeriver::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
FacetBlockDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverInterface::getDerivativeDefinitions
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.