Drupal Entity Reference
Blog

How to create a Drupal Entity programmatically in Drupal 8

Entities have been introduced late in Drupal 7. Drupal entity is an instance of a particular instance type. For example, the entity type for taxonomy terms is named taxonomy_term and the class name Term. We include a class like  Drupal\taxonomy\Entity\Term. Here taxonomy is a core module name, and Term is the class name of Entity.

Related: How to Create Custom Entity in Drupal 8 for Content Management

A. How to create a user programmatically.

The fields to be created are as follows:

  1.  Username
  2.  Email address
  3.  Password
  4.  Status
  5.  Roles
  6.  Picture
  7.  Timezone

If you want to create a Drupal user programmatically we can create it the way as given below:

a) This is the first method

  The output is shown below:user create

Related: How to Create Configurable Block programmatically In Drupal 8

The use of Drupal\user\Entity\User; is to include the class User.

User::create()is used to create a user where it takes the details about the user.

Here name is the username whose account is to be created.

Here mail is the email id of the user and pass is the password for the user.

The status tells you if the user account is active or blocked. If it is 1 then the user account is active and if it is 0 then the account is blocked.

The roles mention which roles is given to the user. To give the user more than one role simply pass an array of roles.

The timezone mentions a timezone set for the user.

The user_picture accepts the image’s file id to get the user image. To get the file id we have passed a static image here in $file_image='/var/www/html/drupal/sites/default/files/2017-04/images.jpeg'; The file_get_contents() reads a file into a string. $directory is the name of the folder where you want to save the uploaded image. file_prepare_directory($directory, FILE_CREATE_DIRECTORY); The above line creates the folder if it doesn't exist. The file_save_data saves the files details. The first parameter takes the file content, the second parameter takes the name of the folder you want to save your image and the name of the file, the third parameter tells you to replace the file if it already exists.

b) This is the second way to create user  

The first parameter of entity_create accepts the entity type that we want to create and the second parameter accepts an array of values to be used to create that entity. The user created is saved to finally create the user.

Related: How to add Product Programmatically to Drupal Commerce Cart

B. How to create a node programmatically

If you want to create a drupal node programmatically we can create the way as given below:

a) This is the first way to do this:

 

node create

 This will be the output.

Here the use \Drupal\node\Entity\Node; is used to insert the class Node.

Node::create() is used to create a node.

Inside Node::create() the node details is passed.

The type tells you the content type whose node is to be created.

The title is the node title, body is the body part of a node.

The field_mail is the email id to be entered if you have a field for email.

The field_link allows you to add a link if you have any link field and field_date holds the value of date field.
 

b) This is the second way to create the node   

  The entity creates function takes two parameters where the first parameter tells you which entity to create and the second parameter should be an array of the node details. 

C. How to create a taxonomy term programmatically

If you want to create taxonomy term programmatically we can create the way as given below:

a) This is the first way to create term

  The output is shown below:

term create

   In above code use Drupal\taxonomy\Entity\Term; is used to insert the class Term

  Term::create has been used to create the term where Term is the class name.

 

Here name and vid are machine names where name represents the name of the new term you want to create, and vid is the name of the taxonomy term whose term you want to create.

b) This is the second way to create a term 

The entity create function takes two parameter where the first parameter tells you which entity to create and the second parameter should be an array of the term details.

 It should contain the term name and the taxonomy name whose term you want to create. You can add the term description but it’s optional.

D. How to create a custom menu

Create a form inside the Form folder as given in the code below.In Drupal 8 modules we write a form inside a Form folder.

Create a routing file which is module_name.routing.yml  Here menu.route is the routing name and _form is the path of the form. 

In module_name.links.menu.yml file write the code for the menu as shown below:

  links.admin is the name of the menu.

The title will appear as the menu name.

The parent tells you the place where the menu will be created like system.admin will create the menu in the main navigation. If the parent is not mentioned then the menu will be created in the tools.

Related: How to create a custom block programmatically in Drupal 8

  The route name is the name of the routing file of whose menu you want to create.

The output is as follows.

menu

  Here TechX is the menu created.

E. How to create a custom permission

 Taking the example of the module above, create a file and name it as module_name.permissions.yml

  Write the following code into it:

 Here 'Menu permission': is the name of the permission that we mention in the routing file ,

 The title gives you the name displayed in the drupal's permissions page. In routing file under _permission we mention this permission name. So whichever role has this permission can view the form.

This is how drupal permissions are given to a particular page when you don’t want to show the page for every user or role.

So here we have learnt how to create entities in drupal 8 programmatically. In drupal 8 you can create node, user, taxonomy without code, but sometimes due to various project requirements and we need to create these programmatically.

Related:

How to send mail programmatically in Drupal 8

Download the Drupal Guide
Enter your email address to receive the guide.
get in touch