Token Module Walk Through: Using it to Automate Paragraph Blocks Admin Title

Published Oct 19, 2024
Updated Oct 19, 2024
By Simon
Table of contents

Token module is one of those behind the scenes must have modules. You will have installed if you are using Pathauto and Metatags module.

In this article, we are going to look at how you can use it to add an automatic title to Paragraph blocks module. It is a walk through of the Token module as much as it is of the admin title feature that was added to the 4.x version of Paragraphs block module. (https://www.drupal.org/project/paragraph_blocks/releases/4.0.0

As stated, you will possibly already have token module installed, If you haven't, install it like any other Drupal module.

Token https://www.drupal.org/project/token

Next, make sure you have the latest 4.x version of Paragraph Blocks module installed. At the time of writing, it is 4.1.4.

Set the Default Admin Title for Paragraphs Block

Next, navigate to one of your paragraph types edit page. 

Manage > Structure > Paragraph types > [Type Label]
/admin/structure/paragraphs_type/[machine_name]

Image
paragraph type edit page

In this example, we are on the edit page of the Hero paragraph type. You can see the Default admin title field. This is new in 4.x. 

Related: Set up a Paragraph Type

To add a default title, you can add plain text, but since we have the Token module installed, we can also use tokens.

To use the token:

  • Click on the Browse available tokens. 

A modal will open and load all the tokens that are available.

Image
availabe tokens modal

Since we are setting up for a paragraph, find the paragraphs section and open it using the small arrow. 

We can use any token, but I think the type will be good for this, and then also use the heading. We use the machine names of our field, so in case it is field_heading. 

You can place the cursor in the admin title field and then click on the [token] to insert into your form. 

Image
adding token to a field

Once you have the pattern set up, click Save.

Add a new Paragraph type instance

Let's see this in action. 

Go to a content type that has the Hero Paragraph type available.  

Add a Hero paragraph instance

You will see that the field now has a default value. Yes, it is the token value we entered on the edit page when setting up the paragraph type in the step above. 

Image
paragraph instance with admin title and heading field

Fill in the other fields and then click save. 

This is a great way to automate a field value, and when you go to use your paragraph block, the tokens will be replaced by the values. If you edit a field value, in this case, the heading, it will automatically update the admin title where ever it is rendered.

Image
add paragraph block in layout bulier

You can add a custom admin title by overriding the default value. If you do this, you may need to remove the token, as it is limited to 100 characters.

I think for the Paragraphs block module, this is a great solution as it solves the issue of needing an admin title but also allows you to overwrite it. 

Well, that is for using the Token module for automating the admin title in paragraphs blocks module. 

Summary

We learnt how you can set up a default admin title for your paragraph type using tokens. We then look at how the token works by add a new instance of a paragraph type. In doing this, we saw how tokens are replaced by field values on render, meaning that you can change the values at any time and the change will be reflected across the site. 

Finally, we talked about how you can replace the token with a custom plain text value.

Now you have enough knowledge to use tokens in fields that can use them. 

Thanks for dropping by the kojo and I hope you learnt something. I write about making Drupal easier for you and also front-end design and development. Be sure to sign up to my newsletter in the pop-up over there to the right or below. 

Until next time, seize the day!