2

When hook_installing a new custom node type filled with custom fields I call field_create_instance($instance) where I also set the custom fields' display like so:

'display' => array(
  'default' => array( // view mode
    'label' => 'hidden',
    'type' => 'text_default'
  ),
  'teaser' => array( // view mode
    'label' => 'hidden',
    'type' => 'hidden'
  )
)

I also added a body field via node_add_body_field($type) but where can I set that body's display properties programmatically? Per default it's set to be displayed in the teaser view. I want to hide it.

2 Answers 2

1

If you want to create the body field for the new content type by invoking node_add_body_field(), then your only option is to implement hook_field_create_instance() to modify the display values of the instance.

This doesn't seem very efficient. Since you are creating other field instances programmatically, you might as well also create a body field instance for your new content type by re-using/copying the code of node_add_body_field in your custom function, which will allow you to set the display values to whatever you want.

0

You can access and alter the view mode configuration via field_bundle_settings(), (which is basically a shortcut to variable_get and variable_set):

$bundle = 'my_node_type';
$bundle_settings = field_bundle_settings('node', $bundle);
$bundle_settings['view_modes']['teaser']['custom_settings'] = true;
field_bundle_settings('node', $bundle, $bundle_settings);
2
  • Is it possible this doesn't work for Drupal's body field?
    – leymannx
    Commented Oct 14, 2015 at 7:00
  • Body is a field just like any other (you can even remove it). A common problem during installation are stale caches. Try to run your steps over multiple processes/requests first. Commented Oct 14, 2015 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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