Optional context is always empty for content_type plugins.
It is caused by ctools_context_optional->add_empty() method during merge operation.
To solve it I propose simple patch:
ctools/includes/context.inc
---
@@ -194,7 +194,8 @@ class ctools_context_optional extends ctools_context_required {
$context = new ctools_context('any');
$context->title = t('No context');
$context->identifier = t('No context');
- $contexts = array_merge(array('empty' => $context), $contexts);
+ //$contexts = array_merge(array('empty' => $context), $contexts);
+ $contexts['empty'] = $context;
}
function filter($contexts) {
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | ctools_context_optional_always_empty-1032218-13.patch | 502 bytes | sylus |
| #5 | ctools_context_optional_always_empty-1032218-5.patch | 573 bytes | reevo |
Comments
Comment #1
merlinofchaos commentedThis isn't right.
The reason we're using array_merge() there is so that empty always appears at the top of the list.
Other than that, this patch isn't changing anything, so I don't understand what it's trying to do. The bug report doesn't make any sense to me. There is not enough context.
Comment #2
brunodboI ran into the same problem. When you define an optional context in the $plugin array, the context isn't passed to the content type anymore (other content types in the same panel variant continue to work).
I found #516504: Problem with multiple optional contexts - I'm only using one context though. Changing the code above to the code below, seems to fix the issue.
Comment #3
Anonymous (not verified) commentedappending the 'empty' context instead of prepending it fixes this problem for me, as well
in ctools/includes/content.inc, line 763
when ctools_context_filter returns its filtered contexts, it returns an array with empty and the correctly matched context in that order. array_shift pulls the first element into the context, which is always 'empty' because add_empty forces it to the front
Comment #4
ianschweer commentedI am also experiencing this problem. I followed sidke advise on the subject and was able to take care of it. By appending the empty context to the array instead of prepending i was able to take care of it. However, i would think that adding restrictions on the ctools content type, then shouldn't the empty context be filtered out?
Comment #5
reevo commentedThe fix in the OP works for me, too.
To summarise, $context->data is always NULL when using ctools_context_optional, even when context is present.
Comment #6
karel010 commentedPatch works as explained.
Comment #7
japerryMarking needs work per #1, I'm not convinced either that this is needed or that it won't break something else.
Comment #8
sylus commentedI ran into this problem as well and patch resolved it for me.
Comment #9
papagrandeThis patch fixes content_type plugins that have optional contexts. Without it, an optional context will be an empty object even when there is valid data to create the object.
I've had this patch on a production site for almost a year with no apparent problems.
Comment #10
damienmckennaThe patch certainly could be cleaner, i.e. remove the commented-out line.
Does anyone have an example plugin that triggers the bug?
Comment #11
reevo commentedI'm actually having trouble replicating the issue now, so will echo @DamienMcKenna's request: @sylus, @PapaGrande, can you provide an example plugin?
Comment #12
papagrande@DamienMcKenna, @reevo Here's a simplified version of one of my plugins:
I can clean up the patch if you agree that this is a good direction to go.
Comment #13
sylus commentedAttaching proper patch to move this issue along.
Comment #14
sylus commentedComment #15
mpotter commentedWow, thanks for this patch. This was driving me crazy. We have a widget in Open Atrium with an optional node context and was having this exact problem (context was always null). The patch in #13 worked great! This really needs to get committed! Seems like there are numerous people reporting success with this patch here.
Comment #16
damienmckenna+1.
Comment #17
mpotter commentedTo add more info to this. #1 is correct in that the only difference with this is that the original code causes the 'empty' key to be at the beginning of the array. With the patch in #13, the array data is all the same except the "empty" key is at the end of the array.
In my case, here is what $contexts (var_dump) was set to using the patch:
My guess is that the *real* cause of this bug is elsewhere in ctools where somehow the order of these keys actually matters. If it finds "empty" first, then it's returning a NULL node context. When "empty" is last it properly returns the node context from panelizer.
Comment #18
mortona2k commented+1 for #13.
Comment #19
damienmckennaComment #20
japerryOkay. looks good to me! Fixed.