diff --git a/src/Plugin/Condition/OtherFacet.php b/src/Plugin/Condition/OtherFacet.php new file mode 100644 index 0000000..1ec38d3 --- /dev/null +++ b/src/Plugin/Condition/OtherFacet.php @@ -0,0 +1,141 @@ +entityStorage = $entity_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $container->get('entity_type.manager')->getStorage('facetapi_facet'), + $configuration, + $plugin_id, + $plugin_definition + ); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $options = []; + $facets = $this->entityStorage->loadMultiple(); + foreach ($facets as $facet) { + $options[$facet->id()] = $facet->label(); + } + + $form['facets'] = [ + '#title' => $this->t('Other facets'), + '#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() { + if (empty($this->configuration['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). + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return ['facets' => []] + parent::defaultConfiguration(); + } + +}