blog-banner

Drupal 7 - Exposing Pseudo Field in View From Custom Table

  • Custom Table
  • Drupal 7
  • EXPOSING FIELD
  • Views
  • VIRTUAL

Drupal Custom Table Views

Introduction:

At KnackForge we often see requirements to expose custom fields to views for better integration with Drupal flow. In this article, we are going to see steps to expose a custom field from a custom table. In fact, it's a virtual field.

Use case:

Assume you have two custom fields total_marks and marks_obtained in a custom table. Now you need to calculate the percentage of marks scored. You don't need a field in the table for this. You can create a virtual field bound to the marks_obtained field.

Hooks to be used:

hook_views_data_alter() or hook_views_data()

hook_views_api()

hook_views_handlers()

I assume the custom table has already been created using the .install file.

Implementation:

In your custom module write,

/**
 * Implements hook_views_api().
 */
function MY_MODULE_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'MY_MODULE') . '/views/',
  );
}
 
this tells Drupal that we have some views related to customizations. Create a views folder under your module, with a file called, MY_MODULE_.views.inc.
 
/**
 * Implementation of hook_views_data_alter().
 */
function MY_MODULE_views_data_alter(&$data) {
  $data['CUSTOM_TABLE_NAME']['percentage'] = array(
      'title' => t('Percentage obtained'),
      'help' => t('Shows percentage obtained'),
      'real field' => 'marks_obtained',
      'field' => array(
        'handler' => 'MY_MODULE_views_handler_field_percentage',
        'click sortable' => TRUE,
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
        'name_field' => 'title',
        'string' => TRUE
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
        'help' => t('Filter results to a particular result set'),
      ),
      'sort' => array('handler' => 'views_handler_sort'),
  ); 
}
 
The above defines our virtual field bound to marks_obtained field for use in views as a field.
 
/**
* Implements hook_views_handlers()
*/
function MY_MODULE_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'MY_MODULE') . '/views/handlers',
    ),
    'handlers' => array(
      'MY_MODULE_views_handler_field_percentage' => array(
        'parent' => 'views_handler_field',
      )
  );
}
 
Place a "handlers" folder inside MY_MODULE and write a file called MY_MODULE_views_handler_field_percentage.inc.
 
Inside that file write,
 
 * @file
 * Handles filtering by networks
 */
class MY_MODULE_views_handler_field_percentage extends views_handler_field {
 function render($values) {
    $total_marks = $this->options['total_marks'];
    $marks_obtained = $this->options['marks_obtained'];
    $tokens = $this->get_render_tokens('');
    if(!empty($tokens)) {
      $total_marks = $tokens[$total_marks];
      $marks_obtained = $tokens[$marks_obtained];
      return ($marks_obtained/$total_marks) * 100;
   }
   return 0;
}
  function option_definition() {
    $options = parent::option_definition();
    //define our custom settings fields
    $options['total_marks'] = array();
    $options['marks_obtained'] = array();
    return $options;
  }
  function options_form(&$form, &$form_state) {    
    $options = array();
    foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
      $options[t('Fields')]["[$field]"] = $handler->ui_name();
    }    
    $form['total_marks'] = array(
      '#type' => 'select',
      '#title' => t('Total marks'),
      '#options' => $options,
      '#default_value' => isset($this->options['total_marks']) ? $this->options['total_marks'] : '',
      '#required' => TRUE
    );
    $form['marks_obtained'] = array(
      '#type' => 'select',
      '#title' => t('Marks obtained'),
      '#options' => $options,
      '#default_value' => isset($this->options['marks_obtained']) ? $this->options['marks_obtained'] : '',
      '#required' => TRUE
    );
    parent::options_form($form, $form_state);
}     
  function options_submit(&$form, &$form_state) {
    parent::options_submit($form, $form_state);
    //update our custom values
    $this->options['total_marks'] = $form_state['values']['options']['total_marks'];
    $this->options['marks_obtained'] = $form_state['values']['options']['marks_obtained']; 
   }
}
 
options_definition() and options_form() method let us define custom options for our field, in which we attach the previously included fields in view i.e total _marks and marks_obatined which are necessary for our field to work. It will show a select box where we can choose the fields that are available as views fields.
 
options_submit() method lets us to get the submitted values.
 
render() method is responsible for the final result of the field. We use get_render_tokens() method to get available tokens for that row and get the total_marks and marks_obtained values. Then we return the final percentage.

Let me know if you have any queries :)

Get awesome tech content in your inbox