You are here

public function Facet::getQueryType in Facets 8

Returns the query type instance.

Return value

string The query type plugin being used.

Overrides FacetInterface::getQueryType

File

src/Entity/Facet.php, line 456

Class

Facet
Defines the facet configuration entity.

Namespace

Drupal\facets\Entity

Code

public function getQueryType() {
  $facet_source = $this
    ->getFacetSource();
  if (is_null($facet_source)) {
    throw new Exception("No facet source defined for facet.");
  }
  $query_types = $facet_source
    ->getQueryTypesForFacet($this);

  // Get the widget configured for this facet.

  /** @var \Drupal\facets\Widget\WidgetPluginInterface $widget */
  $widget = $this
    ->getWidgetInstance();

  // Give the widget the chance to select a preferred query type. This is
  // needed for widget that have different query type. For example the need
  // for a range query.
  $widgetQueryType = $widget
    ->getQueryType();

  // Allow widgets to also specify a query type.
  $processorQueryTypes = [];
  foreach ($this
    ->getProcessors() as $processor) {
    $pqt = $processor
      ->getQueryType();
    if ($pqt !== NULL) {
      $processorQueryTypes[] = $pqt;
    }
  }
  $processorQueryTypes = array_flip($processorQueryTypes);

  // The widget has made no decision and neither have the processors.
  if ($widgetQueryType === NULL && count($processorQueryTypes) === 0) {
    return $this
      ->pickQueryType($query_types, 'string');
  }

  // The widget has made no decision but the processors have made 1 decision.
  if ($widgetQueryType === NULL && count($processorQueryTypes) === 1) {
    return $this
      ->pickQueryType($query_types, key($processorQueryTypes));
  }

  // The widget has made a decision and the processors have not.
  if ($widgetQueryType !== NULL && count($processorQueryTypes) === 0) {
    return $this
      ->pickQueryType($query_types, $widgetQueryType);
  }

  // The widget has made a decision and the processors have 1, being the same.
  if ($widgetQueryType !== NULL && count($processorQueryTypes) === 1 && key($processorQueryTypes) === $widgetQueryType) {
    return $this
      ->pickQueryType($query_types, $widgetQueryType);
  }

  // Invalid choice.
  throw new InvalidQueryTypeException("Invalid query type combination in widget / processors. Widget: {$widgetQueryType}, Processors: " . implode(', ', array_keys($processorQueryTypes)) . ".");
}