{
      "user" : {
        "name" : "yu123456789",
        "uid" : "11",
        "picture" : " \n \n"
      }
    },

all user picture path cann't display.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

g089h515r806’s picture

I have the same issue.

coolman7’s picture

I have same issue.

Strutsagget’s picture

seems to be there still

tyler.frankenstein’s picture

Status: Needs work » Active

Sadly I just spent about 2 hours on this and wasn't able to solve it. Here are the major players (in relative order AFAIK):

  1. _views_json_render_fields() in views_json.module
  2. render() in views_handler_field_user_picture.inc
  3. template_preprocess_user_picture() in user.module
  4. user-picture.tpl.php

This spans through Views JSON, to Views, to Drupal core. I've placed a bunch of watchdog messages along the way and the picture seems to be built just fine, but the images always gets rendered as an empty string in the render function in views_handler_field_user_picture.inc:

// Fake an account object.
// ...
// This line always comes back as an empty string.
$output = theme('user_picture', array('account' => $account));

Oddly, if you switch the Views Format to table, the picture will be displayed, but watchdog still reports the output as an empty string in views_handler_field_user_picture.inc.

I'm starting to wonder if the fact that theme_user_picture() doesn't exist, and instead it gets fed to the preprocessor and ultimately to user-picture.tpl.php has anything to do with it.

tyler.frankenstein’s picture

Title: account.picture path cann't display » user account picture JSON rendered as " \n \n"
tyler.frankenstein’s picture

Here's a workaround:

/**
 * Implements hook_preprocess_views_views_json_style_simple().
 */
function my_module_preprocess_views_views_json_style_simple(&$vars) {
  $views_to_process = array(
    'my_user_view' => array(
      'uid_column_name' => 'uid',
      'picture_column_name' => 'picture',
      'image_style' => 'thumbnail'
    ),
    'my_other_user_view' => array(
      'uid_column_name' => 'uid',
      'picture_column_name' => 'picture',
      'image_style' => 'medium'
    )
  );
  if (!in_array($vars['view']->name, array_keys($views_to_process))) { return; }
  $uids = array();
  $root = $vars['options']['root_object'];
  $child = $vars['options']['top_child_object'];
  $uid_column_name = $views_to_process[$vars['view']->name]['uid_column_name'];
  $picture_column_name = $views_to_process[$vars['view']->name]['picture_column_name'];
  $image_style = $views_to_process[$vars['view']->name]['image_style'];
  foreach ($vars['rows'][$root] as $i => $row) {
    $uid = $row[$child][$uid_column_name];
    $uids[] = $uid;
  }
  $users = user_load_multiple($uids);
  foreach ($vars['rows'][$root] as $i => $row) {
    $uid = $row[$child][$uid_column_name];
    if (!$users[$uid]->picture) { continue; }
    $vars['rows'][$root][$i][$child][$picture_column_name] = array(
      'src' => image_style_url($image_style, $users[$uid]->picture->uri)
    );
  }
}

Replace the values in $views_to_process with your View's machine name(s), the name of your uid and picture columns, and the name of your image style. You can add more than one view to process.

dr-wanni’s picture

@tyler.frankenstein where do i put the above code, in views_datasource/views_json.module?

tyler.frankenstein’s picture

@dr-wanni in a custom module: https://www.drupal.org/developing/modules/7

ultrabob’s picture

Thanks tyler.frankenstein, this was a godsend. The code above assumes you have root and child names set. If you don't then you'll need to modify the code just a little bit to remove the extra root and child levels:

/**
 * Implements hook_preprocess_views_views_json_style_simple().
 */
function my_module_preprocess_views_views_json_style_simple(&$vars) {
  $views_to_process = array(
    'my_user_view' => array(
      'uid_column_name' => 'uid',
      'picture_column_name' => 'picture',
      'image_style' => 'thumbnail'
    ),
    'my_other_user_view' => array(
      'uid_column_name' => 'uid',
      'picture_column_name' => 'picture',
      'image_style' => 'medium'
    )
  );
  if (!in_array($vars['view']->name, array_keys($views_to_process))) { return; }
  $uids = array();
  $uid_column_name = $views_to_process[$vars['view']->name]['uid_column_name'];
  $picture_column_name = $views_to_process[$vars['view']->name]['picture_column_name'];
  $image_style = $views_to_process[$vars['view']->name]['image_style'];
  foreach ($vars['rows'] as $i => $row) {
        $uid = $row[$uid_column_name];
        $uids[] = $uid;
    }
    $users = user_load_multiple($uids);
    foreach ($vars['rows'] as $i => $row) {
        $uid = $row[$uid_column_name];
        if (!$users[$uid]->picture) { continue; }
        $vars['rows'][$i][$picture_column_name] = array(
            'src' => image_style_url($image_style, $users[$uid]->picture->uri)
        );
    }
}
coolman7’s picture

Try this module:

https://www.drupal.org/project/views_upu

It worked for me.

anthonylindsay’s picture

Status: Active » Closed (works as designed)
FileSize
230.68 KB

I don't think there's anything wrong with the module: if you get \n\n outputs, the chances are that you are outputting your JSON as plaintext.
If you turn off plaintext output you should get the desired output.

Have a look at the attached screenshot.

tyler.frankenstein’s picture

Status: Closed (works as designed) » Active

@anthonylindsay thank you for the reply. Turning off the "Plaintext output" does return the HTML for the profile picture. I'd say this isn't very useful for consumption since applications will probably want to render it their own way.

I believe most are looking for, and the way it used to work just prior to the alpha 1 release, is to output the src of the picture including the image style (but at the same time embrace the new format that is used when outputting image fields, i.e. include the alt/title, even though there probably isn't any).

I'd vote to keep this issue active, but I understand if you wish to close it. Please advise, thanks.

anthonylindsay’s picture

Status: Active » Closed (works as designed)
FileSize
188.87 KB

I had another go at this and had some success with it using a relationship: "File Usage: File". This gave me access to the path and alt text. Now, the path did not respect the image style, so that wasn't ideal. I was also able to strip down the returned HTML for the image to a very minimalist image tag, with none of Drupal's usual wrapper gunk by changing the view field settings. I also tried the views_upu module, and that does a nice job of allowing you to output the url of the picture, respecting the image style.

Have a look at the attached screenshot: you'll see an alt text field (alas there's no content for it, but still) and a path field, and the minimalist img output. That's what we get out of the box. With the addition of views_upu you can extract the img src and display just that. I think I'd like to call this one closed, given the data we can access with the file usage relationship and what views_upu can bring to the table. And for those with needs unfulfilled by the modules, Tyler's preprocess hook does offer more options.