Skip to content
ArgumentPluginBase.php 37.3 KiB
Newer Older
Earl Miles's avatar
Earl Miles committed
<?php

/**
 * @file
 * Definition of Drupal\views\Plugin\views\argument\ArgumentPluginBase.
namespace Drupal\views\Plugin\views\argument;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
Earl Miles's avatar
Earl Miles committed
/**
 * @defgroup views_argument_handlers Views argument handlers
 * Handlers to tell Views how to contextually filter queries.
 * @{
 */

/**
 * Base class for arguments.
 *
 * The basic argument works for very simple arguments such as nid and uid
 *
 * Definition terms for this handler:
 * - name field: The field to use for the name to use in the summary, which is
 *               the displayed output. For example, for the node: nid argument,
 *               the argument itself is the nid, but node.title is displayed.
 * - name table: The table to use for the name, should it not be in the same
 *               table as the argument.
 * - empty field name: For arguments that can have no value, such as taxonomy
 *                     which can have "no term", this is the string which
 *                     will be displayed for this lack of value. Be sure to use
 *                     t().
 * - validate type: A little used string to allow an argument to restrict
 *                  which validator is available to just one. Use the
 *                  validator ID. This probably should not be used at all,
 *                  and may disappear or change.
 * - numeric: If set to TRUE this field is numeric and will use %d instead of
 *            %s in queries.
 *
 * @ingroup views_argument_handlers
 */
abstract class ArgumentPluginBase extends HandlerBase {
Earl Miles's avatar
Earl Miles committed
  var $validator = NULL;
  var $argument = NULL;
  var $value = NULL;

  /**
   * The table to use for the name, should it not be in the same table as the argument.
   * @var string
   */
  var $name_table;

  /**
   * The field to use for the name to use in the summary, which is
   * the displayed output. For example, for the node: nid argument,
   * the argument itself is the nid, but node.title is displayed.
   * @var string
   */
  var $name_field;

  /**
   * Overrides Drupal\views\Plugin\views\HandlerBase:init().
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
Earl Miles's avatar
Earl Miles committed

    if (!empty($this->definition['name field'])) {
      $this->name_field = $this->definition['name field'];
    }
    if (!empty($this->definition['name table'])) {
      $this->name_table = $this->definition['name table'];
    }
  }

  /**
   * Give an argument the opportunity to modify the breadcrumb, if it wants.
   * This only gets called on displays where a breadcrumb is actually used.
   *
   * The breadcrumb will be in the form of an array, with the keys being
   * the path and the value being the already sanitized title of the path.
   */
  public function setBreadcrumb(&$breadcrumb) { }
Earl Miles's avatar
Earl Miles committed

  /**
   * Determine if the argument can generate a breadcrumb
   *
   * @return TRUE/FALSE
   */
  public function usesBreadcrumb() {
    $info = $this->defaultActions($this->options['default_action']);
Earl Miles's avatar
Earl Miles committed
    return !empty($info['breadcrumb']);
  }

