diff --git a/src/Plugin/Condition/OtherFacet.php b/src/Plugin/Condition/OtherFacet.php new file mode 100644 index 0000000..ebd4ee0 --- /dev/null +++ b/src/Plugin/Condition/OtherFacet.php @@ -0,0 +1,189 @@ +facetStorage = $entity_storage; + $this->blockManager = $block_manager; + $this->currentUser = $current_user; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $container->get('entity_type.manager')->getStorage('facets_facet'), + $container->get('plugin.manager.block'), + $container->get('current_user'), + $configuration, + $plugin_id, + $plugin_definition + ); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $options = []; + + foreach ($this->blockManager->getDefinitions() as $definition) { + if ($definition['provider'] == 'facets') { + $options[$definition['id']] = $definition['label']; + } + } + + $form['facets'] = [ + '#title' => $this->t('Other facet blocks'), + '#type' => 'radios', + '#options' => $options, + '#default_value' => $this->configuration['facets'], + ]; + $form['facet_value'] = [ + '#title' => $this->t('Facet value'), + '#description' => $this->t('Only applies when a facet is already selected.'), + '#type' => 'textfield', + '#default_value' => $this->configuration['facet_value'], + ]; + + return parent::buildConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['facets'] = $form_state->getValue('facets'); + $this->configuration['facet_value'] = $form_state->getValue('facet_value'); + parent::submitConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function summary() { + $facet = reset($this->configuration['facets']); + return $this->t('The facet is @facet also rendered on the same page.', ['@facet' => $facet]); + } + + /** + * {@inheritdoc} + */ + public function evaluate() { + $allowed_facet_value = $this->configuration['facet_value']; + $allowed_facets = $this->configuration['facets']; + + // Return as early as possible when there are no settings for allowed + // facets. + if (empty($allowed_facets) && !$this->isNegated()) { + return TRUE; + } + if (empty($allowed_facets) && $this->isNegated()) { + return FALSE; + } + + /** @var \Drupal\facets\Plugin\Block\FacetBlock $block_plugin */ + $block_plugin = $this->blockManager->createInstance($allowed_facets); + + // Allowed facet value is not set, so we only have to check if the block is + // shown here. + if (empty($allowed_facet_value)) { + $access_result = $block_plugin->access($this->currentUser); + if ($this->isNegated()) { + // A negated configuration should return the inverse of the normal + // result. + return !($access_result); + } + return $access_result; + } + + $block_plugin_id = $block_plugin->getPluginId(); + $facet_id = explode(PluginBase::DERIVATIVE_SEPARATOR, $block_plugin_id)[1]; + + /** @var \Drupal\facets\FacetInterface $facet */ + $facet = $this->facetStorage->load($facet_id); + + foreach ($facet->getResults() as $result) { + if (($result->getRawValue() == $allowed_facet_value || $result->getDisplayValue() == $allowed_facet_value) && $result->isActive()) { + return TRUE; + } + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + $config = ['facets' => FALSE, 'facet_value' => FALSE]; + return $config + parent::defaultConfiguration(); + } + +} diff --git a/tests/src/Unit/Plugin/Condition/OtherFacetTest.php b/tests/src/Unit/Plugin/Condition/OtherFacetTest.php new file mode 100644 index 0000000..92e02aa --- /dev/null +++ b/tests/src/Unit/Plugin/Condition/OtherFacetTest.php @@ -0,0 +1,113 @@ +getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager') + ->disableOriginalConstructor() + ->getMock(); + $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $sut = new OtherFacet($storage, $manager, $user, ['negate' => $negated], 'other_facet', ''); + + $evaluation = $sut->evaluate(); + if ($negated) { + $this->assertFalse($evaluation); + } + else { + $this->assertTrue($evaluation); + } + } + + /** + * Displayed facet. + * + * @dataProvider provideNegated + */ + public function testDisplayedFacet($negated) { + $block = $this->getMockBuilder('\Drupal\facets\Plugin\Block\FacetBlock') + ->disableOriginalConstructor() + ->getMock(); + $block->expects($this->exactly(1)) + ->method('access') + ->willReturn(TRUE); + $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager') + ->disableOriginalConstructor() + ->getMock(); + $manager->expects($this->exactly(1)) + ->method('createInstance') + ->willReturn($block); + $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $sut = new OtherFacet($storage, $manager, $user, ['facets' => 'test', 'negate' => $negated], 'other_facet', ''); + + $evaluation = $sut->evaluate(); + if ($negated) { + $this->assertFalse($evaluation); + } + else { + $this->assertTrue($evaluation); + } + } + + /** + * Hidden facet. + * + * @dataProvider provideNegated + */ + public function testHiddenFacet($negated) { + $block = $this->getMockBuilder('\Drupal\facets\Plugin\Block\FacetBlock') + ->disableOriginalConstructor() + ->getMock(); + $block->expects($this->exactly(1)) + ->method('access') + ->willReturn(FALSE); + $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager') + ->disableOriginalConstructor() + ->getMock(); + $manager->expects($this->exactly(1)) + ->method('createInstance') + ->willReturn($block); + $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $sut = new OtherFacet($storage, $manager, $user, ['facets' => 'test', 'negate' => $negated], 'other_facet', ''); + + $evaluation = $sut->evaluate(); + if ($negated) { + $this->assertTrue($evaluation); + } + else { + $this->assertFalse($evaluation); + } + } + + /** + * @return array + */ + public function provideNegated() { + return [ + 'normal' => [FALSE], + 'negated' => [TRUE], + ]; + } +}