I have a content type with two fields one called display which is a simple list and the other is an entityreference field.

I would like to change how the entity reference field is rendered depending on which option is selected in the first field.

I have custom formatters installed and I have created two custom formatters so I need a way of changing the #formatter based on the result in the first field.

I've tried a hook_preprocess_node() and also changing it in a custom tpl file but neither seem to render the field any differently to how it is set in the manage field UI.


I would try using the Context module API and adding a new reaction plugin.

  • Yeah I think the overhead of context is a little overkill Mar 19 '14 at 16:32

In the end I did this in the tpl file for my content type using field_view_field(), I hide both fields the one that contains the option and the second that contains the data. I then use a case statement to set the correct formatter.

  print render($content);
  $links_display_mode = $content['field_display_mode']['#items']['0']['value'];
  switch($links_display_mode) {
    case 1:
      $links_formatter = 'custom_formatters_entry_form_display_one';
    case 2:
      $links_formatter = 'custom_formatters_entry_form_display_two';
  $field_link = field_view_field('node', $node, 'field_link', array('label' => 'hidden', 'type' => $links_formatter));
  print render($field_link);

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.