I'm making a module to report the total values of a field for all nodes of content type and I can't figure out how to get the display value of a select fields options.

The module is for field statistics, specifically in my case for sales tracking. I have a taxonomy select field called "Status". The options are like 'No Updates', 'Sold', 'Not Interested', etc. My module would calculate each status and display the totals like this

No Updates: 5
Sold: 10
Not Interested: 10

But what I am getting is this

1:5
2:10
4:10

How can I get the actual value names?

Comments

nevets’s picture

What does your code look like?

ErnestoJaboneta’s picture

<?php
function fieldstats_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $output = '';
                   
  if($display['type']=='fsformat') {
    foreach ($items as $delta => $item) { 
      $tablename = 'field_data_'.$item['fs_field'];
      $tablefield = $item['fs_field'].'_value';
      $result = db_query('SELECT DISTINCT n.'.$tablefield.' AS value, COUNT(*) AS count FROM {'.$tablename.'} n GROUP BY  n.'.$tablefield);
      
      foreach ($result AS $record) {
        $output .= '<p>'.$record->value.':'.$record->count.'</p>';
      }
    }     
    if(isset($output)) {
      $element[$delta] =  array('#markup' => $output);    
    }
  }
     
  return $element;
}

?>
nevets’s picture

How about changing

      $result = db_query('SELECT DISTINCT n.'.$tablefield.' AS value, COUNT(*) AS count FROM {'.$tablename.'} n GROUP BY  n.'.$tablefield);

to

      $result = db_query("SELECT DISTINCT t.name AS value, COUNT(*) AS count FROM {$tablename} n JOIN {taxonomy_term_data} t ON(t.tid = n.$tablefield) GROUP BY  n.$tablefield");

which should replace tid with it's name

ErnestoJaboneta’s picture

Turns out my field is a select list field. Not a taxonomy select field. Where are the values for that stored?