Add/Remove columns using the updater service#
Modifying an existing field api field after the field tables contain data has historically been a challenging and unintuitive set of tasks. Fortunately, the Custom Field module provides a service that makes this incredibly easy!
In your custom module, create or edit an existing .install file and add an update hook. You will need to leverage the methods from the provided service providing arguments as documented. Before you proceed, backup your database and adequately test these updates in a development environment!
As of version 3.1.8, we recommend using the following Drush commands in your local environment which will interactively prompt you for all of the parameters needed, automatically perform the add/remove function and generate an example update hook for deployment to other environments.
Add a new column#
- Enter
drush custom_field:add-column - Follow the prompts to select options for the new column
- Copy the generated update hook to your module and deploy to your environment
The addColumn() method provided by the updater service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
$entity_type_id(string) – A valid content|config entity type machine name containing the custom field.$field_name(string) – The machine name of the existing custom field.$new_property(string) – The machine name of the subfield to add.$data_type(string) – The plugin id of the subfield to create. See plugin options.$options(array) – An array of options to set for the new column.max_length(int)- Applicable plugins: string, telephone
unsigned(bool)- Applicable plugins: integer, float, decimal
size(string)- Applicable plugins: integer, float
- Allowed values: tiny, small, medium, big, normal
precision(int)- Applicable plugins: decimal
scale(int)- Applicable plugins: decimal
target_type(string) – A valid content|config entity type machine name.- Applicable plugins: entity_reference,
datetime_type(string)- Applicable plugins: datetime, daterange
- Allowed values: date, datetime
Remove an existing column#
- Enter
drush custom_field:remove-column - Follow the prompts to select options for the column to remove
- Copy the generated update hook to your module and deploy to your environment
The removeColumn() method provided by the updater service:
1 2 3 4 5 6 7 8 9 10 11 | |
Example Update Hooks#
The following examples demonstrate how to use the updater service to add and remove columns from custom_field. These examples would be placed in a .install file in your module. These examples would normally be generated by running the applicable drush commands above.
Adding a new column#
1 2 3 4 5 6 7 8 | |
Removing a column#
1 2 3 4 5 6 7 8 | |