All RPGs and Storygames by Tod Foley are now available at DrivethruRPG. Bring these games to your table!
Specbee: Have you leveraged Drupal’s Computed Field Module yet? – Get started now with this quick guide
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.Figure 1- Enable the Computed Field ModuleUsing 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.
Figure 2 - Select the Field Type
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.
Figure 3 - Add the Computed (integer) Field
Figure 4 - Setting up the Drupal Computed Field
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.
Figure 5 - Calculating the Age within the PHP Code text area
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.
Figure 6- Entering the Date of Birth
Step 6 - After saving the content the Age will be populated automatically (as shown in the image below)
Figure 7 - Calculated Age presented to the user
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.
The Computed Field module is a very useful module in Drupal 8 that proves handy in various scenarios. Especially when you need to populate a field whose value depends on a computation of two or more fields. However, the module also comes with its own security risks since it allows for PHP code to be inserted via a Web user interface. Hence, it is always recommended to be used and implemented by developers with sound skills in PHP and Drupal. There are other similar modules like the Views PHP (Drupal 7), the Math Field (for Drupal 7) and the Field Token Value modules. Nonetheless, the Drupal Computed Field Module is exactly what you need sometimes.Drupal Planet Shefali ShettyApr 05, 2017 Subscribe For Our Newsletter And Stay Updated Subscribe
Leave us a CommentShefali ShettyApr 05, 2017 Recent Posts Image Have you leveraged Drupal’s Computed Field Module yet? – Get started now with this quick guide Image How these Drupal SEO Modules (and tips) can boost your Website Ranking Image A quick guide to Drupal Sitemaps (and why you need one) Looking to optimize your Drupal site for SEO in the best way possible ? Talk to us Featured Success Stories
Know more about our technology driven approach to recreate the content management workflow for 7.ailink
Find out how we transformed the digital image of world’s largest healthcare provider, an attribute that defined their global presence in the medical world.link
Develop an internal portal aimed at encouraging sellers at Flipkart to obtain latest insights with respect to a particular domain.link
Microsoft-owned Mojang has received the go-ahead to give PlayStation 4 players the ability to play alongside those playing on Xbox One, Switch, and mobile. ...
At GDC 2019 Bungie's Jonathan To discusses Lord Shaxx's creation as Crucible announcer in the Destiny series, and how the character grew from supporting character to fan favorite. ...
The goal of this module is to provide the ability to navigate forward and backward through a web form using the Webform Progress tracker (aka wizard progress bar) supplied by the webform module on multi-page web forms. It also allows for the ability to bypass validation when the user presses the "next" button as they navigate through the form.
With Drupal 9 set to be released later next year, upgrading to Drupal 8 may seem like a lost cause. However, beyond the fact that Drupal 8 is superior to its predecessors, it will also make the inevitable upgrade to Drupal 9, and future releases, much easier.
Acquia puts it best in this eBook, where they cover common hangups that may prevent migration to Drupal 8 and the numerous reasons to push past them.The Benefits of Drupal 8
To put it plainly, Drupal 8 is better. Upon its release, the upgrade shifted the way Drupal operates and has only improved through subsequent patches and iterations, most recently with the release of Drupal 8.8.0.
Some new features of Drupal 8 that surpass those of Drupal 7 include improved page building tools and content authoring, multilingual support, and the inclusion of JSON:API as part of Drupal core. We discussed some of these additions in a previous blog post.
Remaining on Drupal 7 means hanging on to a less capable CMS. Drupal 8 is simply more secure with better features.What Does Any of This Have to Do With Drupal 9?
With an anticipated release date of June 3, 2020, Drupal 9 will see the CMS pivot to an iterative release model, moving away from the incremental releases that have made upgrading necessary in the past. That means that migrating to Drupal 8 is the last major migration Drupal sites will have to undertake. As Acquia points out, one might think “Why can’t I just wait to upgrade to Drupal 9?”Read more
The year has come to a close - and what a year it has been! For Agiledrop as well as for Drupal, this has been a truly amazing and successful year - and yet, we feel it is only a prelude to all that’s coming in 2020. To properly end the year, here’s a recap of all our posts from last month - enjoy!READ MORE