I have one user role on d8 and have few additional fields on user account so when people are registering they can leave additional data. What I would like to achieve is that when user registers - new content of that specific type would be automatically created (node), assigned to him and the fields on that new content would be populated from those fields on user account which he filled during the registration process. I was thinking that this is maybe achievable with the Rules but since couldn't find proper documentation for Rules in d8 not sure is this is doable that way.
-
1At a very generic level, you could use hook_user_insert/hook_entity_insert, but that would fire any time a user is created, and not in the registration process only. You could add a submit handler to the registration form, and create a node that way. Did you look at Business Rules module?– KevinCommented Nov 16, 2019 at 17:34
-
1Are you looking to do this with custom code or without writing code?– anoopjohnCommented Nov 16, 2019 at 17:51
-
Actually if I can do it without code that would be nice. Thanks for that Business Rules module, I didn't know about it. I will check it.– Ivan IlicCommented Nov 16, 2019 at 18:07
-
1You don't need a contrib module, only a few lines of code, see drupal.stackexchange.com/questions/248028/…– 4uk4Commented Nov 17, 2019 at 7:48
2 Answers
This is very easy to do in D8 Rules. Here is an export of a Rule which does what you asked - when a new user is created, it creates a new node (of type article, as an example). It then sets the node author to be the new user. It then sets a field of the new node (the body field, in this case) to a value take from the user (the user's email address, in this case). As you can see, Rules are stored in YAML files, and are imported/exported like any other configuration in D8. So you can just import this and you will immediately have a working example which you can then modify to use the specific fields you've defined on your user entity and node type. This Rule additionally displays a system message, which is a very useful way to debug Rules.
langcode: en
status: true
dependencies: { }
id: create_a_node_for_each_new_user
label: 'Create a node for each new user'
events:
-
event_name: 'rules_entity_insert:user'
description: 'Node fields will be populated from values entered on the user form.'
tags: { }
config_version: '3'
expression:
id: rules_rule
uuid: c9428f2d-c0b9-47bf-a7e5-3f2cc8af9f54
conditions:
id: rules_and
uuid: 47b8d622-ed38-4977-8e58-4cca6d89260a
conditions: { }
actions:
id: rules_action_set
uuid: d96fd6b5-1638-4b15-adde-5f5d7673fb8f
actions:
-
id: rules_action
uuid: e09cd52d-76fe-4682-91de-bfed58a651ab
context_values:
message: 'Node created using information entered by <em>{{ user.name }}</em> when registering'
type: status
repeat: false
context_mapping: { }
context_processors:
message:
rules_tokens: { }
type:
rules_tokens: { }
repeat:
rules_tokens: { }
provides_mapping: { }
action_id: rules_system_message
-
id: rules_action
uuid: fe2acb55-9234-4754-9d2b-3d1889d1775c
context_values:
type: article
title: 'Article created by Rules for {{ user.name }}'
context_mapping: { }
context_processors:
type:
rules_tokens: { }
title:
rules_tokens: { }
provides_mapping: { }
action_id: 'rules_entity_create:node'
-
id: rules_action
uuid: 743be03f-d1b7-4251-a1ac-80b17aa5c86e
context_values:
value: '{{ user.mail.value }}'
context_mapping:
data: entity.body
context_processors:
value:
rules_tokens: { }
provides_mapping: { }
action_id: rules_data_set
-
id: rules_action
uuid: 91f3c8f2-eb47-46e6-a9dd-2affc90e15d2
context_values: { }
context_mapping:
data: entity.uid.target_id
value: user.uid.value
context_processors: { }
provides_mapping: { }
action_id: rules_data_set
I achieved it this way, with custom module and just those few lines in .module file. as @4k4 user suggested in the comment.
<?php
use Drupal\node\Entity\Node;
function provider_module_user_insert(\Drupal\user\UserInterface $user) {
if (($user->get('field_company_type')->value) == 'Provider') {
$tid = 12;
}
elseif (($user->get('field_company_type')->value) == 'Charity') {
$tid = 13;
}
elseif (($user->get('field_company_type')->value) == 'Club') {
$tid = 14;
}
\Drupal\node\Entity\Node::create([
'type' => 'providers_charities',
'field_company_name' => $user->get('field_company')->value,
'field_email_address' => $user->mail,
'uid' => $user->id(),
'field_company_type' => [['target_id' => $tid]],
'field_website' => $user->get('field_website')->value,
'field_phone_number' => $user->get('field_phone_number')->value,
])->save();
}
-
Because I had entity reference on node (taxonomy) I needed to check which field for the company field was populated and than assign it to that taxonomy reference field. I wanted to avoid profile 2 module and create different approach for user pages so this is why I needed this. Commented Nov 29, 2019 at 18:08