10 Dec, 2019
Computed Field Module is extremely beneficial to a Drupal developer at various events. There are times when you have tons of Fields, a few of which can be consolidated into one field. Sometimes, you might need a field that is a computation of two or more fields, which does not need user inputs. Or you might just want to store the current user’s data directly into the database.
When you want to populate content automatically without user inputs in Drupal, we can leverage the Computed Field Module for Drupal 8. This module is a very powerful field module that allows us to insert custom calculated/computed fields via PHP code. These values can be either stored directly in the database or can be calculated while using node views.
This module comes with a security warning as the custom code entered in the field is going to be saved in the database. Adding PHP code in the text area (“Insert your working code here”) without the use of Hooks in custom modules, is a huge threat to the security of your website. This module is recommended to be used by developers with good skills and knowledge in PHP and Drupal APIs. It is also recommended that you first test your PHP code in the Body field of a Drupal page.
Installing the Drupal 8 Computed Field Module:
Initially, you will need to download the Drupal Computed Field module from this link – https://www.drupal.org/project/computed_field/releases/8.x-2.0 or you can Install with the Composer : composer require ‘drupal/computed_field:^2.0’
Once you have installed the module, navigate to extent and enable the Computed field module.
Using Computed Field to automatically populate content
Step1 – Once you have enabled the module, navigate to Structure -> content- type -> select your content type. For now, I have selected the Article content type.
Step 2 – Go to Manage fields, add field of type Computed field. There are 5 types of computed fields –
- Computed (decimal): This field automatically populates as decimal value based on the php code.
- Computed (float): This field automatically populates a floating value based on the php code.
- Computed(integer): This field automatically populates an integer valued based on the php code.
- Computed (text): This field automatically populates a string values based on the php code.
- Computed (text, long): This field is the same as Computed text but takes more text.
Time to Try it out! Let us now automatically calculate and populate the age of a person based on their Date of Birth that the user has entered.
Step 3 – First, we will have to add the Computed Field. In our case I am going to calculate the Age of a person so I will add the field by navigating to
Configuration -> Account settings -> Manage fields. Lets’ select the Computed (integer) field to populate the Age automatically.
When we add the Computed (integer) field and give a Label (name of the calculated field), click on Save. We will then see this window as shown in the image below.
Step 4 – In the settings window, you can add an optional Help text that instructs the user on what the calculated field value is about. The most important part, however, is the text-area called “Code (PHP) to compute the integer value”. Here is where we write the custom code to calculate the Age (in this case). Below the text area you will see some security warnings and some suggestions/recommendations. Also mentioned are a few example variables that can be used in the PHP code. In our case, I am going to write the PHP custom code to calculate Age in the PHP code text area. To automatically populate the Age.
Inside the PHP code text area, we can also use “if” conditions to populate the content automatically.
Step 5 – After writing the custom code for the Compute Field, hit the save button. Next, lets’ test this by entering the Date of Birth.
Step 6 – After saving the content the Age will be populated automatically (as shown in the image below)
This is a very simple example of using the Drupal 8 Computed Field Module but similarly you can populate values with various types of computations and custom code.