2

Is there any way to check if a field has no value (image field) after saving the content in Rules? I tried Data Value is Empty, but it doesn't work!

Output of the rules:

{ "rules_autos_featured_listing_without_image" : {
    "LABEL" : "Autos: featured listing without image",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "autos" ],
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert--listing" : { "bundle" : "listing" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_listing_grouping" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_feature" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_images" } },
      { "data_is" : {
          "data" : [ "node:field-feature" ],
          "value" : { "value" : { "feature" : "feature" } }
        }
      },
      { "data_is_empty" : { "data" : [ "node:field-images" ] } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "node:field-listing-grouping" ], "value" : "1302" } }
    ]
  }
}
1

Make sure the field you are trying to "test" (for its value), is available at the time Rules tries to do so.

That's why I always add a entity_has_field condition (and put it as the first condition) which looks something as in this example (part of a rules export of some rule):

"IF" : [
  { "entity_has_field" : { "entity" : [ "account" ], "field" : "field_next_appointement" } }
],

In my sample here, it is related to entity account, and my field's machine name is field_next_appointement.

So retry your rule with the Data Value is Empty condition, but put such entity_has_field condition in front of it. So that in the end your rule has 2 conditions (no PHP coding at all involved), something like so:

  • entity has field ...
  • NOT Data Value is Empty ...

Below is a (simple) rule from my training class examples. It'll display a message like "Watch, this is ONLY 100 EUR !!!!" for a node which has a field_article_price field, whereas that price is exactly 100 EUR.

{ "rules_this_article_is_for_sale" : {
    "LABEL" : "This article is for sale",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_view" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_article_price" } },
      { "data_is" : { "data" : [ "node:field-article-price" ], "value" : "100" } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Watch, this is ONLY 100 EUR !!!!", "type" : "warning" } }
    ]
  }
}

Looking at your (updated) question (now including your current rule), I think you should try to work step-by-step to investigate which part of your rule "doesn't" work. If I was in your shoes, I'd experiment with these variations (only for debugging):

  1. Replace the ACTION with just a drupal_message:

    • if you get a message then, your problem is within ACTION. Todo: fix the Action.
    • if not your problem is in the EVENT of CONDITION(s). Todo: go to item 2.
  2. Remove all CONDITION(s):

    • if you get a message then, your problem is within CONDITION(s). Todo: fix the CONDITION(s).
    • if not your problem is in the EVENT. Todo: go to item 3.
  3. Review the EVENT: if the event you're trying to use to make the Rule work does not actually happen, or if you're "too late", then of course that needs to be fixed/reviewed. In my experience, there are quite a few Rules scenarios where you should use "When Drupal is initializing". My (personal favorite) sample of that is about "before content is going to be viewed" ...

I realize the above 3 steps do not actually "answer" your question, but they may help to debug what's actually the problem.

  • Thanks again Pierre for answering! so is this a custom PHP code condition? also I dont want to check if the entity has field, but if the field has value! – Gus Aug 30 '15 at 9:08
  • 1
    I updated my answer to try to reply to your comment above. – Pierre.Vriens Aug 30 '15 at 9:11
  • this is what I did the first place, unfortunately against all odds!! it doesn't work this way – Gus Aug 30 '15 at 9:14
  • actually it was a conflict on my side, it was okay at the first place, thanks! – Gus Aug 30 '15 at 9:24
  • actually now that I checked it more :D it doesnt really work for empty condition, I'll update the answer in a sec and thanks for being badass :D – Gus Aug 30 '15 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.