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

Drupal example source code file (views_handler_relationship_translation.inc)

This example Drupal source code file (views_handler_relationship_translation.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, current, default, field, function, if, join, language, options, php, select, string, to, translation

The views_handler_relationship_translation.inc Drupal example source code

<?php
// $Id: views_handler_relationship_translation.inc,v 1.1 2008/09/30 22:07:15 merlinofchaos Exp $

/**
 * Handles relationships for content translation sets and provides multiple
 * options.
 */
class views_handler_relationship_translation extends views_handler_relationship {
  function option_definition() {
    $options = parent::option_definition();
    $options['language'] = array('default' => 'current');

    return $options;
  }

  /**
   * Add a translation selector.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);

    $options = array(
      'all' => t('All'),
      'current' => t('Current language'),
      'default' => t('Default language'),
    );
    $options = array_merge($options, locale_language_list());
    $form['language'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $this->options['language'],
      '#title' => t('Translation option'),
      '#description' => t('The translation options allows you to select which translation or translations in a translation set join on. Select "Current language" or "Default language" to join on the translation in the current or default language respectively. Select a specific language to join on a translation in that language. If you select "All", each translation will create a new row, which may appear to cause duplicates.'),
    );
  }

  /**
   * Called to implement a relationship in a query.
   */
  function query() {
    // Figure out what base table this relationship brings to the party.
    $table_data = views_fetch_data($this->definition['base']);
    $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];

    $this->ensure_my_table();

    $def = $this->definition;
    $def['table'] = $this->definition['base'];
    $def['field'] = $base_field;
    $def['left_table'] = $this->table_alias;
    $def['left_field'] = $this->field;
    if (!empty($this->options['required'])) {
      $def['type'] = 'INNER';
    }

    $def['extra'] = array();
    if ($this->options['language'] != 'all') {
      switch ($this->options['language']) {
        case 'current':
          $def['extra'][] = array(
            'field' => 'language',
            'value' => '***CURRENT_LANGUAGE***',
          );
          break;
        case 'default':
          $def['extra'][] = array(
            'field' => 'language',
            'value' => '***DEFAULT_LANGUAGE***',
          );
          break;
        // Other values will be the language codes.
        default:
          $def['extra'][] = array(
            'field' => 'language',
            'value' => $this->options['language'],
          );
          break;
      }
    }

    if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
      $join = new $def['join_handler'];
    }
    else {
      $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, $this->definition['base'], $this->relationship);
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal views_handler_relationship_translation.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.