4

I am on D7. In the past, I have deleted the body field from a certain content type in my project. Now, the problem is that I want the body field back into this same content type, but cannot find it in the list of existing fields in my project to be added to the content type. How can I add the deleted body field back into the content type? Is it possible? It is important to say that I don't want its content back. I just want the regular Body field with the original ID body, instead of creating a "field_body". Thanks.

12

In a hook_update_n(), module_install(), module_enable(), or devel module eval php code:

$types = node_type_get_types();
node_add_body_field($types['my_content_type_name']);

giving it the machine name of your content type, and optionally you may provide a label for what you want the human readable label for the field to be (defaults to 'Body'). see node_add_body_field() API documentation

  • tenken, is it possible to do it from the Rules module? Are there any actions there where I could run custom PHP code and get this body field created? Thank you. – Marcos Buarque Jul 3 '12 at 13:48
  • wow really dont know typcially you make fields/content types and then Rules acts on these created types/things ... I wouldnt be able to guess what you would hook into to fire this before you try to access this field to add $body content to anything ... maybe look at drupal.org/project/entitysync .... – tenken Jul 3 '12 at 21:39
  • See below comment - this example won't work, $type is an object. – Renee May 7 '13 at 22:36
  • @Renee I havent declared what $type is here in code. My code links to the actual API documentation. I assume the developer knows what $type an element from the $types array. I try to make my examples as clear as possible, sorry if it fell short -- you can edit my answer if you want. – tenken May 8 '13 at 1:05
2

In Drupal 7, I think that if you create a new content type, it appears with default "body" field. So it appears again in "existing fields" menu too. Then you can select it for any other conent type and after that you can delete the new content type created for this pourpose.

2

With drush I used

drush  php-eval '$types = node_type_get_types(); node_add_body_field($types["content_type_name"], $label="Body");'
  • Hello and welcome. Please avoid code-only answers. Explanation is worth more - especially if your code will happen not to work in OP's environment exactly the way it works in yours. – Mołot Aug 8 '14 at 9:19
  • Good example of how to do this with drush thanks ! – undersound Oct 7 '16 at 10:00
1

In a hook_update_n or module_install() or module_enable $types = node_type_get_types(); node_add_body_field($types['my_content_type_name'], $label='Body');

  • This looks very similar to the previous answer... – Boriana Ditcheva Mar 14 '13 at 17:42
  • 1
    This answer is correct. node_add_body_field expects the first parameter to be a node type object. – avr Apr 4 '13 at 18:05
1

insert directly into the field_config_instance table: mysql> INSERT INTO {{database}}.field_config_instance ( field_id, field_name, entity_type, bundle, data, deleted) VALUES ( '2', 'body', 'node', '{{your-content-type}}', '{{blob-data}}', '0');

I copied the blob data separately from an existing content type via MySQLWorkbench and omitted 'id' from the query, which is an auto-increment field

1

Just found out that if you simply have another content type created and has the default BODY field on it, you can simply add it to your needed content type from the Add existing field section on the Manage fields tab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.