Date.php 2.71 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\argument\Date.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
namespace Drupal\views\Plugin\views\argument;
9

10
use Drupal\Component\Annotation\Plugin;
11

merlinofchaos's avatar
merlinofchaos committed
12
13
14
15
16
17
18
19
20
/**
 * Abstract argument handler for dates.
 *
 * Adds an option to set a default argument based on the current date.
 *
 * Definitions terms:
 * - many to one: If true, the "many to one" helper will be used.
 * - invalid input: A string to give to the user for obviously invalid input.
 *                  This is deprecated in favor of argument validators.
21
22
 * - arg_format: The format string to use on the current time when dealing with
 *     date values.
merlinofchaos's avatar
merlinofchaos committed
23
 *
dawehner's avatar
dawehner committed
24
 * @see Drupal\views\ManyTonOneHelper
merlinofchaos's avatar
merlinofchaos committed
25
26
 *
 * @ingroup views_argument_handlers
27
 *
28
 * @Plugin(
29
30
 *   id = "date",
 *   arg_format = "Y-m-d"
31
32
 * )
 */
33
class Date extends Formula {
34

merlinofchaos's avatar
merlinofchaos committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  var $option_name = 'default_argument_date';

  /**
   * Add an option to set the default value to the current date.
   */
  function default_argument_form(&$form, &$form_state) {
    parent::default_argument_form($form, $form_state);
    $form['default_argument_type']['#options'] += array('date' => t('Current date'));
    $form['default_argument_type']['#options'] += array('node_created' => t("Current node's creation time"));
    $form['default_argument_type']['#options'] += array('node_changed' => t("Current node's update time"));  }

  /**
   * Set the empty argument value to the current date,
   * formatted appropriately for this argument.
   */
  function get_default_argument($raw = FALSE) {
    if (!$raw && $this->options['default_argument_type'] == 'date') {
52
      return date($this->definition['format'], REQUEST_TIME);
merlinofchaos's avatar
merlinofchaos committed
53
    }
54
    elseif (!$raw && in_array($this->options['default_argument_type'], array('node_created', 'node_changed'))) {
merlinofchaos's avatar
merlinofchaos committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
      foreach (range(1, 3) as $i) {
        $node = menu_get_object('node', $i);
        if (!empty($node)) {
          continue;
        }
      }

      if (arg(0) == 'node' && is_numeric(arg(1))) {
        $node = node_load(arg(1));
      }

      if (empty($node)) {
        return parent::get_default_argument();
      }
      elseif ($this->options['default_argument_type'] == 'node_created') {
70
        return date($this->definition['format'], $node->created);
merlinofchaos's avatar
merlinofchaos committed
71
72
      }
      elseif ($this->options['default_argument_type'] == 'node_changed') {
73
        return date($this->definition['format'], $node->changed);
merlinofchaos's avatar
merlinofchaos committed
74
75
76
77
78
79
80
81
82
      }
    }

    return parent::get_default_argument($raw);
  }

  function get_sort_name() {
    return t('Date', array(), array('context' => 'Sort order'));
  }
83

84
  /**
85
   * Overrides \Drupal\views\Plugin\views\argument\Formula::get_formula().
86
   */
87
88
89
  function get_formula() {
    $this->formula = $this->getDateFormat($this->definition['arg_format']);
    return parent::get_formula();
90
91
  }

merlinofchaos's avatar
merlinofchaos committed
92
}