Drupal - Create user on content creation

Yes, you can do it with Rules module.

Basically, after enabling the Rules & Rules_UI module, you create a new Rule acted on a node is created, add an action of creating a new user entity. You need to spend a bit of time to specify the condition to suit your need.

Some screenshot of flow attached for you.

  • Create a new Rule
  • Add action: Entity Type - User
  • Configure Rules action
  • Rules Configuration Overview

As previously indicated, you can do this with the Rules module. However, there are a few specifics that are quite important. Moreover, there's better ways to go about doing what you're trying to do.

How to solve your question:

1.0 Create a "reaction" rule (at www.example.com/admin/config/workflow/rules/reaction/add);

1.1 React on Event: "After saving new content";

Note that there are a few other possibilities here that depend on your situation. Depending on exactly what you want to happen, it may be more appropriate to react on "Before saving new content" or even "After updating content". Each possibility will dictate it's own subsequent configuration.

2.0 Add Condition: "Content is of Type" (to ensure the rule only fires when it's "Course" content);

2.1 Content: "Node"

2.2 Content Type: "Course" (or )

3.0 Add Condition: "Entity exists by property" (to check that user name does not exist)

3.1 Entity Type: "User"

3.2 Property: "Name" (or any other user field);

3.3 Value: "instructor name" (based on your related post; otherwise this would be the ).

3.4 Negate this condition. (so that the rule will not fire if a user exists)

4.0 Add Action: "Create a New Entity"

4.1 Entity Type: "User"

4.2 Name: "Node:created:instructor_name" (note that you want the "instructor name" field)

4.3 email: (if creating dummy users you will most likely want an "example.com" address so that no email is ever sent.)

Note that you will need to either provide a valid email or to build it with the available tokens i.e. @example.com. Otherwise, this rule will silently fail - which is why it is often a best practice" to check that what you expected actually happened and notify you if it didn't.

This rule will do exactly what you asked - test if a user exists with the same username as the new course content's "instructor Name" field, and add the user if no user exists. The problem is that your question presupposes a less than optimal solution.

What happens when you have a second course with a different "dummy" user? Do you have any method to tell which are real and which are dummy users? Is it acceptable practice in your application to i.e. display "Taught by DummyUser" to the real user? What happens if the real instructor identified is not an instructor?

Rather than deal with those and many more issues related to dummy users, why not re-frame your question and ask how to relate courses to instructors when the instructor A) exists, and B) does not exist?

If you re-frame your question, there's any number of answers ranging from simple to very sweet. The benefit is that since the data model then matches reality, you avoid all these weird issues that themselves need dealing with.

On the one hand, the answer could be simply don't make the "instructor name" field required. On the other hand, it could add: using the Views module to list only those courses that do not have an instructor. Further, use the Contextual Administration module to provide a way to add a user AND automatically associate it with the course.

As noted by others, Johan Falk at Node One has done a wonderful job explaining all the details in a number of screen-casts.

Tags:

Nodes

Users

7