2

I have 50 users on my site. I want to create 50 nodes and then set the author of the node to each of my 50 users.

Is there a way to do this using a module or programmatically, rather than having to first create all the 50 nodes and then editing the author fields?

  • If you are using Drupal 8, you need to first call Node::create() and then the save() method of the returned object. So, the only way is to first create the node object. The same is true for Drupal 7, except the node object has a different class (i.e. stdClass). – kiamlaluno Feb 20 '17 at 10:24
2

Step 1

Use the Rules module to create a Rules Component, which includes whatever Rules Conditions you might need (if any), and with a Rules Action to Create an entity (of type node). Make sure to use a parameter (of type "integer"), and which you use as the value to be assigned for the "author" of the node to be created. Here is an example of such Rules Component (in Rules export format, if you have the Rules UI enabled you should be able to import it in your own site):

{ "rules_create_a_new_node" : {
    "LABEL" : "Create a new node",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "uid_of_author" : { "label" : "Uid of the user to be used as author", "type" : "integer" } },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "3" : "3" } }
        }
      }
    ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : { "type" : "user", "id" : [ "uid-of-author" ] },
          "PROVIDE" : { "entity_fetched" : { "user_fetched" : "Fetched user" } }
        }
      },
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "page",
            "param_title" : "Node created for user [user-fetched:name]",
            "param_author" : [ "user-fetched" ]
          },
          "PROVIDE" : { "entity_created" : { "node_created" : "Created node" } }
        }
      },
      { "drupal_message" : { "message" : "Node creation completed for user with name \u0022[user-fetched:name]\u0022." } }
    ]
  }
}

Some details about the above Rules Component:

  • It checks if the user using it, has role "admin" (just to be secure it a bit, feel free to omit that if you don't want that).
  • It fetched the info about the user for which the uid was entered as a parameter.
  • It creates a node of type (machine name) page (adapt that to whatever machine name fits for you).
  • It shows a Drupal message to confirm the node got created (just for testing, remove it if you don't need it anymore).

After you imported the above Rules component, use the "execute" link in front of this Rules Component to QA-test your component (for which you'll need to enter an appropriate uid value for the author you want). After it completes, it'll show an informational Drupal message similar to this one (with Demo.User = the name of the user for which I entered the uid):

Node creation completed for user with name "Demo.User".

Step 2

Create a view of the group of users for which those nodes should be created. Then transform that view into a view that uses Views Bulk Operations, and use the technique documented in "How to use Rules to create a custom bulk operation for a VBO view?". In doing so, you'll then be able to use the Rules Component from Step 1. Make sure one of the fields (columns) in your VBO view is the uid for each of the (50) users for which you want to create such node.

Tutorial

To get the idea, have a look at the video about Using Rules components with VBO" which uses a Rules Component with (only) 1 parameter. If you want even more, then also watch the subsequent "Using Rules components with more than one parameter" video.

Attention: to process like 100.000 entities at once is quite a challenge (memory requirements, etc). Therefor you might want to also watch the video about "More details about VBO settings" ... Before you are tempted to use VBO for processing them all at once ...

PS: no need for custom code ...

| improve this answer | |
  • Thanks! The only modification I made to the Rule component was that, rather than use a parameter (of type "integer"), I used a parameter (of type "user"). This ensures that when you execute the VBO, you don't have to provide any variable. Also when you use the parameter (of type "integer"), the component may not be available as an option in the View. – seyi Feb 20 '17 at 23:14
0

You can do that by a drush script looking like this:

$users = entity_load('user');
foreach ($users as $user) {
        $node = new stdClass();
        $node->type = 'yourContentType';
        $node->status = 1;
        $node->uid = $user->uid;
        $node->title = $user->name;
        $node->promote = 0;
        $node->sticky = 0;
        $node->language = 'en';
        $node->body = '<p>body</p>';
        node_save($node);
}

Here is more info about how to execute it How to execute php script using drush?

| improve this answer | |

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.