Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-2234: Passing the contextual filter source to the ContextualAwareProcessorInterface #138

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
"drupal/ctools": "^3.7",
"drupal/drupal-extension": "^4.0",
"drupal/extra_field": "^2.1",
"drupal/inline_entity_form": "^1.0-rc9",
"drupal/inline_entity_form": "^1.0-rc12",
"drupal/token": "^1.10",
"drush/drush": "^10.3",
"openeuropa/code-review": "^2.0",
"openeuropa/oe_link_lists": "^0.14",
"openeuropa/oe_link_lists": "dev-master",
"openeuropa/oe_multilingual": "^1.10",
"openeuropa/open_vocabularies": "^1.0-alpha6",
"openeuropa/task-runner-drupal-project-symlink": "^1.0.0-beta5",
Expand Down
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