1

I'm using the views_json module in Views Datasource to send some information in JSON format to a related website. I've got a bunch of content views built, but now I'm trying to build staff pages. I've got most of it working fine, but when I try to add the user picture I end up with an empty field, When trying to find a url for the user's profile page (using User: Link), I got the text "view", leading me to believe that when drupal tries to output the content as a link, views_json returns the link text. I just want the url in plaintext, I don't need the html for a link. How can I get the user picture, and the absolute url to the user profile in views_json?

Image fields for my node views work just fine, I get field_image.src and field_image.alt elements in the JSON that is returned, with the Use picture I get a blank field or a series of \n´s

1

To get the user pic url, you can do it using Image URL Formatter. Once enabled, when you click on the user picture field, you will now see a formatter option, select image url.

For the user, assuming this is a user view, in FIELDS add the User: Uid as a field and checkmark ☑ exclude from display.

Now, add Global: Custom text as a field. In the text box, put http://ultrabob.com/user/[uid]

I got [uid] from replacement patterns. Side note, in order for fields to appear as a replacement pattern, they must be above the current field.

Else, if you're using pathauto so the user pages are /user/[username] then add the User: Name field instead and use the [name] token.

Now, if your view is a content view, ex: shows articles, then you will need to add a relationship of node author first, once you do that, the user fields will become available to be added as a field.


OK, that issue got me sorted with the pictures, I'm now working on the solution for profile page link. Can you think of a good way to get the domain part of the url (http://ultrabob.com/) in your example dynamically in views from the site configuration. I would like this to continue to work without modification as it is deployed across test servers and onto production

You can using php. Ex: <?php echo $_SERVER['HTTP_HOST']; ?>

Either you could enable the Views PHP module, but remember to turn it off on production, or use a template file.

views-view-field--[view-name]--views-data-export--[field-name].tpl.php

<?php

$fields["field_name"]->content = 'http://' . $_SERVER['HTTP_HOST'] . '/user/' . $fields["field_name"]->content;

echo $fields["field_name"]->content;
  • hmmm, I've enabled the Image URL Formatter module, cleared cache and reloaded the view, but I'm still not seeing any formatter option. I wonder what I'm missing. – UltraBob Jul 13 '16 at 1:58
  • I clicked back over to my node view and confirm I get the formatters on my node image field. It seems not to work for user picture. I found an open issue which I'll go investigate. – UltraBob Jul 13 '16 at 2:07
  • This is a great answer and got me working toward the right answer for this particular situation. In the end I managed to get it all solved in a hook_preprocess_views_views_json_style_simple call, so I'll put up an answer with that solution in it. Thanks as always for your great help! – UltraBob Jul 13 '16 at 3:44
  • @UltraBob oh cool, didn't know about that hook. – No Sssweat Jul 13 '16 at 3:46
1

UPDATE: There was a follow up comment on the drupal.org thread where someone suggested using Views User Picture URL. Since the solution below already works for me I haven't tried it, but this solution seems easier and reasonable. /UPDATE

@no-sssweat provided a superb answer, but it turned out that my problem was the result of a bug in views_datasource in that thread, @tyler-frankenstein provides a really useful solution. Based on that I was able to get both the url path and profile link issues resolved:

/**
 * Implements hook_preprocess_views_views_json_style_simple().
 * solution for user pictures not showing up properly in views_json
 * via https://www.drupal.org/node/2396813#comment-10461959
 */
function MY_MODULE_preprocess_views_views_json_style_simple(&$vars) {
    $views_to_process = array(
        'user_view' => array(
            'uid_column_name' => 'uid',
            'picture_column_name' => 'picture',
            'image_style' => 'medium',
            'url_column_name' => 'url',
        ),
        'my_other_user_view' => array(
            'uid_column_name' => 'uid',
            'picture_column_name' => 'picture',
            'image_style' => 'medium',
            'url_column_name' => 'url',
        )
    );
    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'];
    $url_column_name = $views_to_process[$vars['view']->name]['url_column_name'];
    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];
        $vars['rows'][$i][$url_column_name] = url(drupal_get_path_alias('user/' . $uid), array('absolute' => TRUE));
        if (!$users[$uid]->picture) { continue; }
        $vars['rows'][$i][$picture_column_name] = array(
            'src' => image_style_url($image_style, $users[$uid]->picture->uri)
        );
    }
}

replacing user_view and my_other_user_view with the names of views this needs to work on and putting the label names in the uid_column_name, picture_column_name and url_column_name as well as filling in the value for the image style you've chosen for your picture field will result in your picture and url fields being filled with data like so:

"url" : "http://link.to.the/users/profile",
"photo" : {
  "src" : "http://link.to.the/users/profile/picture/with/styles/applied/properly.jpg?itok=ipLMAws_"
},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.