  public function isException($arg = NULL) {
Earl Miles's avatar
Earl Miles committed
    if (!isset($arg)) {
      $arg = isset($this->argument) ? $this->argument : NULL;
    }
    return !empty($this->options['exception']['value']) && $this->options['exception']['value'] === $arg;
  }

Earl Miles's avatar
Earl Miles committed
    // If title overriding is off for the exception, return the normal title.
    if (empty($this->options['exception']['title_enable'])) {
Earl Miles's avatar
Earl Miles committed
    }
    return $this->options['exception']['title'];
  }

  /**
   * Determine if the argument needs a style plugin.
   *
   * @return TRUE/FALSE
   */
  public function needsStylePlugin() {
    $info = $this->defaultActions($this->options['default_action']);
    $validate_info = $this->defaultActions($this->options['validate']['fail']);
Earl Miles's avatar
Earl Miles committed
    return !empty($info['style plugin']) || !empty($validate_info['style plugin']);
  }

  protected function defineOptions() {
    $options = parent::defineOptions();
Earl Miles's avatar
Earl Miles committed

    $options['default_action'] = array('default' => 'ignore');
    $options['exception'] = array(
      'contains' => array(
        'value' => array('default' => 'all'),
        'title_enable' => array('default' => FALSE, 'bool' => TRUE),
        'title' => array('default' => 'All', 'translatable' => TRUE),
Earl Miles's avatar
Earl Miles committed
      ),
    );
    $options['title_enable'] = array('default' => FALSE, 'bool' => TRUE);
    $options['title'] = array('default' => '', 'translatable' => TRUE);
    $options['breadcrumb_enable'] = array('default' => FALSE, 'bool' => TRUE);
    $options['breadcrumb'] = array('default' => '', 'translatable' => TRUE);
    $options['default_argument_type'] = array('default' => 'fixed');
    $options['default_argument_options'] = array('default' => array());
Earl Miles's avatar
Earl Miles committed
    $options['default_argument_skip_url'] = array('default' => FALSE, 'bool' => TRUE);
    $options['summary_options'] = array('default' => array());
Earl Miles's avatar
Earl Miles committed
    $options['summary'] = array(
      'contains' => array(
        'sort_order' => array('default' => 'asc'),
        'number_of_records' => array('default' => 0),
        'format' => array('default' => 'default_summary'),
Earl Miles's avatar
Earl Miles committed
      ),
    );
    $options['specify_validation'] = array('default' => FALSE, 'bool' => TRUE);
    $options['validate'] = array(
      'contains' => array(
Earl Miles's avatar
Earl Miles committed
        'fail' => array('default' => 'not found'),
      ),
    );
    $options['validate_options'] = array('default' => array());
Earl Miles's avatar
Earl Miles committed

    return $options;
  }

  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
    $argument_text = $this->view->display_handler->getArgumentText();
Earl Miles's avatar
Earl Miles committed

    $form['#pre_render'][] = 'views_ui_pre_render_move_argument_options';

    $form['description'] = array(
      '#markup' => $argument_text['description'],
      '#theme_wrappers' => array('container'),
      '#attributes' => array('class' => array('description')),
    );

    $form['no_argument'] = array(
Earl Miles's avatar
Earl Miles committed
      '#title' => $argument_text['filter value not present'],
    );
    // Everything in the details is floated, so the last element needs to
Earl Miles's avatar
Earl Miles committed
    // clear those floats.
    $form['no_argument']['clearfix'] = array(
      '#weight' => 1000,
      '#markup' => '<div class="clearfix"></div>',
    );
    $form['default_action'] = array(
      '#type' => 'radios',
      '#process' => array(array($this, 'processContainerRadios')),
Earl Miles's avatar
Earl Miles committed
      '#default_value' => $this->options['default_action'],
      '#fieldset' => 'no_argument',
    );

    $form['exception'] = array(
Earl Miles's avatar
Earl Miles committed
      '#title' => t('Exceptions'),
Earl Miles's avatar
Earl Miles committed
      '#fieldset' => 'no_argument',
    );
    $form['exception']['value'] = array(
      '#type' => 'textfield',
      '#title' => t('Exception value'),
      '#size' => 20,
      '#default_value' => $this->options['exception']['value'],
      '#description' => t('If this value is received, the filter will be ignored; i.e, "all values"'),
    );
    $form['exception']['title_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Override title'),
      '#default_value' => $this->options['exception']['title_enable'],
    );
    $form['exception']['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Override title'),
      '#title_display' => 'invisible',
      '#size' => 20,
      '#default_value' => $this->options['exception']['title'],
      '#description' => t('Override the view and other argument titles. Use "%1" for the first argument, "%2" for the second, etc.'),
      '#states' => array(
        'visible' => array(
          ':input[name="options[exception][title_enable]"]' => array('checked' => TRUE),
        ),
Earl Miles's avatar
Earl Miles committed
      ),
    );

    $options = array();
    $defaults = $this->defaultActions();
