Currently printing files with a view using Rendered Entity (from Entity API) row style plugin fails with error:

Notice: Undefined index: ... in entity_views_plugin_row_entity_view->render() (line ... of ... /modules/entity/views/plugins/entity_views_plugin_row_entity_view.inc)

.

The reason is that file_view_multiple() returns not what is expected by entity_views_plugin_row_entity_view->pre_render(). It returns sorted files array without wrapping files into 'files' sub-array. I.e. expected result:

array(
  'files' => array(
     1 => array( file_view data ),
     2 => array( file_view data ),
    ... => array( file_view data ),
  ),
  '#sorted' => TRUE)

Current behavior:

array(
   1 => array( file_view data ),
   2 => array( file_view data ),
  ... => array( file_view data ),
  '#sorted' => TRUE)

As the result, entity_views_plugin_row_entity_view->pre_render() after executing code:

  $render = entity_view($this->entity_type, $this->entities, $this->options['view_mode']);
  $this->rendered_content = reset($render);

assigns rendered_content the value of the first file, stripping entity_id, and the subsequent call to entity_views_plugin_row_entity_view->render() is unable to render an entity as it tries to get entity_view data by id (which is now missed):

$render = $this->rendered_content[entity_id($this->entity_type, $entity)];

Why I think this is file_view_multiple() problem? Because the equivalent function from node.module called node_view_multiple() also wraps node entities into 'nodes' sub-array:
http://drupalcode.org/project/drupal.git/blob/3a24da1b40f5e05876ad777504...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

OnkelTem’s picture

Status: Active » Needs review
FileSize
1.61 KB

Providing the patch.

Dave Reid’s picture

I'm a little torn about changing this API. On the one hand, yes that seems the proper thing. But also core is inconsistent. node_view_multiple() and taxonomy_term_view_multiple() use a first-level key, and the second level as the entity IDs. But comment_view_multiple() does what we're doing. How does entity API handle that comment_view_multiple() returns the unexpected syntax?

+++ b/file_entity.field.incundefined
@@ -102,7 +102,7 @@ function file_entity_field_formatter_view($entity_type, $entity, $field, $instan
       }
-      $files_built = file_view_multiple($files_display, $view_mode);
+      $files_built = file_view_multiple(reset($files_display, $view_mode));
     }

This change seems to be unrelated and not sure what its purpose is?

OnkelTem’s picture

How does entity API handle that comment_view_multiple() returns the unexpected syntax?


function entity_metadata_view_comment($entities, $view_mode = 'full', $langcode = NULL) {
  $build = array();
  $nodes = array();
  // The comments, indexed by nid and then by cid.
  $nid_comments = array();
  foreach ($entities as $cid => $comment) {
    $nid = $comment->nid;
    $nodes[$nid] = $nid;
    $nid_comments[$nid][$cid] = $comment;
  }
  $nodes = node_load_multiple(array_keys($nodes));
  foreach ($nid_comments as $nid => $comments) {
    $node = isset($nodes[$nid]) ? $nodes[$nid] : NULL;
    $build += comment_view_multiple($comments, $node, $view_mode, 0, $langcode);
  }
  return array('comment' => $build);
}

So it forces 'comment' injecting.

This change seems to be unrelated and not sure what its purpose is?

Thanks for finding this, it is definitely mistypo and should read as:

$files_built = reset(file_view_multiple($files_display, $view_mode));

which just strips that first-level key and allows to leave the rest of code untouched.

OnkelTem’s picture

Actually, another approach to fix this — is to create Entity API wrapper like this:

/**
 * Callback to view files.
 */
function entity_metadata_view_file($entities, $view_mode = 'full', $langcode = NULL) {
  $result = file_view_multiple($entities, $view_mode, 0, $langcode);
  // Make sure to key the result with 'file'.
  return array('file' => $result);
}

and this was my first fix. But then I realized that Entity API should check for presence of 'file_entity' module which I didn't consider as good idea. Maybe I'm wrong...

p.s. In the above code "#sorted" gets under first-level 'file' key too. This is no good of course, it should be processed correctly.

OnkelTem’s picture

FileSize
1.61 KB

Updated patch (with 'reset' changes from #3)

Cale Bierman’s picture

I've used the updated patch from #5 and created a view using Rendered Entity row style that has a list of files and I've had no errors.

Cale Bierman’s picture

Status: Needs review » Reviewed & tested by the community
Dave Reid’s picture

Status: Reviewed & tested by the community » Fixed

Committed #5 to 7.x-2.x! Thanks Cale and OnkelTem! http://drupalcode.org/project/file_entity.git/commit/2bd1b4c

After reviewing the list of functions that call file_view_multiple(), I also made a small commit to the mediafield sub-module in Media as well: http://drupalcode.org/project/media.git/commit/e5d43ab

Status: Fixed » Closed (fixed)

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