You are here

class views_ifempty_handler_field in Views If Empty 6

Same name and namespace in other branches
  1. 7 includes/views/handlers/views_ifempty_handler_field.inc \views_ifempty_handler_field

@file Views field handler to output an alterate field when a field is empty.

Hierarchy

Expanded class hierarchy of views_ifempty_handler_field

1 string reference to 'views_ifempty_handler_field'
views_ifempty_views_data in ./views_ifempty.views.inc
Implements hook_views_data().

File

handlers/views_ifempty_handler_field.inc, line 8
Views field handler to output an alterate field when a field is empty.

View source
class views_ifempty_handler_field extends views_handler_field {

  /**
   * Set default values for form elements.
   */
  function option_definition() {
    $options = parent::option_definition();

    // Never display the label of this field.
    $options['label']['default'] = NULL;
    $options['emptyfield'] = array(
      'default' => '',
    );
    $options['outputfield'] = array(
      'default' => '',
    );
    return $options;
  }

  /**
   * Add our form elements.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['relationship']['#access'] = FALSE;

    // Scan all the fields and add them as options for our field selectors.
    $fields = array(
      0 => '- ' . t('no field selected') . ' -',
    );
    foreach ($this->view->display_handler
      ->get_handlers('field') as $field => $handler) {

      // We only use fields up to (not including) this one.
      if ($field == $this->options['id']) {
        break;
      }
      $fields[$field] = $handler
        ->ui_name();
    }
    $form['emptyfield'] = array(
      '#type' => 'select',
      '#title' => t('If this field is empty'),
      '#description' => t('Check this field to see if is empty. This field will be output normally if not empty.'),
      '#options' => $fields,
      '#default_value' => $this->options['emptyfield'],
    );
    $form['outputfield'] = array(
      '#type' => 'select',
      '#title' => t('Then output this field'),
      '#description' => t('Only output this field when the other field is empty. This field will be hidden if the other field is not empty.'),
      '#options' => $fields,
      '#default_value' => $this->options['outputfield'],
    );
  }

  /**
   * Validate the options form.
   */
  function options_validate($form, &$form_state) {
    $values =& $form_state['values']['options'];
    if (empty($values['emptyfield']) || empty($values['outputfield'])) {
      if (empty($values['emptyfield'])) {
        form_set_error('emptyfield', t('Empty field must be specified.'));
      }
      if (empty($values['outputfield'])) {
        form_set_error('outputfield', t('Output field must be specified.'));
      }
    }
    elseif ($values['emptyfield'] == $values['outputfield']) {
      form_set_error('outputfield', t('The output field must be different from the empty field.'));
    }
  }

  /**
   * Add some summary text to the UI that explains what this field will do.
   */
  function admin_summary() {
    if (!empty($this->options['emptyfield']) && !empty($this->options['outputfield'])) {
      return t('If %emptyfield is empty, output %outputfield', array(
        '%emptyfield' => $this->options['emptyfield'],
        '%outputfield' => $this->options['outputfield'],
      ));
    }
    else {
      return t('Invalid field selection');
    }
  }

  /**
   * Remove advanced rendering options from form.
   */
  function allow_advanced_render() {
    return FALSE;
  }

  /**
   * Do not involve the query at all for this field.
   */
  function query() {
    $this->field_alias = 'views_ifempty_' . $this->position;
  }

  /**
   * Render the output.
   */
  function render($values) {
    $emptyfield = $this->options['emptyfield'];
    $outputfield = $this->options['outputfield'];
    if (!empty($emptyfield) && !empty($outputfield) && $emptyfield != $outputfield) {
      $fields = $this->view->display_handler
        ->get_handlers('field');
      if (isset($fields[$emptyfield]) && isset($fields[$outputfield])) {
        if (empty($fields[$emptyfield]->last_render)) {
          $this->last_render = $fields[$outputfield]->last_render;
        }
        else {
          $this->last_render = $fields[$emptyfield]->last_render;
        }
      }
    }
    return $this->last_render;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_ifempty_handler_field::admin_summary function Add some summary text to the UI that explains what this field will do.
views_ifempty_handler_field::allow_advanced_render function Remove advanced rendering options from form.
views_ifempty_handler_field::options_form function Add our form elements.
views_ifempty_handler_field::options_validate function Validate the options form.
views_ifempty_handler_field::option_definition function Set default values for form elements.
views_ifempty_handler_field::query function Do not involve the query at all for this field.
views_ifempty_handler_field::render function Render the output.