diff --git a/src/Entity/Facet.php b/src/Entity/Facet.php index 889a074..54dc338 100644 --- a/src/Entity/Facet.php +++ b/src/Entity/Facet.php @@ -252,6 +252,14 @@ class Facet extends ConfigEntityBase implements FacetInterface { /** * {@inheritdoc} */ + public function getQueryOperator() { + return $this->getOption('query_operator', 'OR'); + } + + + /** + * {@inheritdoc} + */ public function getFieldAlias() { // For now, create the field alias based on the field identifier. $field_alias = preg_replace('/[:\/]+/', '_', $this->field_identifier); diff --git a/src/FacetInterface.php b/src/FacetInterface.php index 7e39f2a..fdbdc80 100644 --- a/src/FacetInterface.php +++ b/src/FacetInterface.php @@ -122,6 +122,14 @@ interface FacetInterface extends ConfigEntityInterface { public function getQueryType(); /** + * Get the query operator. + * + * @return string + * The query operator being used. + */ + public function getQueryOperator(); + + /** * Get the plugin name for the url processor. * * @return string diff --git a/src/Form/FacetDisplayForm.php b/src/Form/FacetDisplayForm.php index d55dfe1..e316fa1 100644 --- a/src/Form/FacetDisplayForm.php +++ b/src/Form/FacetDisplayForm.php @@ -352,6 +352,15 @@ class FacetDisplayForm extends EntityForm { '#default_value' => isset($empty_behavior_config['text_format']) ? $empty_behavior_config['text'] : '', ]; + // Query operator. + $form['facet_settings']['query_operator'] = [ + '#type' => 'radios', + '#title' => $this->t('Operator'), + '#options' => ['OR' => $this->t('OR'), 'AND' => $this->t('AND')], + '#description' => $this->t('AND filters are exclusive and narrow the result set. OR filters are inclusive and widen the result set.'), + '#default_value' => $facet->getQueryOperator(), + ]; + $form['weights'] = array( '#type' => 'details', '#title' => t('Advanced settings'), @@ -529,6 +538,9 @@ class FacetDisplayForm extends EntityForm { } $facet->setOption('empty_behavior', $empty_behavior_config); + //Query operator + $facet->setOption('query_operator', $form_state->getValue(['facet_settings', 'query_operator'])); + $facet->save(); drupal_set_message(t('Facet %name has been updated.', ['%name' => $facet->getName()])); } diff --git a/src/Plugin/facets/query_type/SearchApiString.php b/src/Plugin/facets/query_type/SearchApiString.php index c44d0bb..fab8010 100644 --- a/src/Plugin/facets/query_type/SearchApiString.php +++ b/src/Plugin/facets/query_type/SearchApiString.php @@ -44,6 +44,7 @@ class SearchApiString extends QueryTypePluginBase { // Alter the query here. if (!empty($query)) { $options = &$query->getOptions(); + $operator = $this->facet->getQueryOperator(); $field_identifier = $this->facet->getFieldIdentifier(); $options['search_api_facets'][$field_identifier] = array( @@ -56,12 +57,13 @@ class SearchApiString extends QueryTypePluginBase { // Add the filter to the query if there are active values. $active_items = $this->facet->getActiveItems(); + if (count($active_items)) { + $filter = $query->createConditionGroup($operator); foreach ($active_items as $value) { - $filter = $query->createConditionGroup(); $filter->addCondition($this->facet->getFieldIdentifier(), $value); - $query->addConditionGroup($filter); } + $query->addConditionGroup($filter); } } } diff --git a/src/Plugin/facets/widget/CheckboxWidget.php b/src/Plugin/facets/widget/CheckboxWidget.php index 6d8bf49..1ab2bf6 100644 --- a/src/Plugin/facets/widget/CheckboxWidget.php +++ b/src/Plugin/facets/widget/CheckboxWidget.php @@ -46,32 +46,39 @@ class CheckboxWidget implements WidgetInterface { public function build(FacetInterface $facet) { /** @var \Drupal\facets\Result\Result[] $results */ $results = $facet->getResults(); - $items = []; + $form = array(); $configuration = $facet->get('widget_configs'); $show_numbers = (bool) $configuration['show_numbers']; + $options = []; foreach ($results as $result) { - if ($result->getCount()) { - // Get the link. - $text = $result->getDisplayValue(); - if ($show_numbers) { - $text .= ' (' . $result->getCount() . ')'; - } - if ($result->isActive()) { - $text = '(-) ' . $text; - } - $link = $this->linkGenerator()->generate($text, $result->getUrl()); - $items[] = $link; + // Get the link. + $text = $result->getDisplayValue(); + + if ($show_numbers) { + $text .= ' (' . $result->getCount() . ')'; + } + + $url = $result->getUrl()->toString(); + $form[$url] = array( + '#type' => 'checkbox', + '#title' => '' . $text . '', + ); + + if ($result->isActive()) { + $form[$url]['#attributes']['checked'] = 'checked'; } } - $build = [ - '#theme' => 'item_list', - '#items' => $items, - ]; - $build['#prefix'] = $this->t('Checkboxes'); - return $build; + return $form; + } + + /** + * {@inheritdoc} + */ + public function getQueryType($query_types) { + return $query_types['string']; } /** @@ -95,13 +102,6 @@ class CheckboxWidget implements WidgetInterface { } /** - * {@inheritdoc} - */ - public function getQueryType($query_types) { - return $query_types['string']; - } - - /** * Gets the link generator. * * @return \Drupal\Core\Utility\LinkGeneratorInterface