Add the Body field back in Drupal 7

Sat, 13/08/2016 - 19:48

In many cases the Body field in Drupal's content types can be completely unnecessary. The best examples would be nodes used for sliders, galleries, panels and more complex types where textarea field has no place.

The best practice in such cases is just to delete the default Body field. This will make the node edit / create forms cleaner and site editors will not be confused.

However as the project evolves and new requirements are added, you might end up needing a textarea for storing larger amount of text. Creating another field is not really efficient and whenever field configuration permits, you should try to share fields between content types and user accounts. This is because Drupal adds at least 2 tables to the database for each field you create. One table (field_data_YOUR_FIELD) will be used for storing the current data of your field while the other one (field_revision_YOUR_FIELD) will keep the revision information. Both of these tables are created automatically and even when you are not using revisioning for your content.

In such cases the best solution would be to add back the Body field. It already exists in the database and no new tables will be created. You will notice that Drupal will not list it in the available fields list so you will not be able to add it back using the admin panel.

However, this is easily done by executing the following code:

// Get the list of all content types in your website.
$content_types = node_type_get_types();
// Add the body field. Note that
node_add_body_field($content_types['YOUR_CONTENT_TYPE'], $label = 'Body');

You can run this either through a custom module (e.g. hook_update_N()) or even easier - through Devel's page for executing PHP code. Once you install the Devel module (which you should have installed anyway) you can access the page at /devel/php. Just paste the code above and execute it.

That's all - the new field will appear again in your content type and new tables will not be created.

Last thing: the behavior of Body field has been "fixed" in Drupal 8. If you remove the field, it will be still visible in the list of available fields, so you can do this without executing any code.

See also: