diff --git a/src/Plugin/Block/FacetBlock.php b/src/Plugin/Block/FacetBlock.php index 849d0df..c353e0b 100644 --- a/src/Plugin/Block/FacetBlock.php +++ b/src/Plugin/Block/FacetBlock.php @@ -10,6 +10,7 @@ namespace Drupal\facets\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Plugin\PluginBase; use Drupal\facets\FacetManager\DefaultFacetManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -24,7 +25,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface { /** - * The facet_manager plugin manager. + * The facet manager service. * * @var DefaultFacetManager */ @@ -67,12 +68,13 @@ class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface { * {@inheritdoc} */ public function build() { - /** @var \Drupal\facets\FacetInterface $facet */ - $facet_mapping = $this->configuration['id']; - $facet_id = explode(':', $facet_mapping)[1]; - $em = \Drupal::getContainer()->get('entity_type.manager'); + // The id saved in the configuration is in the format of + // base_plugin:facet_id. We're splitting that to get to the facet id. + $facet_mapping = $this->configuration['id']; + $facet_id = explode(PluginBase::DERIVATIVE_SEPARATOR, $facet_mapping)[1]; + /** @var \Drupal\facets\FacetInterface $facet */ $facet = $em->getStorage('facets_facet')->load($facet_id); @@ -109,11 +111,13 @@ class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface { * {@inheritdoc} */ public function calculateDependencies() { - $facet_mapping = $this->configuration['id']; - $facet_id = explode(':', $facet_mapping)[1]; - $em = \Drupal::getContainer()->get('entity_type.manager'); + // The id saved in the configuration is in the format of + // base_plugin:facet_id. We're splitting that to get to the facet id. + $facet_mapping = $this->configuration['id']; + $facet_id = explode(PluginBase::DERIVATIVE_SEPARATOR, $facet_mapping)[1]; + /** @var \Drupal\facets\FacetInterface $facet */ $facet = $em->getStorage('facets_facet')->load($facet_id); $config_name = $facet->getConfigDependencyName(); diff --git a/src/Plugin/Block/FacetBlockDeriver.php b/src/Plugin/Block/FacetBlockDeriver.php index a082946..e410f3d 100644 --- a/src/Plugin/Block/FacetBlockDeriver.php +++ b/src/Plugin/Block/FacetBlockDeriver.php @@ -13,7 +13,7 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a 'FacetBlock' block. + * This deriver creates a block for every facet that has been created. */ class FacetBlockDeriver implements ContainerDeriverInterface { @@ -27,9 +27,11 @@ class FacetBlockDeriver implements ContainerDeriverInterface { protected $derivatives = array(); /** + * The entity storage used for facets. + * * @var \Drupal\Core\Entity\EntityStorageInterface $facetStorage */ - public $facetStorage; + protected $facetStorage; /** * {@inheritdoc} @@ -57,11 +59,11 @@ class FacetBlockDeriver implements ContainerDeriverInterface { $base_plugin_id = $base_plugin_definition['id']; if (!isset($this->derivatives[$base_plugin_id])) { - $plugin_derivatives = array(); + + /** @var \Drupal\facets\FacetInterface[] $all_facets */ $all_facets = $this->facetStorage->loadMultiple(); - /** @var \Drupal\facets\FacetInterface $facet */ foreach ($all_facets as $facet) { $machine_name = $facet->id(); @@ -73,7 +75,6 @@ class FacetBlockDeriver implements ContainerDeriverInterface { ] + $base_plugin_definition; $sources[] = $this->t('Facet: :facet', [':facet' => $facet->getName()]); - } $this->derivatives[$base_plugin_id] = $plugin_derivatives;