Creating a Rule condition that evaluates a field an entity that is referenced by a created node

by CW Smith   Last Updated December 26, 2016 08:03 AM

I'm trying to create a rule for new node content that is based on entity references.

To give some background I have story pages and chapter pages. The story pages are story meta data, the chapters are real content. The chapters reference the story pages and are sorted by chapter numbers set by the users.

I want to create a rule for when a new chapter is published. But it needs to be able to evaluate a field on the Story the chapter is a part of. How do I get rules to go to that story entity in question and check the value of the fields in question?

Tags : entities rules


Answers 1


Okay, I think I figured out a way to pull this off, and even if it's not the best practice for what I'm attempting to do, it is working.

So like I said, I was trying to create a condition that relies on information contained in a node that is being referenced as an entity. One of the discussion posts indicated I may need to create a component.

So I went to components and created a new component. Data type I set for Any Entity then named it story page (story_page).

I set conditions that the entity is content, and then a condition where content is of type story page.

Now that we have the content type identified and set we create a condition for list contains item and search for the item in the specific taxonomy I'm looking for. 

TO understand this component understand that there are some folk who post more mature content on my page that I DON'T want exposed to the general public. So I require that that kind of content be marked by a specific setting in two fields.

This checks one of those fields and makes the determination. The information however on a chapter for that to be determined is stored in a referenced node which is set in field_story. You have to be able to get the information from that node, and that information is stored in field_genre. So the Componenet is built like this.

{ "rules_check_for_erotica" : {
"LABEL" : "Check for Erotica",
"PLUGIN" : "and",
"OWNER" : "rules",
"TAGS" : [ "Entity Reference", "Node", "Twitter" ],
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : { "story_page" : { "label" : "Story\u00a0Front Page", "type" : "entity" } },
"AND" : [
  { "entity_is_of_type" : { "entity" : [ "story-page" ], "type" : "node" } },
  { "node_is_of_type" : {
      "node" : [ "story-page" ],
      "type" : { "value" : { "story_front_page" : "story_front_page" } }
    }
  },
  { "list_contains" : { "list" : [ "story-page:field-genre" ], "item" : "161" } }
]

} }

Now, we have a component that can test and figure out if the attached story field has that entry. We can then use it for rules related to story pages to test for it, and make a determination on what to do based on it.

I then created another rule similar to this but it was checking a different field to determine if the story in question is an original story or if it's part of an original series on this page. I then created a new rule to check the chapter page.

{ "rules_publish_new_chapter_darkscribes_" : {
"LABEL" : "Publish New Chapter (Darkscribes)",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"TAGS" : [ "Facebook", "Social", "Twitter" ],
"REQUIRES" : [ "rules", "twitter_actions", "fb_autopost_entity" ],
"ON" : { "node_insert--chapter_page" : { "bundle" : "chapter_page" } },
"IF" : [
  { "component_rules_check_for_original" : { "story_page" : [ "node:field-story" ] } },
  { "NOT component_rules_check_for_erotica" : { "story_page" : [ "node:field-story" ] } }
],
"DO" : [
  { "rules_core_twitter_actions_set_status_action" : {
      "message" : "[node:author] just posted a new chapter for [node:field_story] | Chapter [node:field-chapter-no-]: [node:title] | [node:url]",
      "sender" : "Darkscribes"
    }
  },
  { "entity_create" : {
      "USING" : {
        "type" : "facebook_publication",
        "param_type" : "post",
        "param_user" : [ "site:current-user" ]
      },
      "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
    }
  },
  { "data_set" : {
      "data" : [ "entity-created:field-facebook-link" ],
      "value" : [ "node:url" ]
    }
  },
  { "data_set" : {
      "data" : [ "entity-created:field-facebook-message" ],
      "value" : [ "node:body:summary" ]
    }
  },
  { "publish_to_facebook" : {
      "USING" : { "publication" : [ "entity-created" ], "pages" : "215544991848808" },
      "PROVIDE" : { "facebook_publication_id" : { "facebook_publication_id" : "Facebook publication returned ID" } }
    }
  },
  { "data_set" : {
      "data" : [ "facebook-publication-id" ],
      "value" : [ "facebook-publication-id" ]
    }
  },
  { "entity_save" : { "data" : [ "entity-created" ] } }
]

} }

I tested it to see if it would post a message to one of the two twitter accounts in question, and it SEEMS to have done just right.

I may need to revisit this answer later and add an image or screenshot to it at some point. Or find a way to consolidate my content checking rules.

CW Smith
CW Smith
December 25, 2016 20:31 PM

Related Questions


Updated July 19, 2016 08:03 AM

Updated March 04, 2017 19:07 PM

Updated March 05, 2017 00:07 AM

Updated July 06, 2017 23:07 PM