Hi,
I was working to produce a relationship with a required, and a optional context (basically a relationship for a term, with an optional second term as input). As indicated by the help-document (help/context-relationships.html) this should be possible, as it says that you can specify "One or more ctools_context_required/optional objects" as the contexts for the relationship)
However, this is not the case. If you have an optional context, and leave it blank (i.e. choose the empty context), then the relationship will not be attached.
This is due to the function of ctools_context_get_context_from_relationships from include/context.inc. What it does, is basically to run through each of the relationships, check that the chosen contexts exists, and then pass those contexts onwards. However, the empty-context is not present so if a relationship have chosen the empty-context for one or more of its optional contexts, then the relationship does not have all of the configured contexts, and the relationship will be skipped.
This is basically the lines
// Snippet from ctools_context_get_context_from_relationships from include/context.inc,
if (is_array($rdata['context'])) {
$rcontexts = array();
foreach ($rdata['context'] as $cid) {
if (empty($contexts[$cid])) {
continue 2;
}
$rcontexts[] = $contexts[$cid];
}
}
If a optional relationship is left blank, then the context-id ($cid) will be empty, and $contexts['empty'] does not exist, so the continue 2; will ensure that the relationship is skipped.
To fix, either $contexts needs to contain the empty context, or there needs to be a special case inside of foreach above, that injects the empty context if the $cid equals empty.
Kind regards
Morten
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | ctools-optional-context-relationship-2341363-6.patch | 3.63 KB | junaidpv |
| #5 | ctools-optional-context-relationship-2341363-5.patch | 3.56 KB | junaidpv |
| #1 | ctools-optional-context-relationship-2341363-1.patch | 3.56 KB | luis.hdz1010 |
Comments
Comment #1
luis.hdz1010 commentedI came across the same issue when trying to use a "view_from_argument" relationship with optional contexts, aside from what you mention in the issue description, the view_from_argument plugin wasn't ready for handling optional arguments. In this patch I use your proposed fix (still figuring out if it’s OK to just push out a null when we encounter an empty context) and attempt to fix the view_from_argument plugin shortcomings.
I don't know if this is the correct approach to fix this, but it works for me.
Comment #3
geek-merlinI had the same problem and this patch fixed it.
(also needed #2556189: Optional context from empty argument passes wrong data which currently needs review)
Comment #4
japerryPostponed until we get a chance to flush out #2556189: Optional context from empty argument passes wrong data
Comment #5
junaidpvFirst of all, thanks for the patch. But I could not apply the patch from comment#1. Here is the rerolled patch for latest dev. It is working good.
Comment #6
junaidpvA crude re-rolled version of #5 as it is not being applied 7.x-1.20. Please note it is not tested yet.
Comment #7
euk commentedLooking at this piece of code - this doesn't feel right:
empty()returns TRUE for anything that is falsy, so NULL, 0, '', [] all are empty. Would be better to pass the value as is unless it is not set:$rcontexts[] = isset($contexts[$cid]) ? $contexts[$cid] : NULL;instead the whole first IF block?