Earl Miles's avatar
Earl Miles committed
    $validate_options = array();
    foreach ($defaults as $id => $info) {
      $options[$id] = $info['title'];
      if (empty($info['default only'])) {
        $validate_options[$id] = $info['title'];
      }
      if (!empty($info['form method'])) {
        $this->{$info['form method']}($form, $form_state);
      }
    }
    $form['default_action']['#options'] = $options;

    $form['argument_present'] = array(
Earl Miles's avatar
Earl Miles committed
      '#title' => $argument_text['filter value present'],
    );
    $form['title_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Override title'),
      '#default_value' => $this->options['title_enable'],
      '#fieldset' => 'argument_present',
    );
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Provide title'),
      '#title_display' => 'invisible',
      '#default_value' => $this->options['title'],
      '#description' => t('Override the view and other argument titles. Use "%1" for the first argument, "%2" for the second, etc.'),
      '#states' => array(
        'visible' => array(
          ':input[name="options[title_enable]"]' => array('checked' => TRUE),
        ),
Earl Miles's avatar
Earl Miles committed
      ),
      '#fieldset' => 'argument_present',
    );

    $form['breadcrumb_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Override breadcrumb'),
      '#default_value' => $this->options['breadcrumb_enable'],
      '#fieldset' => 'argument_present',
    );
    $form['breadcrumb'] = array(
      '#type' => 'textfield',
      '#title' => t('Provide breadcrumb'),
      '#title_display' => 'invisible',
      '#default_value' => $this->options['breadcrumb'],
      '#description' => t('Enter a breadcrumb name you would like to use. See "Title" for percent substitutions.'),
      '#states' => array(
        'visible' => array(
          ':input[name="options[breadcrumb_enable]"]' => array('checked' => TRUE),
        ),
Earl Miles's avatar
Earl Miles committed
      ),
      '#fieldset' => 'argument_present',
    );

    $form['specify_validation'] = array(
      '#type' => 'checkbox',
      '#title' => t('Specify validation criteria'),
      '#default_value' => $this->options['specify_validation'],
      '#fieldset' => 'argument_present',
    );

    $form['validate'] = array(
      '#type' => 'container',
      '#fieldset' => 'argument_present',
    );
    $form['validate']['type'] = array(
      '#type' => 'select',
      '#title' => t('Validator'),
      '#default_value' => $this->options['validate']['type'],
      '#states' => array(
        'visible' => array(
          ':input[name="options[specify_validation]"]' => array('checked' => TRUE),
        ),
    $plugins = Views::pluginManager('argument_validator')->getDefinitions();
Earl Miles's avatar
Earl Miles committed
    foreach ($plugins as $id => $info) {
      if (!empty($info['no_ui'])) {
Earl Miles's avatar
Earl Miles committed
        continue;
      }

      $valid = TRUE;
      if (!empty($info['type'])) {
        $valid = FALSE;
        if (empty($this->definition['validate type'])) {
          continue;
        }
        foreach ((array) $info['type'] as $type) {
          if ($type == $this->definition['validate type']) {
            $valid = TRUE;
            break;
          }
        }
      }

      // If we decide this validator is ok, add it to the list.
      if ($valid) {
        $plugin = $this->getPlugin('argument_validator', $id);
Earl Miles's avatar
Earl Miles committed
        if ($plugin) {
          if ($plugin->access() || $this->options['validate']['type'] == $id) {
            $form['validate']['options'][$id] = array(
              '#prefix' => '<div id="edit-options-validate-options-' . $id . '-wrapper">',
              '#suffix' => '</div>',
              '#type' => 'item',
              // Even if the plugin has no options add the key to the form_state.
              '#input' => TRUE, // trick it into checking input to make #process run
              '#states' => array(
                'visible' => array(
                  ':input[name="options[specify_validation]"]' => array('checked' => TRUE),
                  ':input[name="options[validate][type]"]' => array('value' => $id),
                ),
Earl Miles's avatar
Earl Miles committed
              ),
              '#id' => 'edit-options-validate-options-' . $id,
            $plugin->buildOptionsForm($form['validate']['options'][$id], $form_state);
Earl Miles's avatar
Earl Miles committed
            $validate_types[$id] = $info['title'];
          }
        }
      }
    }

    asort($validate_types);
    $form['validate']['type']['#options'] = $validate_types;

    $form['validate']['fail'] = array(
      '#type' => 'select',
      '#title' => t('Action to take if filter value does not validate'),
      '#default_value' => $this->options['validate']['fail'],
      '#options' => $validate_options,
      '#states' => array(
        'visible' => array(
          ':input[name="options[specify_validation]"]' => array('checked' => TRUE),
        ),
Earl Miles's avatar
Earl Miles committed
      ),
      '#fieldset' => 'argument_present',
    );
  }

  public function validateOptionsForm(&$form, &$form_state) {
Earl Miles's avatar
Earl Miles committed
    if (empty($form_state['values']['options'])) {
      return;
    }

    // Let the plugins do validation.
    $default_id = $form_state['values']['options']['default_argument_type'];
    $plugin = $this->getPlugin('argument_default', $default_id);
Earl Miles's avatar
Earl Miles committed
    if ($plugin) {
      $plugin->validateOptionsForm($form['argument_default'][$default_id], $form_state, $form_state['values']['options']['argument_default'][$default_id]);
Earl Miles's avatar
Earl Miles committed
    }

    // summary plugin
    $summary_id = $form_state['values']['options']['summary']['format'];
    $plugin = $this->getPlugin('style', $summary_id);
Earl Miles's avatar
Earl Miles committed
    if ($plugin) {
      $plugin->validateOptionsForm($form['summary']['options'][$summary_id], $form_state, $form_state['values']['options']['summary']['options'][$summary_id]);
Earl Miles's avatar
Earl Miles committed
    }

    $validate_id = $form_state['values']['options']['validate']['type'];
    $plugin = $this->getPlugin('argument_validator', $validate_id);
Earl Miles's avatar
Earl Miles committed
    if ($plugin) {
      $plugin->validateOptionsForm($form['validate']['options'][$default_id], $form_state, $form_state['values']['options']['validate']['options'][$validate_id]);
  public function submitOptionsForm(&$form, &$form_state) {
Earl Miles's avatar
Earl Miles committed
    if (empty($form_state['values']['options'])) {
      return;
    }

    // Let the plugins make submit modifications if necessary.
    $default_id = $form_state['values']['options']['default_argument_type'];
    $plugin = $this->getPlugin('argument_default', $default_id);
Earl Miles's avatar
Earl Miles committed
    if ($plugin) {
      $options = &$form_state['values']['options']['argument_default'][$default_id];
      $plugin->submitOptionsForm($form['argument_default'][$default_id], $form_state, $options);
Earl Miles's avatar
Earl Miles committed
      // Copy the now submitted options to their final resting place so they get saved.
      $form_state['values']['options']['default_argument_options'] = $options;
    }

    // summary plugin
    $summary_id = $form_state['values']['options']['summary']['format'];
    $plugin = $this->getPlugin('style', $summary_id);
Earl Miles's avatar
Earl Miles committed
    if ($plugin) {
      $options = &$form_state['values']['options']['summary']['options'][$summary_id];
      $plugin->submitOptionsForm($form['summary']['options'][$summary_id], $form_state, $options);
Earl Miles's avatar
Earl Miles committed
      // Copy the now submitted options to their final resting place so they get saved.
      $form_state['values']['options']['summary_options'] = $options;
    }

    $validate_id = $form_state['values']['options']['validate']['type'];
    $plugin = $this->getPlugin('argument_validator', $validate_id);
Earl Miles's avatar
Earl Miles committed
    if ($plugin) {
      $options = &$form_state['values']['options']['validate']['options'][$validate_id];
      $plugin->submitOptionsForm($form['validate']['options'][$validate_id], $form_state, $options);
Earl Miles's avatar
Earl Miles committed
      // Copy the now submitted options to their final resting place so they get saved.
      $form_state['values']['options']['validate_options'] = $options;
    }

    // Clear out the content of title if it's not enabled.
    $options =& $form_state['values']['options'];
    if (empty($options['title_enable'])) {
      $options['title'] = '';
    }
  }

  /**
   * Provide a list of default behaviors for this argument if the argument
   * is not present.
   *
   * Override this method to provide additional (or fewer) default behaviors.
   */
  protected function defaultActions($which = NULL) {
Earl Miles's avatar
Earl Miles committed
    $defaults = array(
      'ignore' => array(
        'title' => t('Display all results for the specified field'),
Earl Miles's avatar
Earl Miles committed
        'breadcrumb' => TRUE, // generate a breadcrumb to here
      ),
      'default' => array(
        'title' => t('Provide default value'),
Earl Miles's avatar
Earl Miles committed
        'has default argument' => TRUE,
        'default only' => TRUE, // this can only be used for missing argument, not validation failure
        'breadcrumb' => TRUE, // generate a breadcrumb to here
      ),
      'not found' => array(
        'title' => t('Hide view'),
Earl Miles's avatar
Earl Miles committed
        'hard fail' => TRUE, // This is a hard fail condition
      ),
      'summary' => array(
        'title' => t('Display a summary'),
Earl Miles's avatar
Earl Miles committed
        'style plugin' => TRUE,
        'breadcrumb' => TRUE, // generate a breadcrumb to here
      ),
      'empty' => array(
        'title' => t('Display contents of "No results found"'),
Earl Miles's avatar
Earl Miles committed
        'breadcrumb' => TRUE, // generate a breadcrumb to here
      ),
      'access denied' => array(
        'title' => t('Display "Access Denied"'),
Earl Miles's avatar
Earl Miles committed
        'breadcrumb' => FALSE, // generate a breadcrumb to here
      ),
    );

    if ($this->view->display_handler->hasPath()) {
Earl Miles's avatar
Earl Miles committed
      $defaults['not found']['title'] = t('Show "Page not found"');
    }

    if ($which) {
      if (!empty($defaults[$which])) {
        return $defaults[$which];
      }
    }
    else {
      return $defaults;
    }
  }

  /**
   * Provide a form for selecting the default argument when the
   * default action is set to provide default argument.
   */
  public function defaultArgumentForm(&$form, &$form_state) {
    $plugins = Views::pluginManager('argument_default')->getDefinitions();
Earl Miles's avatar
Earl Miles committed
    $options = array();

    $form['default_argument_skip_url'] = array(
      '#type' => 'checkbox',
      '#title' => t('Skip default argument for view URL'),
      '#default_value' => $this->options['default_argument_skip_url'],
      '#description' => t('Select whether to include this default argument when constructing the URL for this view. Skipping default arguments is useful e.g. in the case of feeds.')
    );

    $form['default_argument_type'] = array(
      '#prefix' => '<div id="edit-options-default-argument-type-wrapper">',
      '#suffix' => '</div>',
      '#type' => 'select',
      '#id' => 'edit-options-default-argument-type',
      '#title' => t('Type'),
      '#default_value' => $this->options['default_argument_type'],
      '#states' => array(
        'visible' => array(
          ':input[name="options[default_action]"]' => array('value' => 'default'),
        ),
      ),
Earl Miles's avatar
Earl Miles committed
      // Views custom key, moves this element to the appropriate container
      // under the radio button.
      '#argument_option' => 'default',
    );

    foreach ($plugins as $id => $info) {
      if (!empty($info['no_ui'])) {
Earl Miles's avatar
Earl Miles committed
        continue;
      }
      $plugin = $this->getPlugin('argument_default', $id);
Earl Miles's avatar
Earl Miles committed
      if ($plugin) {
        if ($plugin->access() || $this->options['default_argument_type'] == $id) {
          $form['argument_default']['#argument_option'] = 'default';
          $form['argument_default'][$id] = array(
            '#prefix' => '<div id="edit-options-argument-default-options-' . $id . '-wrapper">',
            '#suffix' => '</div>',
            '#id' => 'edit-options-argument-default-options-' . $id,
            '#type' => 'item',
            // Even if the plugin has no options add the key to the form_state.
            '#input' => TRUE,
            '#states' => array(
              'visible' => array(
                ':input[name="options[default_action]"]' => array('value' => 'default'),
                ':input[name="options[default_argument_type]"]' => array('value' => $id),
              ),
Earl Miles's avatar
Earl Miles committed
          );
          $options[$id] = $info['title'];
          $plugin->buildOptionsForm($form['argument_default'][$id], $form_state);
Earl Miles's avatar
Earl Miles committed
        }
      }
    }

    asort($options);
    $form['default_argument_type']['#options'] = $options;
  }

  /**
   * Provide a form for selecting further summary options when the
   * default action is set to display one.
   */
  public function defaultSummaryForm(&$form, &$form_state) {
    $style_plugins = Views::pluginManager('style')->getDefinitions();
Earl Miles's avatar
Earl Miles committed
    $summary_plugins = array();
    $format_options = array();
    foreach ($style_plugins as $key => $plugin) {
      if (isset($plugin['display_types']) && in_array('summary', $plugin['display_types'])) {
Earl Miles's avatar
Earl Miles committed
        $summary_plugins[$key] = $plugin;
        $format_options[$key] = $plugin['title'];
      }
    }

    $form['summary'] = array(
      // Views custom key, moves this element to the appropriate container
      // under the radio button.
      '#argument_option' => 'summary',
    );
    $form['summary']['sort_order'] = array(
      '#type' => 'radios',
      '#title' => t('Sort order'),
      '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
      '#default_value' => $this->options['summary']['sort_order'],
      '#states' => array(
        'visible' => array(
          ':input[name="options[default_action]"]' => array('value' => 'summary'),
        ),
      ),
Earl Miles's avatar
Earl Miles committed
    );
    $form['summary']['number_of_records'] = array(
      '#type' => 'radios',
      '#title' => t('Sort by'),
      '#default_value' => $this->options['summary']['number_of_records'],
      '#options' => array(
Earl Miles's avatar
Earl Miles committed
        1 => t('Number of records')
      ),
      '#states' => array(
        'visible' => array(
          ':input[name="options[default_action]"]' => array('value' => 'summary'),
        ),
      ),
Earl Miles's avatar
Earl Miles committed
    );

    $form['summary']['format'] = array(
      '#type' => 'radios',
      '#title' => t('Format'),
      '#options' => $format_options,
      '#default_value' => $this->options['summary']['format'],
      '#states' => array(
        'visible' => array(
          ':input[name="options[default_action]"]' => array('value' => 'summary'),
        ),
      ),
Earl Miles's avatar
Earl Miles committed
    );

    foreach ($summary_plugins as $id => $info) {
      $plugin = $this->getPlugin('style', $id);
Earl Miles's avatar
Earl Miles committed
        continue;
      }
      if ($plugin) {
        $form['summary']['options'][$id] = array(
          '#prefix' => '<div id="edit-options-summary-options-' . $id . '-wrapper">',
          '#suffix' => '</div>',
          '#id' => 'edit-options-summary-options-' . $id,
          '#type' => 'item',
          '#input' => TRUE, // trick it into checking input to make #process run
          '#states' => array(
            'visible' => array(
              ':input[name="options[default_action]"]' => array('value' => 'summary'),
              ':input[name="options[summary][format]"]' => array('value' => $id),
            ),
Earl Miles's avatar
Earl Miles committed
        );
        $options[$id] = $info['title'];
        $plugin->buildOptionsForm($form['summary']['options'][$id], $form_state);
Earl Miles's avatar
Earl Miles committed
      }
    }
  }

  /**
   * Handle the default action, which means our argument wasn't present.
   *
   * Override this method only with extreme care.
   *
   * @return
   *   A boolean value; if TRUE, continue building this view. If FALSE,
   *   building the view will be aborted here.
   */
  function default_action($info = NULL) {
    if (!isset($info)) {
      $info = $this->defaultActions($this->options['default_action']);
Earl Miles's avatar
Earl Miles committed
    }

    if (!$info) {
      return FALSE;
    }

    if (!empty($info['method args'])) {
      return call_user_func_array(array(&$this, $info['method']), $info['method args']);
    }
    else {
      return $this->{$info['method']}();
    }
  }

  /**
   * How to act if validation failes
   */
    $info = $this->defaultActions($this->options['validate']['fail']);
Earl Miles's avatar
Earl Miles committed
    return $this->default_action($info);
  }
  /**
   * Default action: ignore.
   *
   * If an argument was expected and was not given, in this case, simply
   * ignore the argument entirely.
   */
Earl Miles's avatar
Earl Miles committed
    return TRUE;
  }

  /**
   * Default action: not found.
   *
   * If an argument was expected and was not given, in this case, report
   * the view as 'not found' or hide it.
   */
  protected function defaultNotFound() {
Earl Miles's avatar
Earl Miles committed
    // Set a failure condition and let the display manager handle it.
    $this->view->build_info['fail'] = TRUE;
    return FALSE;
  }

  /**
   * Default action: access denied.
   *
   * If an argument was expected and was not given, in this case, report
   * the view as 'access denied'.
   */
  public function defaultAccessDenied() {
Earl Miles's avatar
Earl Miles committed
    $this->view->build_info['denied'] = TRUE;
    return FALSE;
  }

  /**
   * Default action: empty
   *
   * If an argument was expected and was not given, in this case, display
   * the view's empty text
   */
Earl Miles's avatar
Earl Miles committed
    // We return with no query; this will force the empty text.
    $this->view->built = TRUE;
    $this->view->executed = TRUE;
    $this->view->result = array();
    return FALSE;
  }

  /**
   * This just returns true. The view argument builder will know where
   * to find the argument from.
   */
Earl Miles's avatar
Earl Miles committed
    return TRUE;
  }

  /**
   * Determine if the argument is set to provide a default argument.
   */
    $info = $this->defaultActions($this->options['default_action']);
Earl Miles's avatar
Earl Miles committed
    return !empty($info['has default argument']);
  }

  /**
   * Get a default argument, if available.
   */
    $plugin = $this->getPlugin('argument_default');
