home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (views_handler_relationship_node_term_data.inc)

This example Drupal source code file (views_handler_relationship_node_term_data.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, array_filter, empty, form, function, join, options, php, string, td, term_data, term_node, that, vocabularies

The views_handler_relationship_node_term_data.inc Drupal example source code

<?php
// $Id: views_handler_relationship_node_term_data.inc,v 1.3 2009/10/05 22:31:09 merlinofchaos Exp $
/**
 * @file
 * Views' relationship handlers.
 */

class views_handler_relationship_node_term_data extends views_handler_relationship  {
  function option_definition() {
    $options = parent::option_definition();
    $options['vids'] = array('default' => array());
    return $options;
  }

  /**
   * Default options form that provides the label widget that all fields
   * should have.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $vocabularies = taxonomy_get_vocabularies();
    $options = array();
    foreach ($vocabularies as $voc) {
      $options[$voc->vid] = check_plain($voc->name);
    }

    $form['vids'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Vocabularies'),
      '#options' => $options,
      '#default_value' => $this->options['vids'],
      '#description' => t('Choose which vocabularies you wish to relate. Remember that every term found will create a new record, so this relationship is best used on just one vocabulary that has only one term per node.'),
    );
  }

  /**
   * Called to implement a relationship in a query.
   */
  function query() {
    $this->ensure_my_table();

    $def = $this->definition;
    $def['table'] = 'term_data';

    if (!empty($this->options['required']) || !array_filter($this->options['vids'])) {
      $term_node = $this->query->add_table('term_node', $this->relationship);
      $def['left_table'] = $term_node;
      $def['left_field'] = 'tid';
      $def['field'] = 'tid';
      if (!empty($this->options['required'])) {
        $def['type'] = 'INNER';
      }
    }
    else {
      // If the join is optional, join a subselect that will emulate term_data table instead
      $def['left_table'] = $this->table_alias;
      $def['left_field'] = 'vid';
      $def['field'] = 'revision';
      // fapi ensures vids are safe here.
      $vids = implode(', ', array_filter($this->options['vids']));
      $def['table formula'] = "(SELECT td.*, tn.vid AS revision FROM {term_data} td INNER JOIN {term_node} tn ON tn.tid = td.tid WHERE td.vid IN ($vids))";
    }

    $join = new views_join();

    $join->definition = $def;
    $join->construct();
    $join->adjusted = TRUE;

    // use a short alias for this:
    $alias = $def['table'] . '_' . $this->table;

    $this->alias = $this->query->add_relationship($alias, $join, 'term_data', $this->relationship);
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal views_handler_relationship_node_term_data.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.