3

I am trying to create a Rules Component that fetches a list of nodes based on their content type. But when I try creating it, I get a little stuck. Here's my process:

  1. I create a rules component using "action component"
  2. Then add an action to the component "fetch entity by property"
  3. Select the entity type I would like to fetch "node"

I am then presented with the following:

fetch entity by type

In the value box I don't have node types nor a way to pass a text string that represents one, I have tried several ways by including introducing a text variable set to the entity type but if I try and use that as the value I get an error which says something about the variable being the wrong type.

  • 1
    I don't have access to the sire anymore as the question was asked three years ago, sorry :( – Daniel Harper Apr 27 '17 at 10:48
2

Introduce a variable of type Text Token with the content type as the value.

Parameter: Type: Text token, Value: content_type

And then use this new variable as the value for comparison with the property.

1

Have a look at this Rules Component (an Action Set), in Rules export format:

{ "rules_fetch_all_nodes_of_a_selected_content_type" : {
    "LABEL" : "Fetch all nodes of a selected content type",
    "PLUGIN" : "action set",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "fetched_content_type" : { "label" : "Fetched content type", "type" : "token" } },
    "ACTION SET" : [
      { "entity_query" : {
          "USING" : {
            "type" : "node",
            "property" : "type",
            "value" : [ "fetched-content-type" ]
          },
          "PROVIDE" : { "entity_fetched" : { "node_fetched" : "Fetched node" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "node-fetched" ] },
          "ITEM" : { "current_node" : "Current node" },
          "DO" : [
            { "drupal_message" : { "message" : "Title of current node  (with nid = [current-node:nid]) is [current-node:title]" } }
          ]
        }
      }
    ]
  }
}

After you import it in your own site, use the "execute" link (to the right of it) to experiment with it in your own site.

As a sample, enter a value like article for its input parameter (=fetched_content_type, which is the machine name of the content type to be processed). After you do, it'll show a Drupal message for each of your nodes with a content type that matches the value of your parameter. Here is a sample of such messages:

Title of current node (with nid = 19) is How to do X?
Title of current node (with nid = 53) is Hello World ...
Title of current node (with nid = 57) is Bonjour!

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.