Creating a user in code
mumble.
Table of contents
Drupal project discussed in this chapter: Entity API.
- https://www.drupal.org/forum/support/module-development-and-code-questions/2015-03-04/how-to-programmatically-create-a-user
- drupal8.ovh: Create a user account programmatically in Drupal 8
Introduction
The Entity API project facilitates creating a node on code. How you do it is explained in the tutorials:
This chapter just reiterates that recipe, with some additional explanation.
Setting up
Before starting, please download and enable the contributed module Entity API. This module will be used to do some of the heavy lifting below. If you haven't done so already, do:
$ drush en entity -y
The custom module to create the node in code shall be named “My
module”, with the machine name “mymodule”. Start by creating the
file mymodule.info
and make sure you set
“entity
” (machine name of the Entity API
project) as a dependency:
name = My module description = "Project to create a node in code." core = 7.x dependencies[] = entity
Then create the file mymodule.module
and
create hook_menu
to make it accessible from the GUI.
/** * @file * A module to create a node in code. */ /** * Implements hook_menu(). */ function mymodule_menu() { $items = array(); $items['admin/createnode'] = array( 'title' => 'Create node', 'description' => 'Small custom module to create a basic page in code.', 'page callback' => 'mymodule_nodecreate', 'access arguments' => array('administer content types'), 'weight' => 9, ); return $items; }
The code to create a node
The following boilerplate is a function to create a basic page belonging to the logged-in user:
function mymodule_nodecreate() { global $user; // The function entity_create do the heavy lifting required to // create a new node object and setting its 5 basic properties. $values = array( 'type' => 'page', 'uid' => $user->uid, 'status' => 1, 'comment' => 0, 'promote' => 0, ); $entity = entity_create('node', $values); // The entity is now created, but we have not yet simplified use of it. // Now create an entity_metadata_wrapper around the new node entity // to make getting and setting values easier $ewrapper = entity_metadata_wrapper('node', $entity); // Using the wrapper, we do not have to worry about telling Drupal // what language we are using. The Entity API handles that for us. $ewrapper->title->set('YOUR TITLE'); $my_body_content = 'A bunch of text about things that interest me.'; $ewrapper->body->set(array('value' => $my_body_content)); $ewrapper->body->summary->set('Things that interest me.'); $ewrapper->body->format->set('filtered_html'); // Now just save the wrapper and the entity. $ewrapper->save(); }
Custom fields added to a content type using the GUI will have a
human label and a machine name. The machine name will start
with field
, usually followed by a lowercase ASCII
version of the project name and field label separated by underscores.
When using the wrapper, values can be assigned to the machine name using
the set()
method. Example:
$ewrapper->field_myproject_myfieldname->set('value');
Below are some notes about setting specific field types.
Entity reference field
To set the value of an entity reference field, you pass the nid of the entity to which you want to refer as an integer. The nid used here (42) is just an example:
$ref_nid = 42; $ewrapper->field_my_entity_ref->set(intval($ref_nid));
Date field
For date fields, you can use the set()
method if the
value is an integer (Unix epoch time).
$ewrapper->field_myproject_datefield->set(REQUEST_TIME);
The value must be passed as an integer (not a string).
Not using the ewrapper
Some fields, such as date fields using other formats, cannot be set using the Entity API ewrapper, so the old method for storing the value must be used. Example:
$my_date = new DateTime('January 8, 2019'); $entity->field_my_date[LANGUAGE_NONE][0] = array( 'value' => date_format($my_date, 'Y-m-d'), 'timezone' => 'UTC', 'timezone_db' => 'UTC', );
Accessing user fields [D9]
use Drupal\user\Entity\User;
$user = User::load(\Drupal::currentUser()->id()); $uid = $user->id();
Source DSE: Access user fields?.
Final word
[TBA]
Last update: 2019-01-08 [gh].