diff --git a/modules/field/field.attach.inc b/modules/field/field.attach.inc index 30a12d0..9638054 100644 --- a/modules/field/field.attach.inc +++ b/modules/field/field.attach.inc @@ -1131,12 +1131,16 @@ function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcod $options['language'] = array(); - // To ensure hooks are only run once per entity, only process items without - // the _field_view_prepared flag. + // To ensure hooks are only run once per entity per view mode, only process + // items without the _field_view_prepared[$view_mode] flag. // @todo: resolve this more generally for both entity and field level hooks. $prepare = array(); foreach ($entities as $id => $entity) { - if (empty($entity->_field_view_prepared)) { + // An entity can be rendered in more than one view mode on given page + // e.g. in preview mode and the display options for a field vary so we need + // to take view mode into account when processing elements and record the + // view mode that the enitity has been built for. + if (empty($entity->_field_view_prepared[$view_mode])) { // Add this entity to the items to be prepared. $prepare[$id] = $entity; @@ -1145,7 +1149,9 @@ function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcod $options['language'][$id] = field_language($entity_type, $entity, NULL, $langcode); // Mark this item as prepared. - $entity->_field_view_prepared = TRUE; + $entity->_field_view_prepared = array( + $view_mode => TRUE, + ); } } diff --git a/modules/node/node.test b/modules/node/node.test index b1d78fa..d93fa56 100644 --- a/modules/node/node.test +++ b/modules/node/node.test @@ -457,10 +457,72 @@ class PagePreviewTestCase extends DrupalWebTestCase { } function setUp() { - parent::setUp(); + parent::setUp(array('taxonomy')); $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content')); $this->drupalLogin($web_user); + + // Add a vocabulary so we can test different view modes. + $vocabulary = new stdClass(); + $vocabulary->name = $this->randomName(); + $vocabulary->description = $this->randomName(); + $vocabulary->machine_name = drupal_strtolower($this->randomName()); + $vocabulary->langcode = LANGUAGE_NOT_SPECIFIED; + $vocabulary->help = ''; + $vocabulary->nodes = array('page' => 'page'); + $vocabulary->weight = mt_rand(0, 10); + + taxonomy_vocabulary_save($vocabulary); + + $this->vocabulary = $vocabulary; + + // Add a term to the vocabulary. + $term = entity_create('taxonomy_term', array( + 'name' => $this->randomName(), + 'description' => $this->randomName(), + // Use the first available text format. + 'format' => db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField(), + 'vid' => $this->vocabulary->vid, + 'langcode' => LANGUAGE_NOT_SPECIFIED, + )); + taxonomy_term_save($term); + + $this->term = $term; + + // Setup a field and instance. + $this->field_name = drupal_strtolower($this->randomName()); + $this->field = array( + 'field_name' => $this->field_name, + 'type' => 'taxonomy_term_reference', + 'settings' => array( + 'allowed_values' => array( + array( + 'vocabulary' => $this->vocabulary->machine_name, + 'parent' => '0', + ), + ), + ) + ); + + field_create_field($this->field); + $this->instance = array( + 'field_name' => $this->field_name, + 'entity_type' => 'node', + 'bundle' => 'page', + 'widget' => array( + 'type' => 'options_select', + ), + // Hide on full display but render on teaser. + 'display' => array( + 'default' => array( + 'type' => 'hidden', + ), + 'teaser' => array( + 'type' => 'taxonomy_term_reference_link', + ), + ), + ); + field_create_instance($this->instance); } /** @@ -470,9 +532,11 @@ class PagePreviewTestCase extends DrupalWebTestCase { $langcode = LANGUAGE_NONE; $title_key = "title"; $body_key = "body[$langcode][0][value]"; + $term_key = "{$this->field_name}[$langcode]"; // Fill in node creation form and preview node. $edit = array(); + $edit[$term_key] = $this->term->tid; $edit[$title_key] = $this->randomName(8); $edit[$body_key] = $this->randomName(16); $this->drupalPost('node/add/page', $edit, t('Preview'));