0

The users on my website have an integer field My medals, which can contain an unlimited amount of values, representing nids from nodes of the type Medal. Every Medal node contains one term of the taxonomy Colour (either Gold, Silver and Bronze).

Given a certain uid, I would like to create a View that shows the total number of nodes per taxonomy term, based on the user's field My medals.

Example. Suppose the nodes with nids 1, 2 and 3 have a Gold term, the node with nid 4 has a Silver term, and the node with nid 5 has a Bronze term. Let user 1 have a My medals field with values 1, 2, 4, 5. Then the View should return "Gold: 2, Silver: 1, Bronze: 1" when the uid 1 is given.

1 Answer 1

0

Mmm i dont think drupal 7 views is up to this perhaps this work around may help create a custom module and put

/**
 * Implements hook_block_info().
 */
function mymodule_block_info() {
  $blocks = [];
  $blocks['my_block'] = [
    'info' => t('My Medals'),
  ];
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function mymodule_block_view($delta = '') {
  $block = array();
  switch($delta) {
    case 'my_block' :
      $block['content'] = my_block_view();
      break;
  }
  return $block;
}

function my_block_view() {
  global $user;
  $uid = $user->uid;

  $result = db_query('SELECT * FROM {node} WHERE uid = :uid AND type = :type', [
   ':uid' => $uid,
   ':type' => 'post'
  ]);
  $node_ids = [];
  foreach ($result as $row) {
    $node_ids[] = $row->nid;
  }
  $output = '';
  $gold = [];
  $silver = [];
  $bron = [];
  if (count($node_ids) != 0) {
    foreach ($node_ids as $nid) {
      $node = node_load($nid);
      $tid = $node->field_term['und'][0]['tid'];
      switch($tid) {
        case 1;
          $gold[] = $nid;
        break;
        case 2;
          $silver[] = $nid;
        break;
        case 3;
          $bron[] = $nid;
        break;
        default;
        break;
      }
    }
  }
// bad practice but quick win. 
  $output = 'GOLD ' . count($gold) . ' Silver ' . count($silver) . ' Bronze ' . count($bron);
  $block = [
    '#type' => 'markup',
    '#markup' => $output,
  ];
  return $block;
}

The above will create a custom block then you have to place the block on the user and change the "global $user; $uid" to fit your need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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