diff --git a/facets.services.yml b/facets.services.yml index c33a611..ac34ee9 100644 --- a/facets.services.yml +++ b/facets.services.yml @@ -19,9 +19,3 @@ services: - '@plugin.manager.facets.facet_source' - '@plugin.manager.facets.processor' - '@entity_type.manager' - - facets.facet_context: - class: Drupal\facets\ContextProvider\FacetContextProvider - arguments: ['@entity_type.manager'] - tags: - - { name: context_provider } diff --git a/src/ContextProvider/FacetContextProvider.php b/src/ContextProvider/FacetContextProvider.php deleted file mode 100644 index c663e5c..0000000 --- a/src/ContextProvider/FacetContextProvider.php +++ /dev/null @@ -1,72 +0,0 @@ -facetStorage = $entity_type_manager->getStorage('facets_facet'); - } - - /** - * {@inheritdoc} - */ - public function getRuntimeContexts(array $unqualified_context_ids = []) { - $ids = $this->facetStorage->getQuery() - ->condition('uuid', $unqualified_context_ids, 'IN') - ->execute(); - $contexts = []; - foreach ($this->facetStorage->loadMultiple($ids) as $facet) { - $context = new Context(new ContextDefinition('entity:facets_facet'), $facet); - $contexts[$facet->uuid()] = $context; - } - return $contexts; - } - - /** - * {@inheritdoc} - */ - public function getAvailableContexts() { - $facets = $this->facetStorage->loadMultiple(); - $contexts = []; - - /** @var \Drupal\facets\FacetInterface $facet */ - foreach ($facets as $facet) { - $context = new Context( - new ContextDefinition('entity:facets_facet', $facet->label()), - $facet - ); - $contexts[$facet->uuid()] = $context; - } - - return $contexts; - } - -} diff --git a/src/Plugin/Block/FacetBlock.php b/src/Plugin/Block/FacetBlock.php index bab4d4b..849d0df 100644 --- a/src/Plugin/Block/FacetBlock.php +++ b/src/Plugin/Block/FacetBlock.php @@ -3,8 +3,6 @@ /** * @file * Contains Drupal\facets\Plugin\Block\FacetBlock. - * - * NOTE: There should be a facetblock or settings for the facets later. */ namespace Drupal\facets\Plugin\Block; @@ -19,11 +17,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * Provides a 'FacetBlock' block. * * @Block( - * id = "facet_block", - * admin_label = @Translation("Facet block"), - * context = { - * "facet" = @ContextDefinition("entity:facets_facet", label=@Translation("Facet")) - * } + * id = "facet_block", + * deriver = "Drupal\facets\Plugin\block\FacetBlockDeriver" * ) */ class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface { @@ -72,8 +67,14 @@ class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface { * {@inheritdoc} */ public function build() { - /** @var Facet $facet */ - $facet = $this->getContextValue('facet'); + /** @var \Drupal\facets\FacetInterface $facet */ + $facet_mapping = $this->configuration['id']; + $facet_id = explode(':', $facet_mapping)[1]; + + $em = \Drupal::getContainer()->get('entity_type.manager'); + + /** @var \Drupal\facets\FacetInterface $facet */ + $facet = $em->getStorage('facets_facet')->load($facet_id); // Let the facet_manager build the facets. $build = $this->facetManager->build($facet); @@ -108,19 +109,13 @@ class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface { * {@inheritdoc} */ public function calculateDependencies() { - $facet_context_mapping = $this->configuration['context_mapping']['facet']; - $facet_id = explode(':', $facet_context_mapping)[1]; + $facet_mapping = $this->configuration['id']; + $facet_id = explode(':', $facet_mapping)[1]; $em = \Drupal::getContainer()->get('entity_type.manager'); /** @var \Drupal\facets\FacetInterface $facet */ - $facets = $em->getStorage('facets_facet') - ->loadByProperties(['uuid' => $facet_id]); - - $keys = array_keys($facets); - - $facet = $facets[$keys[0]]; - + $facet = $em->getStorage('facets_facet')->load($facet_id); $config_name = $facet->getConfigDependencyName(); return ['config' => [$config_name]]; diff --git a/src/Plugin/Block/FacetBlockDeriver.php b/src/Plugin/Block/FacetBlockDeriver.php new file mode 100644 index 0000000..a082946 --- /dev/null +++ b/src/Plugin/Block/FacetBlockDeriver.php @@ -0,0 +1,83 @@ +facetStorage = \Drupal::getContainer()->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 = array(); + $all_facets = $this->facetStorage->loadMultiple(); + + /** @var \Drupal\facets\FacetInterface $facet */ + 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' => $this->t('Facet block: :facet', [':facet' => $facet->getName()]), + 'description' => $this->t('Facet'), + ] + $base_plugin_definition; + + $sources[] = $this->t('Facet: :facet', [':facet' => $facet->getName()]); + + } + + $this->derivatives[$base_plugin_id] = $plugin_derivatives; + } + return $this->derivatives[$base_plugin_id]; + } +} diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index 808d856..dc8c55a 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -17,13 +17,6 @@ use \Drupal\facets\Tests\WebTestBase as FacetWebTestBase; class IntegrationTest extends FacetWebTestBase { /** - * The submitted block values used by this test. - * - * @var array - */ - protected $blockValues; - - /** * The block entities used by this test. * * @var \Drupal\block\BlockInterface[] @@ -242,25 +235,15 @@ class IntegrationTest extends FacetWebTestBase { protected function createFacetBlock($id) { // Create a block. Load the entity to obtain the uuid when creating the // block. - $facet = \Drupal::service('entity_type.manager')->getStorage('facets_facet')->load($id); - $this->blockValues = [ - [ - 'label' => 'Facet Block', - 'tr' => '16', - 'plugin_id' => 'facet_block', - 'settings' => [ - 'region' => 'footer', - 'id' => str_replace('_', '-', $id), - 'context_mapping' => [ - 'facet' => '@facets.facet_context:' . $facet->uuid(), - ], - ], - 'test_weight' => '0', - ], + + $block = [ + 'plugin_id' => 'facet_block:' . $id, + 'settings' => [ + 'region' => 'footer', + 'id' => str_replace('_', '-', $id), + ] ]; - foreach ($this->blockValues as $values) { - $this->blocks[$id] = $this->drupalPlaceBlock($values['plugin_id'], $values['settings']); - } + $this->blocks[$id] = $this->drupalPlaceBlock($block['plugin_id'], $block['settings']); } /**