6

In a "course" content type, I have an "instructor name" field, which points to a registered user.

When the "course" content is saved, I need to check if there are any registered users with the name in the "instructor name" field. If there is, I'd like to relate the created content with his id. If not, I'd like to create a new user (a dummy user maybe) and relate the content with that user.

Is this possible without php coding, just using modules? And if it is, how? I am a new drupal user with .net background and have no knowledge of php.

6

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
  • 2
    Hi Turzifer, I would like to add to this answer by pointing you in the direction of Node One's awesome video tutorials on the subject. - dev.nodeone.se/en/blog/taming-the-butler-learning-rules – Stan Ascher Jan 16 '12 at 18:33
  • @gilzero, thank you for the rules module recommendation and the screenshots. – turzifer Jan 23 '12 at 15:36
  • Still, I could not find a way to check if a user with that name already exists (as a condition to the rule). – turzifer Jan 23 '12 at 15:55
  • You can use Unix time + some random string to be username, should be unique. – gilzero Jan 23 '12 at 22:41
4

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.

  • Why DON'T I see the "Entity Exists By Property" in condition selection ? Are you sure that this condition does exist ? I'm using Rules 7.26 – jetwong Dec 30 '13 at 3:50
  • This "Entity by property" is sadly not in Rules yet, see drupal.org/node/1777204 – user25184 Jan 12 '14 at 16:58
  • rules is good but could it be possible to implement it via code. Which hook should I use hook_node_insert or hook_node_presave. In my requirement username is autogenerated using email_registration, whereas in rules it is mandatory to enter username while creating new user entity. – kiranking Feb 16 '15 at 9:02

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.