diff --git a/src/Plugin/Condition/OtherFacet.php b/src/Plugin/Condition/OtherFacet.php index 1ec38d3..378c919 100644 --- a/src/Plugin/Condition/OtherFacet.php +++ b/src/Plugin/Condition/OtherFacet.php @@ -2,11 +2,13 @@ /** * @file - * Contains \Drupal\facetapi\Plugin\Condition\OtherFacet. + * Contains \Drupal\facets\Plugin\Condition\OtherFacet. */ -namespace Drupal\facetapi\Plugin\Condition; +namespace Drupal\facets\Plugin\Condition; +use Drupal\Component\Plugin\PluginBase; +use Drupal\Core\Block\BlockManager; use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; @@ -20,7 +22,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * id = "other_facet", * label = @Translation("Other facet"), * ) - * */ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginInterface { @@ -29,13 +30,22 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn * * @var \Drupal\Core\Entity\EntityStorageInterface */ - protected $entityStorage; + protected $facetStorage; + + /** + * The block plugin manager. + * + * @var \Drupal\Core\Block\BlockManager + */ + protected $blockManager; /** * Creates a new instance of the condition. * * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage * The entity storage. + * @param \Drupal\Core\Block\BlockManager $block_manager + * The block plugin manager. * @param array $configuration * The plugin configuration, i.e. an array with configuration values keyed * by configuration option name. The special key 'context' may be used to @@ -46,9 +56,10 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn * @param mixed $plugin_definition * The plugin implementation definition. */ - public function __construct(EntityStorageInterface $entity_storage, array $configuration, $plugin_id, $plugin_definition) { + public function __construct(EntityStorageInterface $entity_storage, BlockManager $block_manager, array $configuration, $plugin_id, $plugin_definition) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->entityStorage = $entity_storage; + $this->facetStorage = $entity_storage; + $this->blockManager = $block_manager; } /** @@ -56,7 +67,8 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( - $container->get('entity_type.manager')->getStorage('facetapi_facet'), + $container->get('entity_type.manager')->getStorage('facets_facet'), + $container->get('plugin.manager.block'), $configuration, $plugin_id, $plugin_definition @@ -68,13 +80,15 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $options = []; - $facets = $this->entityStorage->loadMultiple(); - foreach ($facets as $facet) { - $options[$facet->id()] = $facet->label(); + + foreach ($this->blockManager->getDefinitions() as $definition) { + if ($definition['provider'] == 'facets') { + $options[$definition['id']] = $definition['label']; + } } $form['facets'] = [ - '#title' => $this->t('Other facets'), + '#title' => $this->t('Other facet blocks'), '#type' => 'radios', '#options' => $options, '#default_value' => $this->configuration['facets'], @@ -85,6 +99,7 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn '#type' => 'textfield', '#default_value' => $this->configuration['facet_value'], ]; + return parent::buildConfigurationForm($form, $form_state); } @@ -109,24 +124,35 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn * {@inheritdoc} */ public function evaluate() { - if (empty($this->configuration['facets']) && !$this->isNegated()) { + $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; } - // Check if the required facet is also rendered on the same page. Even - // though all we have currently is a link to the facet trough it's id in - // $this->configuration['facets']. - // We should probably have a link to the facet's display (block, panel,...) - // or load those based on the facet id. - // - // Depending on the return value of that check, we should return TRUE (if - // we're allowed to render) or FALSE if rendering is not allowed. - // - // if $this->configuration['facet_value'] is filled in and we would return - // TRUE in the description above. We first have to check in the facet if the - // "facet_value" is set to active. This should be fairly trivial and can be - // done by iterating over the active results from the facet - // (Facet::getActiveItems). + /** @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)) { + return $block_plugin->access(\Drupal::currentUser()); + } + + $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; } @@ -135,7 +161,8 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn * {@inheritdoc} */ public function defaultConfiguration() { - return ['facets' => []] + parent::defaultConfiguration(); + $config = ['facets' => FALSE, 'facet_value' => FALSE]; + return $config + parent::defaultConfiguration(); } }