Hi!

Very useful module! Thx a lot!

But there is an issue. The module uses the entity language key to retrieve the field value:

  $lang = LANGUAGE_NONE;
  if (isset($context['entity']->language)) {
    $lang = $context['entity']->language;
  }
...
  $display_mode = $context['entity']->{$field_name}[$lang][0]['entity_display'];

this is not right. The entity language is often different than the field language. I provided a patch to use the right language key.

CommentFileSizeAuthor
field_language_patch.patch1.04 KBckidow

Comments

xtfer’s picture

Status: Active » Needs review

Thanks for that. I will review.

ckidow’s picture

A more elegant solution is to use field_get_items:

function entity_display_field_entity_view_mode_alter(&$view_mode, $context) {
  // Get our 'entity_display_field' names;
  $display_fields = field_read_fields(array('type' => 'entity_display_field'));

  // If display fields are present, swap the view mode.
  if (!empty($display_fields)) {
    foreach (array_keys($display_fields) as $field_name) {
      if (!empty($context['entity']->{$field_name})) {
        
        $display_modes = field_get_items($context['entity_type'], $context['entity'], $field_name);
        
        if(!empty($display_modes)) {
          $display_mode = $display_modes[0]['entity_display'];

          if (!empty($display_mode) && strtolower($display_mode) != 'default') {
            $view_mode = $display_mode;
            break;
          }
        }
      }
    }
  }
}