开发者

Creating a view of users who have created nodes

开发者 https://www.devze.com 2023-02-05 17:57 出处:网络
Ba开发者_如何学编程sically i want to creeate a block diaplay view,which displays a list of all the users thata have posted some nodes on the drupal website.Oddly enough thinking about this right now i

Ba开发者_如何学编程sically i want to creeate a block diaplay view,which displays a list of all the users thata have posted some nodes on the drupal website.


Oddly enough thinking about this right now it could be a little tricky. You have two possible solutions off the top of my head.

1 - Create a new view of item type Node. Your row style will obviously be set to Fields. Under which Fields to pull select the User group and then tick off the User: Name checkbox. Set your Items to display setting to 0 for unlimited results.

Under the preview you should get a ton of results looking something like:

Name: John Doe

Name: Mary Jane

Name: John Doe

Name: Anonymous

What you're seeing is the authors of all the nodes posted in your system. There will be duplication because a user in your system could be the author of multiple nodes. Unfortunately you can't just tick off the Distinct: Yes option because this only applies to nodes and not the users.

How to deal with the duplicate user name results tho? Custom theme your view by creating a custom template under Theme: information. Inside the template write some PHP code which intercepts the row results from the View query before it renders and only render distinct user names from the results. You'd have to write the logic though to determine whether a user name has already been added.

As simple as creating a new custom array, adding each row result (user name) to array but first checking to see whether it already exists in your custom array - if it does then toss it and move on to the next user name. At the end you'll have an array filled with distinct user names who have posted on your site.

voila! It works. It's not elegant but it definitely will work if built this way.

2 - Alternatively maybe you can get this module working to accomplish the same thing in a less complicated manner: http://drupal.org/project/views_customfield but I have never used it so I cannot comment on it.

Good luck. Hope that helps.


My solution was to:

  • Create a view of people
  • Add a UID field (and any other fields you want)
  • Create a theme.tpl.php file for the Row Style
  • Do a DB call on each loop through the row to search for nodes created by the supplied UID.

Here is what I have in my semanticviews-view-fields-VIEWNAME.tpl.php

<?php
  //Query the Drupal DB for nodes associated to the supplied UID
  $existing_nid = db_query("SELECT nid FROM {node} WHERE (type = :ctype) AND uid = :uid", array("ctype" => "CONTENT_TYPE", "uid" => $fields['uid']->content))->fetchField();

  //If the supplied UID created content of the supplied type, then show their name
  if ($existing_nid != FALSE) {
    echo "Name:" . $fields['name']->content;
  }
?>

This way only UID's that have content associated to it in the DB will be printed out, and those that don't, won't.

Hope that helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消