Earl Miles's avatar
Earl Miles committed
    if ($plugin) {
Earl Miles's avatar
Earl Miles committed
    }
  }

  /**
   * Process the summary arguments for display.
   *
   * For example, the validation plugin may want to alter an argument for use in
   * the URL.
   */
  public function processSummaryArguments(&$args) {
Earl Miles's avatar
Earl Miles committed
    if ($this->options['validate']['type'] != 'none') {
      if (isset($this->validator) || $this->validator = $this->getPlugin('argument_validator')) {
        $this->validator->processSummaryArguments($args);
Earl Miles's avatar
Earl Miles committed
      }
    }
  }

  /**
   * Default action: summary.
   *
   * If an argument was expected and was not given, in this case, display
   * a summary query.
   */
  protected function defaultSummary() {
Earl Miles's avatar
Earl Miles committed
    $this->view->build_info['summary'] = TRUE;
    $this->view->build_info['summary_level'] = $this->options['id'];

    // Change the display style to the summary style for this
    // argument.
    $this->view->style_plugin = Views::pluginManager("style")->createInstance($this->options['summary']['format']);
    $this->view->style_plugin->init($this->view, $this->displayHandler, $this->options['summary_options']);
Earl Miles's avatar
Earl Miles committed

    // Clear out the normal primary field and whatever else may have
    // been added and let the summary do the work.
Earl Miles's avatar
Earl Miles committed

    $by = $this->options['summary']['number_of_records'] ? 'num_records' : NULL;
    $this->summarySort($this->options['summary']['sort_order'], $by);
Earl Miles's avatar
Earl Miles committed

    // Summaries have their own sorting and fields, so tell the View not
    // to build these.
Earl Miles's avatar
Earl Miles committed
    return TRUE;
  }

  /**
   * Build the info for the summary query.
   *
   * This must:
   * - add_groupby: group on this field in order to create summaries.
   * - addField: add a 'num_nodes' field for the count. Usually it will
Earl Miles's avatar
Earl Miles committed
   *   be a count on $view->base_field
   * - set_count_field: Reset the count field so we get the right paging.
   *
   * @return
   *   The alias used to get the number of records (count) for this entry.
   */
