In context_ui_editor_process and context_ui_form_process there are blocks that say:

        if ($submit && context_empty($context->conditions[$condition]['values'])) {
          unset($context->conditions[$condition]);
        }

These will automatically remove any contexts which are "empty". This includes contexts that have their fields set to 0, which could well be a valid value. (in my case I'm writing 'min/max search results' plugins, and I want to support the case of there being no results).

The purpose of this code is presumably to make the 'conditions' list on the edit screen a little prettier by removing any conditions that are no longer used by the context. I think that's actually a bad UI anyway, because it is fairly likely that users will edit the same context multiple times as they try to get it to work properly and may clear all values from a particular condition plugin, but soon after change their mind and add those values back. They shouldn't need to find the condition plugin from the dropdown again - it should be in the list as they were only just using it. If they are sure they don't want it any more then it is easy for them to click the remove link.

If others disagree with my reasoning, then how about changing context_empty so the non-array check is

$empty = $empty && empty($element) && $element !== '0' && $element !== 0;

This will mean the condition will be removed for empty strings and empty arrays, but will remain for zeros.

CommentFileSizeAuthor
#1 context_empty-1842566-1.patch303 bytesDeciphered
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Deciphered’s picture

Title: Conditions removed if they are "empty" » context_empty() is flawed.
Status: Active » Needs review
FileSize
303 bytes

Yes, I just hit this issue with a reaction, that reaction being:

'environment_indicator_status' => 0

For my module, this is a valid reaction, however it will get removed as it is deemed as empty.

Maybe instead of checking empty, it should check either for is_null() or !isset().

!isset() patch attached.

ianthomas_uk’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

That solution works for me

colan’s picture

Status: Reviewed & tested by the community » Fixed

Committed in a19066c.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.