You are here

views_xml_backend_handler_field.inc in Views XML Backend 6

Same filename and directory in other branches
  1. 7 handlers/views_xml_backend_handler_field.inc

Base field handler for views_xml_backend.

File

handlers/views_xml_backend_handler_field.inc
View source
<?php

/**
 * @file
 * Base field handler for views_xml_backend.
 */
class views_xml_backend_handler_field extends views_handler_field {
  function render($values) {
    $values = $values->{$this->field_alias};
    if ($this->options['multiple']) {
      foreach ($values as $i => $value) {
        $values[$i] = $this
          ->render_field($value);
      }
      if ($this->options['list_type'] == 'other') {
        return implode(check_plain($this->options['custom_separator']), $values);
      }
      if ($this->options['list_type'] == 'br') {
        return implode('<br />', $values);
      }
      return theme('item_list', $values, NULL, $this->options['list_type']);
    }
    return $this
      ->render_field($values);
  }
  function render_field($value) {
    return check_plain(decode_entities($value));
  }
  function option_definition() {
    $options = parent::option_definition();
    $options['xpath_selector'] = array(
      'default' => '',
    );
    $options['multiple'] = array(
      'default' => FALSE,
    );
    $options['list_type'] = array(
      'default' => 'ul',
    );
    $options['custom_separator'] = array(
      'default' => ', ',
    );
    return $options;
  }
  function options_form(&$form, &$form_state) {
    $form['xml'] = array(
      '#type' => 'fieldset',
      '#title' => t('XPath settings'),
    );
    $form['xml']['xpath_selector'] = array(
      '#title' => t('XPath selector'),
      '#description' => t('The xpath selector'),
      '#type' => 'textfield',
      '#default_value' => $this->options['xpath_selector'],
      '#required' => TRUE,
      '#parents' => array(
        'options',
        'xpath_selector',
      ),
    );
    $form['xml']['multiple'] = array(
      '#title' => t('Allow multiple'),
      '#description' => t('Treat this field as multi-valued.'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['multiple'],
      '#parents' => array(
        'options',
        'multiple',
      ),
    );
    $form['xml']['list_type'] = array(
      '#title' => t('List type'),
      '#description' => t('The type of list.'),
      '#type' => 'radios',
      '#default_value' => $this->options['list_type'],
      '#options' => array(
        'ul' => t('Unordered list'),
        'ol' => t('Ordered list'),
        'br' => check_plain('<br>'),
        'other' => t('Custom separator'),
      ),
      '#process' => array(
        'expand_radios',
        'views_process_dependency',
      ),
      '#dependency' => array(
        'edit-options-multiple' => array(
          TRUE,
        ),
      ),
      '#prefix' => '<div id="edit-options-list-type-wrapper">',
      '#id' => 'edit-options-list-type',
      '#suffix' => '</div>',
      '#parents' => array(
        'options',
        'list_type',
      ),
    );
    $form['xml']['custom_separator'] = array(
      '#type' => 'textfield',
      '#title' => t('Separator'),
      '#default_value' => $this->options['custom_separator'],
      '#process' => array(
        'views_process_dependency',
      ),
      '#dependency' => array(
        'radio:options[list_type]' => array(
          'other',
        ),
      ),
      '#parents' => array(
        'options',
        'custom_separator',
      ),
    );
    parent::options_form($form, $form_state);
    $form['ui_name']['#weight'] = -100;
  }

  /**
   * Called to add the field to a query.
   */
  function query() {

    // Add the field.
    $this->table_alias = 'xml';
    $this->field_alias = $this->query
      ->add_field($this->table_alias, $this->options['xpath_selector'], '', $this->options);
  }
  function ui_name($short = FALSE) {
    if (!empty($this->options['ui_name'])) {
      $title = check_plain($this->options['ui_name']);
      return $title;
    }
    $title = $this->definition['title'];
    if ($short && isset($this->definition['title short'])) {
      $title = $this->definition['title short'];
    }
    return t('!xpath: !title', array(
      '!xpath' => $this->options['xpath_selector'],
      '!title' => $title,
    ));
  }

  /**
   * Called to determine what to tell the clicksorter.
   */
  function click_sort($order) {
    if (isset($this->field_alias)) {
      $this->query
        ->add_orderby(NULL, $this->field_alias, $order);
    }
  }

}

Classes

Namesort descending Description
views_xml_backend_handler_field @file Base field handler for views_xml_backend.