You are here

class JsonApiQueryString in JSON:API Search API 8

Query string URL processor.

Plugin annotation


@FacetsUrlProcessor(
  id = "json_api",
  label = @Translation("JSON:API Query string"),
  description = @Translation("Process facets generating in JSON:API filter syntax.")
)

Hierarchy

Expanded class hierarchy of JsonApiQueryString

File

modules/jsonapi_search_api_facets/src/Plugin/facets/url_processor/JsonApiQueryString.php, line 21

Namespace

Drupal\jsonapi_search_api_facets\Plugin\facets\url_processor
View source
class JsonApiQueryString extends QueryString {

  /**
   * The query string variable.
   *
   * @var string
   *   The query string variable that holds all the facet information.
   */
  protected $filterKey = Filter::KEY_NAME;

  /**
   * An array of the existing non-facet filters.
   *
   * @var array
   *  The filters.
   */
  protected $originalFilters = [];

  /**
   * {@inheritDoc}
   */
  protected function initializeActiveFilters() {
    $url_parameters = $this->request->query;

    // Get the active facet parameters.
    // @todo can we leverage \Drupal\jsonapi_search_api\Query\Filter
    $active_params = $url_parameters
      ->get($this->filterKey, []);
    $facet_source_id = $this->configuration['facet']
      ->getFacetSourceId();

    // When an invalid parameter is passed in the url, we can't do anything.
    if (!is_array($active_params)) {
      return;
    }
    foreach ($active_params as $param_identifier => $param_value) {

      // Check if this is a complex filter definition.
      if (is_array($param_value)) {
        if (isset($param_value['condition'])) {
          $facet_id = $this
            ->getFacetIdByUrlAlias($param_value['condition']['path'], $facet_source_id);

          // Skip filters that do not target facets.
          if ($facet_id === NULL) {
            $this->originalFilters[$param_identifier] = $param_value;
            continue;
          }
          $this->activeFilters[$facet_id] = [];
          if (isset($param_value['condition']['value'])) {
            foreach ($param_value['condition']['value'] as $condition_value) {
              $this->activeFilters[$facet_id][] = $condition_value;
            }
          }
        }
        else {
          $this->originalFilters[$param_identifier] = $param_value;
        }
      }
      else {

        // Shorthand filter notation.
        $facet_id = $this
          ->getFacetIdByUrlAlias($param_identifier, $facet_source_id);
        if ($facet_id === NULL) {
          $this->originalFilters[$param_identifier] = $param_value;
        }
        else {
          $this->activeFilters[$facet_id] = $param_value;
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setActiveItems(FacetInterface $facet) {

    // Get the filter key of the facet.
    if (isset($this->activeFilters[$facet
      ->id()])) {
      $active_filters = $this->activeFilters[$facet
        ->id()];
      if (is_array($active_filters)) {
        $facet
          ->setActiveItems($active_filters);
      }
      else {
        $facet
          ->setActiveItem($active_filters);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildUrls(FacetInterface $facet, array $results) {

    // No results are found for this facet, so don't try to create urls.
    if (empty($results)) {
      return [];
    }
    $facet_source = $facet
      ->getFacetSource();

    // This URL processor only works with JSON:API Facets.
    if (!$facet_source instanceof JsonApiFacets) {
      return [];
    }

    // First get the current list of get parameters.
    $get_params = $this->request->query;

    // When adding/removing a filter the number of pages may have changed,
    // possibly resulting in an invalid page parameter.
    if ($get_params
      ->has(OffsetPage::KEY_NAME)) {
      $page_params = $get_params
        ->get(OffsetPage::KEY_NAME);
      unset($page_params[OffsetPage::OFFSET_KEY]);
      $get_params
        ->set(OffsetPage::KEY_NAME, $page_params);
    }

    // Set the url alias from the facet object.
    $this->urlAlias = $facet
      ->getUrlAlias();
    $facet_source_path = $facet
      ->getFacetSource()
      ->getPath();
    $request = $this
      ->getRequestByFacetSourcePath($facet_source_path);
    $requestUrl = $this
      ->getUrlForRequest($facet_source_path, $request);
    $original_filter_params = [];
    foreach ($this
      ->getActiveFilters() as $facet_id => $value) {
      $facet_url_alias = $this
        ->getUrlAliasByFacetId($facet_id, $facet
        ->getFacetSourceId());
      $this
        ->addToFilter($facet_url_alias, $value, $original_filter_params);
    }
    foreach ($results as &$result) {

      // Reset the URL for each result.
      $url = clone $requestUrl;

      // Sets the url for children.
      if ($children = $result
        ->getChildren()) {
        $this
          ->buildUrls($facet, $children);
      }
      $filter_string = $result
        ->getRawValue();
      $result_get_params = clone $get_params;
      $filter_params = $original_filter_params;
      if ($filter_string !== NULL) {
        $this
          ->addToFilter($this->urlAlias, $filter_string, $filter_params);
      }
      if ($result
        ->isActive()) {

        // Facets' itself does not allow hierarchical settings to be configured
        // on anything that's not a view as of current. With that in mind this
        // code -will- still operate like the parent it inherits from if the
        // bogus check is bypassed.
        // @see: https://www.drupal.org/project/facets/issues/3157720
        if ($facet
          ->getEnableParentWhenChildGetsDisabled() && $facet
          ->getUseHierarchy()) {

          // Enable parent id again if exists.
          $parent_ids = $facet
            ->getHierarchyInstance()
            ->getParentIds($result
            ->getRawValue());
          if (isset($parent_ids[0]) && $parent_ids[0]) {

            // Get the parents children.
            $child_ids = $facet
              ->getHierarchyInstance()
              ->getNestedChildIds($parent_ids[0]);

            // Check if there are active siblings.
            $active_sibling = FALSE;
            if ($child_ids) {
              foreach ($results as $result2) {
                if ($result2
                  ->isActive() && $result2
                  ->getRawValue() != $result
                  ->getRawValue() && in_array($result2
                  ->getRawValue(), $child_ids)) {
                  $active_sibling = TRUE;
                  continue;
                }
              }
            }
            if (!$active_sibling) {
              $this
                ->addToFilter($this->urlAlias, $parent_ids[0], $filter_params);
            }
          }
        }
      }
      else {
        if ($facet
          ->getUseHierarchy()) {

          // If hierarchy is active, unset parent trail and every child when
          // building the enable-link to ensure those are not enabled anymore.
          $parent_ids = $facet
            ->getHierarchyInstance()
            ->getParentIds($result
            ->getRawValue());
          $child_ids = $facet
            ->getHierarchyInstance()
            ->getNestedChildIds($result
            ->getRawValue());
          $parents_and_child_ids = array_merge($parent_ids, $child_ids);
          foreach ($parents_and_child_ids as $id) {
            $filter_params = array_diff($filter_params, [
              $id,
            ]);
          }
        }

        // Exclude currently active results from the filter params if we are in
        // the show_only_one_result mode.
        if ($facet
          ->getShowOnlyOneResult()) {
          foreach ($results as $result2) {
            if ($result2
              ->isActive()) {
              $active_filter_string = $result2
                ->getRawValue();
              foreach ($filter_params as $filter_key => $filter_values) {
                if (in_array($active_filter_string, $filter_values)) {
                  $filter_params[$filter_key] = array_diff($filter_values, [
                    $active_filter_string,
                  ]);
                }
              }
            }
          }
        }
      }

      // Rewrite into JSON:API style filters now, start by removing all
      // existing filters as they will be re-created.
      $result_get_params
        ->remove('filter');
      $result_get_params
        ->set('filter', $this->originalFilters);
      foreach ($filter_params as $filter_path => $values) {
        $existing_params = $result_get_params
          ->all();

        // Simple facets can be done.
        if (count($values) === 1) {
          $value = reset($values);
          $params = array_merge_recursive($existing_params, [
            'filter' => [
              $filter_path => $value,
            ],
          ]);
        }
        else {

          // Build up "fancy" facets, handle other ops (NOT)?.
          $filter_key = strtr('!field-facet', [
            '!field' => $filter_path,
          ]);
          $params = array_merge_recursive($existing_params, [
            'filter' => [
              $filter_key => [
                'condition' => [
                  'path' => $filter_path,
                  'operator' => 'IN',
                  'value' => [],
                ],
              ],
            ],
          ]);
          foreach ($values as $value) {
            $params['filter'][$filter_key]['condition']['value'][] = $value;
          }
        }
        $result_get_params
          ->add($params);
      }
      if ($result_get_params
        ->all() !== [
        $this->filterKey => [],
      ]) {
        $new_url_params = $result_get_params
          ->all();

        // Set the new url parameters.
        $url
          ->setOption('query', $new_url_params);
      }
      $result
        ->setUrl($url);
    }
    return $results;
  }

  /**
   * Adds a facet value to the active filter parameters.
   *
   * @param string $key
   *   The filter key.
   * @param string|array $values
   *   The value.
   * @param array $filter_params
   *   The filter params.
   */
  private function addToFilter($key, $values, array &$filter_params) {
    if (!isset($filter_params[$key])) {
      $filter_params[$key] = [];
    }
    if (!is_array($values)) {
      $values = [
        $values,
      ];
    }
    foreach ($values as $value) {
      if (!in_array($value, $filter_params[$key], TRUE)) {
        $filter_params[$key][] = $value;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
JsonApiQueryString::$filterKey protected property The query string variable. Overrides UrlProcessorPluginBase::$filterKey
JsonApiQueryString::$originalFilters protected property An array of the existing non-facet filters.
JsonApiQueryString::addToFilter private function Adds a facet value to the active filter parameters.
JsonApiQueryString::buildUrls public function Adds urls to the results. Overrides QueryString::buildUrls
JsonApiQueryString::initializeActiveFilters protected function Initializes the active filters from the request query. Overrides QueryString::initializeActiveFilters
JsonApiQueryString::setActiveItems public function Sets active items. Overrides UrlProcessorPluginBase::setActiveItems
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProcessorInterface::STAGE_BUILD constant Processing stage: build.
ProcessorInterface::STAGE_POST_QUERY constant Processing stage: post_query.
ProcessorInterface::STAGE_PRE_QUERY constant Processing stage: pre_query.
ProcessorInterface::STAGE_SORT constant Processing stage: sort.
ProcessorPluginBase::buildConfigurationForm public function Adds a configuration form for this processor. Overrides ProcessorInterface::buildConfigurationForm 10
ProcessorPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ProcessorPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 8
ProcessorPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ProcessorPluginBase::getDefaultWeight public function Returns the default weight for a specific processing stage. Overrides ProcessorInterface::getDefaultWeight
ProcessorPluginBase::getDescription public function Retrieves the processor description. Overrides ProcessorInterface::getDescription
ProcessorPluginBase::getQueryType public function Picks the preferred query type for this widget. Overrides ProcessorInterface::getQueryType 4
ProcessorPluginBase::isHidden public function Determines whether this processor should be hidden from the user. Overrides ProcessorInterface::isHidden
ProcessorPluginBase::isLocked public function Determines whether this processor should always be enabled. Overrides ProcessorInterface::isLocked
ProcessorPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ProcessorPluginBase::submitConfigurationForm public function
ProcessorPluginBase::supportsFacet public function Checks if the facet is supported by this widget. Overrides ProcessorInterface::supportsFacet 6
ProcessorPluginBase::supportsStage public function Checks whether this processor implements a particular stage. Overrides ProcessorInterface::supportsStage
ProcessorPluginBase::validateConfigurationForm public function Validates a configuration form for this processor. Overrides ProcessorInterface::validateConfigurationForm 2
QueryString::$eventDispatcher protected property The event dispatcher.
QueryString::$urlAlias protected property A string of how to represent the facet in the url.
QueryString::create public static function Creates an instance of the plugin. Overrides UrlProcessorPluginBase::create
QueryString::getFacetIdByUrlAlias protected function Gets the facet id from the url alias & facet source id.
QueryString::getRequestByFacetSourcePath protected function Gets a request object based on the facet source path.
QueryString::getUrlAliasByFacetId protected function Gets the url alias from the facet id & facet source id.
QueryString::getUrlForRequest protected function Gets the URL object for a request.
QueryString::__construct public function Constructs a new instance of the class. Overrides UrlProcessorPluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlProcessorPluginBase::$activeFilters protected property An array of active filters.
UrlProcessorPluginBase::$entityTypeManager protected property The entity type manager.
UrlProcessorPluginBase::$request protected property The clone of the current request object.
UrlProcessorPluginBase::$separator protected property The url separator variable. 1
UrlProcessorPluginBase::getActiveFilters public function Returns the active filters. Overrides UrlProcessorInterface::getActiveFilters
UrlProcessorPluginBase::getFilterKey public function Returns the filter key. Overrides UrlProcessorInterface::getFilterKey
UrlProcessorPluginBase::getSeparator public function Returns the url separator. Overrides UrlProcessorInterface::getSeparator
UrlProcessorPluginBase::setActiveFilters public function Set active filters. Overrides UrlProcessorInterface::setActiveFilters