Earl Miles's avatar
Earl Miles committed
    // Add the field.
    $this->base_alias = $this->query->addField($this->tableAlias, $this->realField);
Earl Miles's avatar
Earl Miles committed
  }

  /**
   * Add the name field, which is the field displayed in summary queries.
   * This is often used when the argument is numeric.
   */
  protected function summaryNameField() {
Earl Miles's avatar
Earl Miles committed
    // Add the 'name' field. For example, if this is a uid argument, the
    // name field would be 'name' (i.e, the username).

    if (isset($this->name_table)) {
      // if the alias is different then we're probably added, not ensured,
      // so look up the join and add it instead.
      if ($this->tableAlias != $this->name_table) {
        $j = HandlerBase::getTableJoin($this->name_table, $this->table);
Earl Miles's avatar
Earl Miles committed
        if ($j) {
          $join = clone $j;
          $join->leftTable = $this->tableAlias;
          $this->name_table_alias = $this->query->addTable($this->name_table, $this->relationship, $join);
Earl Miles's avatar
Earl Miles committed
        }
      }
      else {
        $this->name_table_alias = $this->query->ensure_table($this->name_table, $this->relationship);
      }
    }
    else {
      $this->name_table_alias = $this->tableAlias;
Earl Miles's avatar
Earl Miles committed
    }

    if (isset($this->name_field)) {
      $this->name_alias = $this->query->addField($this->name_table_alias, $this->name_field);
Earl Miles's avatar
Earl Miles committed
    }
    else {
      $this->name_alias = $this->base_alias;
    }
  }

  /**
   * Some basic summary behavior that doesn't need to be repeated as much as
  public function summaryBasics($count_field = TRUE) {
Earl Miles's avatar
Earl Miles committed
    // Add the number of nodes counter
    $distinct = ($this->view->display_handler->getOption('distinct') && empty($this->query->no_distinct));
    $count_alias = $this->query->addField($this->view->storage->get('base_table'), $this->view->storage->get('base_field'), 'num_records', array('count' => TRUE, 'distinct' => $distinct));
Earl Miles's avatar
Earl Miles committed
    $this->query->add_groupby($this->name_alias);

    if ($count_field) {
      $this->query->set_count_field($this->tableAlias, $this->realField);
Earl Miles's avatar
Earl Miles committed
    }

    $this->count_alias = $count_alias;
  }

  /**
   * Sorts the summary based upon the user's selection. The base variant of
   * this is usually adequte.
   *
   * @param $order
   *   The order selected in the UI.
   */
  public function summarySort($order, $by = NULL) {
Earl Miles's avatar
Earl Miles committed
    $this->query->add_orderby(NULL, NULL, $order, (!empty($by) ? $by : $this->name_alias));
  }

  /**
   * Provide the argument to use to link from the summary to the next level;
   * this will be called once per row of a summary, and used as part of
Earl Miles's avatar
Earl Miles committed
   *
   * @param $data
   *   The query results for the row.
   */
