Skip to content

Commit

Permalink
EWPP-2234: Passing the contextual filter source to the ContextualAwar…
Browse files Browse the repository at this point in the history
…eProcessorInterface.
  • Loading branch information
upchuk committed May 17, 2022
1 parent def9191 commit a0b9233
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ interface ContextualAwareProcessorInterface {
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
* @param string $source
* The source from where the contextual filter value should come.
*
* @return array
* The filter values.
*/
public function getContextualValues(ContentEntityInterface $entity): array;
public function getContextualValues(ContentEntityInterface $entity, string $source = ContextualPresetFilter::FILTER_SOURCE_FIELD_VALUES): array;

}
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,16 @@ protected function extractValuesFromField(FieldItemListInterface $items, FacetIn
* The filter values.
*/
protected function getValuesForContextualFilter(ContextualPresetFilter $contextual_filter, ContentEntityInterface $entity, ListSourceInterface $list_source, CacheableMetadata $cache): array {
$facet = $this->configurationBuilder->getFacetById($list_source, $contextual_filter->getFacetId());
$processor = ContextualFiltersHelper::getContextualAwareSearchApiProcessor($list_source, $facet);

// First, determine where the filters need to look for the values.
if ($contextual_filter->getFilterSource() === ContextualPresetFilter::FILTER_SOURCE_ENTITY_ID) {
if ($contextual_filter->getFilterSource() === ContextualPresetFilter::FILTER_SOURCE_ENTITY_ID && !$processor) {
// If the current entity ID is the source, we just have to return it.
return [$entity->id()];
}

// Otherwise, load the facet and check the field definition.
$facet = $this->configurationBuilder->getFacetById($list_source, $contextual_filter->getFacetId());
$definition = $this->getFacetFieldDefinition($facet, $list_source);
if ($definition) {
$field_name = $definition->getName();
Expand All @@ -268,12 +270,11 @@ protected function getValuesForContextualFilter(ContextualPresetFilter $contextu

// If there is no field definition, it may be a custom Search API field
// processor that may be contextually aware.
$processor = ContextualFiltersHelper::getContextualAwareSearchApiProcessor($list_source, $facet);
if (!$processor) {
throw new InapplicableContextualFilter();
}

return $processor->getContextualValues($entity);
return $processor->getContextualValues($entity, $contextual_filter->getFilterSource());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\oe_list_pages_link_list_source\ContextualAwareProcessorInterface;
use Drupal\oe_list_pages_link_list_source\ContextualPresetFilter;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
Expand Down Expand Up @@ -75,7 +76,7 @@ public function addFieldValues(ItemInterface $item) {
/**
* {@inheritdoc}
*/
public function getContextualValues(ContentEntityInterface $entity): array {
public function getContextualValues(ContentEntityInterface $entity, string $source = ContextualPresetFilter::FILTER_SOURCE_FIELD_VALUES): array {
// Check if the entity has a test contextual field that we can return
// a value from. Otherwise default to the entity ID.
if ($entity->hasField('field_test_contextual_filter')) {
Expand Down

0 comments on commit a0b9233

Please sign in to comment.