drupal 8

The field system of Drupal has made it quite popular. There are still some restrictions associated with it that keep emerging on a periodic basis. For example: if you have a field named field_one_liner that is of 64 characters. After creating 30 nodes, you gave a thought that field size should have been 255. But, if you perform this from Drupal’s field management UI, a message will pop up saying: “There is data for this field in the database. The field settings can no longer be changed.”

The most common way to resize it is by removing the current field. This does not makes great sense because it can also be done by increasing the field size with the help of SQL.

This tutorial shares the steps to increase the size of existing text (plain) field in Drupal 8 without making sure that the data is not lost and using hook_update_N()

Requirements:

If you will use the code given above, ensure that you have the field given in any node type.

Name: One-liner
Machine name: field_one_liner
Type: Text (Plain)
Length: 64

Once the field is configured, design some nodes with data on the One-liner field.

Decreasing the field length will lead to data damage.

Using hook_update_N()

Hook_update_N() allows you to run commands to update the database schema. You can easily make, update and erase database tables/columns using this hook once the module has been installed. To make use of this hook, you should have s custom module.

For example: The hook is implemented in a custom module. This is named as custom_field_resize. All the custom modules are named as custom_ to namespace. In the case of custom module, we make use of the hook in a MODULE install file. Here MODULE is the machine name of a website.

 

To modify the field size, there are 4 things to do:

Resizing columns

A set of queries is made to run that updates the relevant database columns.

 

If revisions are disabled, node_revision__field_one_liner table will not exist. You can also remove the second query if your entity does not allow revisions.

Updating Storage Schema

Resizing columns with query is not the only solution. Drupal keeps a record of what database schema is currently installed. If this is skipped, Drupal will think that database schema is required to be updated because column lengths in database won’t match the configuration storage.

 

This code will update the key value table to store the updated length of field_one_liner in the configuration.

Updating field configuration

There are other places where Drupal saves the configuration. Next step is informing the Drupal config management that the field length is 255.

 

Drupal also saves information about the actively installed configuration and schema. To restore this, you need to re-save the field storage configuration and detect all changes.

 

After doing so, running drush updb or update.php from the admin will detect hook_update_N(). It will update the field size.

Ending Notes!

We can restructure the field type by making use of SQL. There are certain situations in which it is required to form a temporary table with a new structure of data. You just need to copy the existing data into the table with queries. Once the data has successfully been copied, you will have to replace the present table with temporary table.

So, you can use these simple and effective tips to resize fields. If you have any query, please let us know by leaving the comments in the section below.