Earl Miles's avatar
Earl Miles committed
    return $data->{$this->base_alias};
  }

  /**
   * Provides the name to use for the summary. By default this is just
   * the name field.
   *
   * @param $data
   *   The query results for the row.
   */
Earl Miles's avatar
Earl Miles committed
    $value = $data->{$this->name_alias};
    if (empty($value) && !empty($this->definition['empty field name'])) {
      $value = $this->definition['empty field name'];
    }
    return check_plain($value);
  }

  /**
   * Set up the query for this argument.
   *
   * The argument sent may be found at $this->argument.
   */
  public function query($group_by = FALSE) {
    $this->query->addWhere(0, "$this->tableAlias.$this->realField", $this->argument);
Earl Miles's avatar
Earl Miles committed
  }

  /**
   * Get the title this argument will assign the view, given the argument.
   *
   * This usually needs to be overridden to provide a proper title.
   */
  function title() {
    return check_plain($this->argument);
  }

  /**
   * Called by the view object to get the title. This may be set by a
   * validator so we don't necessarily call through to title().
   */
Earl Miles's avatar
Earl Miles committed
    if (isset($this->validated_title)) {
      return $this->validated_title;
    }
    else {
      return $this->title();
    }
  }

  /**
   * Validate that this argument works. By default, all arguments are valid.
   */
  public function validateArgument($arg) {
Earl Miles's avatar
Earl Miles committed
    // By using % in URLs, arguments could be validated twice; this eases
    // that pain.
    if (isset($this->argument_validated)) {
      return $this->argument_validated;
    }

Earl Miles's avatar
Earl Miles committed
      return $this->argument_validated = TRUE;
    }

    $plugin = $this->getPlugin('argument_validator');
    return $this->argument_validated = $plugin->validate_argument($arg);
