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

ISSUE-449: Fix multiple Contextual filters, some "NULL" and allow Exposed filters. But avoid if already set same "key" via a Cont… #450

Merged
merged 2 commits into from
May 22, 2024
Merged
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
52 changes: 44 additions & 8 deletions src/Controller/MetadataAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,15 @@ public function castViaView(
// We will bring them first into the right order.
// We will do this a bit more expensive
// @TODO make this an entity method
foreach ($executable->display_handler->getOption('filters') ?? [] as $filter) {
if ($filter['exposed'] == TRUE) {
// TODO. What are we doing with exposed ones??
}
}

$arguments = [];
//@ TODO maybe allow to cast into ANY entity? well...
foreach ($executable->display_handler->getOption('arguments') ?? [] as $argument_key => $filter) {
// The order here matters
// The order here matters. So we pre-set them all as Exception values/or as Empty
// Empty/NULL might fail validation of course, and we end with Zero RESULTS.
// Up to the API builder to either Enforce a Value OR make the Contextual Filtes flexible.
$exception_value = $filter['exception']['value'] ?? NULL;
$arguments[$argument_key] = $exception_value;
foreach ($arguments_with_values as $param_name => $value) {
if ($argument_key == $param_name) {
// Why we check this?
Expand Down Expand Up @@ -408,13 +408,49 @@ public function castViaView(
}
}
}
// Deal with Filters. Exclude values set already for the same field on Contextual ones
// At least for now.
$filters = [];
foreach ($executable->display_handler->getOption('filters') ?? [] as $filter_key => $filter) {
if ($filter['exposed'] == TRUE) {
foreach ($arguments_with_values as $param_name => $value) {
if ($filter_key == $param_name && !isset($arguments[$param_name])) {
$exposed_filter_id = $filter['expose']['identifier'] ?? NULL;
// avoid setting Filters for values we already set as $contextual ones.
// The only one we know for sure without crazy code to be a nod is nid
if ($exposed_filter_id) {
$filters[$exposed_filter_id] = $value;
if ($filter_key == "nid") {
if (Uuid::isValid($value)) {
$nodes = $this->entityTypeManager->getStorage('node')
->loadByProperties(
['uuid' => $value]
);
if (!empty($nodes)) {
$node = reset($nodes);
$filters[$exposed_filter_id] = $node->id();
}
} elseif (is_scalar($value)) {
// Deals with NODE ids instead.
$node = $this->entityTypeManager->getStorage('node')
->load($value);
if ($node) {
$filters[$exposed_filter_id] = $node->id();
}
}
}
}
}
}
}
}

// We need to destroy the path here ...
$executable->exposed_data = $filters;
// We need to destroy the path, if any, here too.
if ($executable->hasUrl()) {
$executable->display_handler->overrideOption('path', '/node');
}
$executable->setArguments(array_values($arguments));
//
$views_validation = $executable->validate();
if (empty($views_validation)) {
try {
Expand Down