/** * {@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 []; } // 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. $get_params->remove('page'); // Set the url alias from the the facet object. $this->urlAlias = $facet->getUrlAlias(); $request = $this->request; if ($facet->getFacetSource()->getPath()) { $request = Request::create($facet->getFacetSource()->getPath()); } $url = Url::createFromRequest($request); $url->setOption('attributes', ['rel' => 'nofollow']); /** @var \Drupal\facets\Result\ResultInterface[] $results */ foreach ($results as &$result) { // Sets the url for children. if ($children = $result->getChildren()) { $this->buildUrls($facet, $children); } $filter_string = $this->urlAlias . self::SEPARATOR . $result->getRawValue(); $result_get_params = clone $get_params; $filter_params = $result_get_params->get($this->filterKey, [], TRUE); // If the value is active, remove the filter string from the parameters. if ($result->isActive()) { foreach ($filter_params as $key => $filter_param) { if ($filter_param == $filter_string) { unset($filter_params[$key]); } } } else { $filter_params[] = $filter_string; // 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 = $this->urlAlias . self::SEPARATOR . $result2->getRawValue(); foreach ($filter_params as $key2 => $filter_param2) { if ($filter_param2 == $active_filter_string) { unset($filter_params[$key2]); } } } } } } $result_get_params->set($this->filterKey, array_values($filter_params)); $url = clone $url; $url->setOption('query', $result_get_params->all()); $result->setUrl($url); } return $results; }
/** * {@inheritdoc} */ public function build(FacetInterface $facet, array $results) { $field_identifier = $facet->getFieldIdentifier(); $entity = 'node'; // Support multiple entities when using Search API. if ($facet->getFacetSource() instanceof SearchApiFacetSourceInterface) { $index = $facet->getFacetSource()->getIndex(); $field = $index->getField($field_identifier); $entity = str_replace('entity:', '', $field->getDatasourceId()); } // If it's an entity base field, we find it in the field definitions. // We don't have access to the bundle via SearchApiFacetSourceInterface, so // we check the entity's base fields only. $base_fields = $this->entityFieldManager->getFieldDefinitions($entity, ''); // This only works for configurable fields. $config_entity_name = sprintf('field.storage.%s.%s', $entity, $field_identifier); if (isset($base_fields[$field_identifier])) { $field = $base_fields[$field_identifier]; } elseif ($this->configManager->loadConfigEntityByName($config_entity_name) !== NULL) { $field = $this->configManager->loadConfigEntityByName($config_entity_name); } if ($field) { $function = $field->getSetting('allowed_values_function'); if (empty($function)) { $allowed_values = $field->getSetting('allowed_values'); } else { $allowed_values = ${$function}($field); } if (is_array($allowed_values)) { /** @var \Drupal\facets\Result\ResultInterface $result */ foreach ($results as &$result) { if (isset($allowed_values[$result->getRawValue()])) { $result->setDisplayValue($allowed_values[$result->getRawValue()]); } } } } return $results; }
/** * {@inheritdoc} */ public function buildUrls(FacetInterface $facet, array $results) { // Create links for all the values. // First get the current list of get parameters. $get_params = $this->request->query; // Set the url alias from the the facet object. $this->urlAlias = $facet->getUrlAlias(); // No results are found for this facet, so don't try to create urls. if (empty($results)) { return []; } /** @var \Drupal\facets\Result\ResultInterface $result */ foreach ($results as &$result) { $filter_string = $this->urlAlias . self::SEPARATOR . $result->getRawValue(); $result_get_params = clone $get_params; $filter_params = $result_get_params->get($this->filterKey, [], TRUE); // If the value is active, remove the filter string from the parameters. if ($result->isActive()) { foreach ($filter_params as $key => $filter_param) { if ($filter_param == $filter_string) { unset($filter_params[$key]); } } } else { $filter_params[] = $filter_string; } $result_get_params->set($this->filterKey, $filter_params); $request = $this->request; if ($facet->getFacetSource()->getPath()) { $request = Request::create($facet->getFacetSource()->getPath()); } $url = Url::createFromRequest($request); $url->setOption('query', $result_get_params->all()); $result->setUrl($url); } return $results; }