I would like to have a view where I can see the content from all of my buddies, who belong to a specified buddy group.
For example I could create such a view that the URL content_by_buddy_group/11 would show all the content belonging to buddy group 11.
I saw this is not implemented yet, so I started to work on it, but as somebody said, "I don't have the ninja power" to completely understand the Views API (at least yet).

It's not working yet. I would need buddylist_buddy_group to be join-ed with users, but apparently it's not. I don't know what's wrong. When I go to a similar URL as given above it says:

user warning: Unknown column 'buddylist_buddy_group.label_id' in 'where clause' query: SELECT count( DISTINCT(node.nid)) FROM node node INNER JOIN users users ON node.uid = users.uid LEFT JOIN node_comment_statistics node_comment_statistics ON node.nid = node_comment_statistics.nid WHERE (node.status = '1') AND (buddylist_buddy_group.label_id = 17) in
\includes\database.mysqli.inc on line 151.

PLEASE HELP !!!

The patch also contains a fix (lines of 'list-type' => 'select'). When I created this fix I gave it the explanation of "value of the filter left empty if the only possible item is not selected from the list when creating the view)".

Hope the patch is working, this is my first patch created by diff -up as read in http://drupal.org/patch/create.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

attila75’s picture

Apparently I left some unnecessary copy-pasted code, and did not include any fields in the table definitions. Investigating...

attila75’s picture

I still don't know what's wrong, I need a Views guru.
I attached the latest state of the patch, still not working (same error).
Any insights on how to make it work would be highly appreciated!
Have a good weekend.

attila75’s picture

Sorry for the spam. I thought that I mention that the name and help text of fields as well as the name of the argument handler and maybe other stuff may need correction. Changing them to some sensible values would be appreciated as well.

attila75’s picture

I've got it working. Here you are. I hope it doesn't have any bugs. Enjoy.


function buddylist_views_tables() {

#<snip>

  $tables['buddylist_buddy_group'] = array(
    'name' => 'buddylist_buddy_group',
    'join' => array(
      'type' => 'inner',
      'left' => array(
        "table" => "users",
        "field" => "uid",
      ),
      'right' => array(
        'field' => 'buddy'
      ),
    ),
    'fields' => array(
      'buddy' => array(
        'name' => t('Buddylist: Buddy Group Buddy'),
        'help' => t('Buddy Group Buddy.'),
      ),
      'label_id' => array(
        'name' => t('Buddylist: Buddy Group ID'),
        'help' => t('Buddy Group ID.'),
      ),
    ),
  );

  $tables['buddylist_groups'] = array(
    'name' => 'buddylist_groups',
    'join' => array(
      'type' => 'inner',
      'left' => array(
        "table" => "buddylist_buddy_group",
        "field" => "label_id",
      ),
      'right' => array(
        'field' => 'label_id'
      ),
    ),
    'fields' => array(
      'label_id' => array(
        'name' => t('Buddylist: Buddy Group ID'),
        'help' => t('Buddy Group ID.'),
      ),
    ),
  );

#<snip>
}

/**
 * Implementation of hook_views_arguments
 */
function buddylist_views_arguments() {
  $arguments = array();
  if (module_exists('usernode')) {
    $arguments = array(
      'buddylist_buddies' => array(
        'name' => t('Buddylist: Usernode is buddy of UID'),
        'handler' => 'buddylist_views_handler_arg_buddies',
        'help' => t('This will filter usernodes from users, which are buddies of the user with the ID passed to the view as argument.'),
      ),
      'buddylist_buddies_buddyof' => array(
        'name' => t('Buddylist: UID is buddy of Usernode'),
        'handler' => 'buddylist_views_handler_arg_buddies_buddyof',
        'help' => t('This will filter usernodes from users, of which the user with the ID passed to the view as argument is a buddy of.'),
      ),
    );
  }
  $arguments['buddylist_buddylists_buddyof'] =
    array(
      'name' => t('Buddylist: Buddylist ID'),
      'handler' => 'buddylist_views_handler_arg_buddylists_buddyof',
      'help' => t('The Buddylist ID argument allows users to filter to nodes authored by buddies who are in the the specified Buddylist.'), 
    );
  return $arguments;
}

/**
 * Callback for buddylist_views_arguments
 * "$op is the operation, $query is the query object being manipulated, and $a1
 * and $a2 vary based upon the $op field."
 */
function buddylist_views_handler_arg_buddylists_buddyof($op, &$query, $a1, $a2 = '') {
  global $user;

  switch($op) {
    case 'summary':
      /*
        "$a1 is the argument type (the key to the array that defines the
        argument), and $a2 is the option the user selected, if applicable.

        The handler must create the summary query, which requires adding the
        right fields and filters to the $query object and returning an array
        of information that tells Views how to interpret the summary query
        results. If needs to perform a $query->add_field() on any fields that
        will be used to display the summary.
      */

      $table_data = _views_get_tables();
      $query->add_table('buddylist_groups', true);
      $query->add_field('label_id', 'buddylist_groups');
      $query->add_field('label', 'buddylist_groups');
      $query->add_where("buddylist_buddy_group.uid = %d", $user->uid);

      $fieldinfo['field'] = "buddylist_groups.label";
      return $fieldinfo;
      break;
    case 'sort':
      /*
        "$a1 is the sort direction; either ASC or DESC.
        $a2 is unused.

        The handler must apply the proper sorting. This handler will not always
        be called, as the user can select "
      */ 
      $query->add_orderby('buddylist_groups', 'label', $a1);
      break;
    case 'filter':
      /*
        "$a1 is the full database information of the argument (including the
        'options').
        $a2 is the actual arg from the URL.

        The handler must apply a filter for the given argument."
      */

      $buddylist_id = intval($a2);
      $query->add_table("buddylist_buddy_group", true);
      $query->add_where("buddylist_buddy_group.label_id = %d", $buddylist_id);
      // Restrict result to the buddylist of the current user, do not allow
      // seing posts of other's buddylists.
      $query->add_where("buddylist_buddy_group.uid = %d", $user->uid);
      break;
    case 'link':
      /*
        "$query is not the query object in this case; it is actually the raw
        data object returned from the query for this record in the summary view.

        $a1 is the argument type.
        $a2 is the base of the URL, which is usually the URL of the summary view.

        During a summary view, this operation is called to provide a link to the
        view with that argument for each result. This should be run through l()."
      */

      // $a1 is the key of the array returned by the implementation of
      // hook_views_arguments.
      // We have access to those fields in $query, which we added when
      // $op == 'summary'.
      return l($query->label, "$a2/$query->label_id");
    case 'title':
      /*
        "$query is not the query object in this case; it is actually the raw
        argument used.

        $a1 is the argument type.
        $a2 is unused.

        When creating a view that uses an argument, or a breadcrumb based on
        an argument, this is used to create the title. This is largely used to
        translate the title into 'human readable' data."
      */
      $group = db_fetch_object(db_query("SELECT label FROM {buddylist_groups} WHERE label_id = %d", $query));
      return check_plain($group->label);
  }
}

attila75’s picture

Status: Needs work » Needs review

Changing status.

dldege’s picture

This sounds like a good idea - can you please submit this as a patch file.

http://drupal.org/patch/create

gagarine’s picture

track