[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/handlers/ -> views_handler_field_boolean.inc (source)

   1  <?php
   2  
   3  /**
   4   * A handler to provide proper displays for booleans.
   5   *
   6   * Allows for display of true/false, yes/no, on/off.
   7   *
   8   * Definition terms:
   9   *   - output formats: An array where the first entry is displayed on boolean true
  10   *      and the second is displayed on boolean false. An example for sticky is:
  11   *      @code
  12   *      'output formats' => array(
  13   *        'sticky' => array(t('Sticky'), ''),
  14   *      ),
  15   *      @endcode
  16   *
  17   * @ingroup views_field_handlers
  18   */
  19  class views_handler_field_boolean extends views_handler_field {
  20    function option_definition() {
  21      $options = parent::option_definition();
  22      $options['type'] = array('default' => 'yes-no');
  23      $options['not'] = array('definition bool' => 'reverse');
  24  
  25      return $options;
  26    }
  27  
  28    function init(&$view, $options) {
  29      parent::init($view, $options);
  30  
  31      $default_formats = array(
  32        'yes-no' => array(t('Yes'), t('No')),
  33        'true-false' => array(t('True'), t('False')),
  34        'on-off' => array(t('On'), t('Off')),
  35      );
  36      $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
  37      $this->formats = array_merge($default_formats, $output_formats);
  38    }
  39  
  40    function options_form(&$form, &$form_state) {
  41      parent::options_form($form, $form_state);
  42      foreach ($this->formats as $key => $item) {
  43        $options[$key] = implode('/', $item);
  44      }
  45  
  46      $form['type'] = array(
  47        '#type' => 'select',
  48        '#title' => t('Output format'),
  49        '#options' => $options,
  50        '#default_value' => $this->options['type'],
  51      );
  52      $form['not'] = array(
  53        '#type' => 'checkbox',
  54        '#title' => t('Reverse'),
  55        '#description' => t('If checked, true will be displayed as false.'),
  56        '#default_value' => $this->options['not'],
  57      );
  58    }
  59  
  60    function render($values) {
  61      $value = $values->{$this->field_alias};
  62      if (!empty($this->options['not'])) {
  63        $value = !$value;
  64      }
  65  
  66      if (isset($this->formats[$this->options['type']])) {
  67        return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
  68      }
  69      else {
  70        return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
  71      }
  72    }
  73  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7