Earl Miles's avatar
Earl Miles committed
  }

  /**
   * Called by the menu system to validate an argument.
   *
   * This checks to see if this is a 'soft fail', which means that if the
   * argument fails to validate, but there is an action to take anyway,
   * then validation cannot actually fail.
   */
  function validate_argument($arg) {
    $validate_info = $this->defaultActions($this->options['validate']['fail']);
Earl Miles's avatar
Earl Miles committed
    if (empty($validate_info['hard fail'])) {
      return TRUE;
    }

    $rc = $this->validateArgument($arg);
Earl Miles's avatar
Earl Miles committed

    // If the validator has changed the validate fail condition to a
    // soft fail, deal with that:
    $validate_info = $this->defaultActions($this->options['validate']['fail']);
Earl Miles's avatar
Earl Miles committed
    if (empty($validate_info['hard fail'])) {
      return TRUE;
    }

    return $rc;
  }

  /**
   * Set the input for this argument
   *
   * @return TRUE if it successfully validates; FALSE if it does not.
   */
Earl Miles's avatar
Earl Miles committed
    $this->argument = $arg;
    return $this->validateArgument($arg);
Earl Miles's avatar
Earl Miles committed
  }

  /**
   * Get the value of this argument.
   */
Earl Miles's avatar
Earl Miles committed
    // If we already processed this argument, we're done.
    if (isset($this->argument)) {
      return $this->argument;
    }

    // Otherwise, we have to pretend to process ourself to find the value.