I had the need to base a context condition on the contents of a field. I'll be attaching a patch based loosely on the code in #1392794: Expose user fields as a context condition, giving the ability to set context conditions on the content of text/term reference fields, and additionally adding empty/not-empty conditions for fields of most types.

Comments

acrollet’s picture

Status: Active » Needs review
StatusFileSize
new7 KB

Patch attached.

acrollet’s picture

updated patch attached, fixes a php notice when using the condition on an image field.

acrollet’s picture

updated patch attached, fixes notices when testing on a non-existent field.

pasqualle’s picture

pbfleetwood’s picture

Patch context-node_field_conditions-1403330-3.patch in #3 does not contain the code for context.core.inc and context.plugins.inc that the two prior patches included. I've not yet learned how to prepare a patch, so I cannot provide the fix here; though I was able to manually update all three .inc files on my site with the code provided in these patches, and the new functionality provided by this fix works very well--just what I needed.

pbfleetwood’s picture

Here is a use case that will demonstrate how helpful this plugin can be.

I needed to provide a sidebar block containing default contact information on all pages of a certain content type, except those that have more specific contact information. To do this, I created a block containing the default information, added a long text field (needed to be able to edit it with WYSIWYG) to my Basic Page content type, added a context using the node field condition provided in this thread, conditioned the block to appear on all pages unless the node's contact field is empty, used the CCK Blocks module to add a block that displays the contents of the node's contact field. If the user enters contact information to a page, a block with that information appears, otherwise the default block appears. Voila!

This plugin will be useful for making all sorts of site functions work with defaults and overrides, and with little or no administrator maintenance.

pbfleetwood’s picture

I ran into a snag using the approach that I outlined in #6, and this may be a problem for anyone using the "Node field" context with multiple content types.

If a given content type doesn't have the specified field, the context will not trigger. Instead, it would be logical for it to react as though the field were empty; thus, a condition requiring an 'empty' node field would evaluate to true.

#acrollet, would that be a feasible change? I'll root around in the code that you provided to see whether I can figure it out, but I'm a PHP neophyte, so I may not get very far with it. Got it! See below.

pbfleetwood’s picture

It appears that inserting the following code between lines 93 and 94 of your patch (#3) will add the logic that I mentioned in #7, i.e. if the specified field is missing from a content type, the 'empty' condition will be satisfied. It works on my site.

            else {
              $this->condition_met($context);
            }

I hope that the Node Field condition (with this enhancement) will eventually be committed to a dev release; all of these patches make me crazy. o_O

pbfleetwood’s picture

Status: Needs review » Needs work

FWIW, this plug-in has now been running on our production site for a couple of weeks. We've had 100k+ page views, and have had no errors or conflicts resulting from this plug-in. We would love to see it added to the module's distribution by someone who knows how to do it.

If someone wants to add this plug-in to the distribution, please see the comments (#2, #3, #5, and #8), as the patch alone does not quite do it. The tweaks described in the comments are necessary.

@acrollet, if you would incorporate my tweaks into your patch, it would help to move this along. Thank you.

doublejosh’s picture

pbfleetwood’s picture

@doublejosh, no, issue #629756: CCK text and number conditions using content_allowed_values(). refers to 6.x-3.x, this issue refers to 7.x-3.x. Unless a solution is to be ported one way or the other, they are not duplicates.

pbfleetwood’s picture

Status: Needs work » Closed (won't fix)

I've found this patch troublesome, so I've switched to using the new Context entity field module.

As no one else seems to be interested in this issue, I'm closing it.