Hi,

I hope this is the right module queue for this question.

Is there a way I can change the options for a field's settings on the 'manage display' tab via code?

I'd like to change visibility and render mode, and maybe weight as well, for a particular custom field I have defined but don't want to use the GUI to make the change. This is so that in our environment I can more easily manage changes during our development process that won't require manual steps during our deployment window.

Ideally, I'd like to run this php code from a hook_update() function.

Thanks,
Cliff

Comments

tasc’s picture

When programmatically creating new taxonomy_vocabularies I need to have some additional fields on its terms. So I attach a field-collection-field and change the display-settings this way:

  $array_instances = array(
    'field_migration_data' => array(
      'field_name' => 'field_migration_data',
      'entity_type' => 'taxonomy_term',
      'bundle' => $vocabulary_name,
    ),  
  );
  
  foreach ($array_instances as $instance) {
    $field = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);

    if (empty($field)) {
      $field = field_create_instance($instance);
    }
    // field_migration_data is a field-collection and I don't want to have links displayed
    $field['display']['default']['settings']['edit'] = '';
    $field['display']['default']['settings']['delete'] = '';
    $field['display']['default']['settings']['add'] = '';

    field_update_instance($field);

  }
yan’s picture

Title: programmatically change display options » Programmatically change display settings of a field
Version: 7.x-1.4 » 7.x-1.6
Status: Active » Fixed

Thanks for the hint, tsc_a. So I think basically it's a combination of field_info_instance() and field_update_instance() as described on the update functions page:

// Fetch an instance info array.
$instance_info = field_info_instance($entity_type, $field_name, $bundle_name);
// Change a single property in the instance definition.
$instance_info['required'] = TRUE;
// Write the changed definition back.
field_update_instance($instance_info);

To alter the display settings, that would be inside the $instance_info['display'] array.

For example, to hide the node body from teaser on a 'story' node type, you would use:

// Fetch an instance info array.
$instance_info = field_info_instance('node', 'body', 'story');
// Change a single property in the instance definition.
$instance_info['display']['teaser']['type'] = 'hidden';
// Write the changed definition back.
field_update_instance($instance_info);

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

RAWDESK’s picture

Thanks for #1 and #2 !
I've used this to update node field instance display settings, for newly created fields coming from a remote data feed.

ryanoreilly’s picture

I've successfully been able to update the value of a display setting. I'm using a preprocess function to update the image style based on number of entities referenced. However, after node save, it's requiring a manual refresh in order for the updated display value to show.

The image style value is correct on node save, but the different image style won't display until the page is refreshed again after save. Any suggestions?

function THEME_preprocess_entity(&$variables) {
  if ($variables['entity_type'] == 'paragraphs_item') {
    if ($variables['elements']['#bundle'] == 'BUNDLE') {
      $promo_fields = field_info_instance('node', 'FIELD', 'promo');
      $promo = $variables['field_promotions'];
      $promo_count = count($variables['field_promotions']);
      $promo_img_styles = array(
        1 => 'promotion_1x',
        2 => 'promotion_2x',
        3 => 'promotion_3x'
      );
      $promo_img_style = str_replace(array_keys($promo_img_styles), $promo_img_styles, $promo_count);
      $promo_fields['display']['full']['settings']['image_style'] = $promo_img_style;
      field_update_instance($promo_fields);
    }
  }
}