I'm running Entity View Modes 7.x-1.0-beta3+9-dev on Drupal 7.19 and PHP 5.3.3. I have two custom view modes defined in hook_entity_view_mode_info() of two different modules. No other custom view modes are defined with the module (nothing in the variable).

These are my hook implementations:

function Aslideshow_entity_view_mode_info() {
  $info = array();

  $info['node']['slide'] = array(
    'label' => 'Slide',
    'custom settings' => 1,
  );

  return $info;
}

and

/**
 * Implements hook_entity_view_mode_info().
 */
function Bsite_entity_view_mode_info() {
  $info = array();

  $info['taxonomy_term']['button'] = array(
    'label' => 'Button',
    'custom settings' => 1,
  );

  return $info;
}

Instead of getting the "button" view mode on taxonomy terms and the "slide" view mode on nodes, I end up with the slide view mode on both nodes and taxonomy terms.

I debugged this and found the problem in entity_view_mode_entity_info_alter().


  // Because entity_get_info() merges in the default 'custom settings' value
  // before it invokes hook_entity_info_alter(), we need to make sure that the
  // custom view modes also have this value defined.
  foreach ($view_mode_info as $entity_type => &$view_modes) {

In the loop above, the $view_modes variable is passed by reference through the loop.


  // Add in the combined custom entity view modes which override the existing
  // view modes in the entity information.
  foreach ($view_mode_info as $entity_type => $view_modes) {

When the next loop uses the same variable without unsetting it, it's still referencing the last value in the array from the previous loop. That's also the array in this loop. The first set of view modes is replacing the last set of view modes.

This wasn't obvious to me, so excuse me if my explanation is long-winded.

CommentFileSizeAuthor
#3 1955992-no-more-reference.patch865 bytesdave reid
#1 evm_array_reference-1955992-1.patch500 bytesAnonymous (not verified)

Comments

Anonymous’s picture

Status: Active » Needs review
StatusFileSize
new500 bytes

The attached patch fixes it for me, but I can't explain why.

damienmckenna’s picture

Status: Needs review » Reviewed & tested by the community

This patch works for me.

In my scenario I had added some custom view modes to node, and everything was fine. Once I added a view mode to another entity type, in my case 'user', the custom view modes from 'node' were added to 'user' and the one I specifically added didn't show.

That said, the variable itself was fine, just the entity info was corrupt.

dave reid’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new865 bytes

Rather than unsetting, it's better if I don't assign variable by reference in the foreach() loop and then attempt to use the same variable name in the same scope/function. Can either of you test this version out?

damienmckenna’s picture

Status: Needs review » Reviewed & tested by the community

That works too.

dave reid’s picture

Status: Reviewed & tested by the community » Fixed

Ok thanks all for patching and testing. Committed #3 to 7.x-1.x.
http://drupalcode.org/project/entity_view_mode.git/commit/a518484

dave reid’s picture

Status: Fixed » Active
Issue tags: +Needs tests

I wonder if we should leave this open to figure out if we can get a test condition written that would have exposed this bug.