Can I somehow use operator on views argument?
Basically, I need to filter nodes with the date created older than the currently loaded node and I want to use views argument for that... If I specify just node's date created, it uses the exact match which is not what I want. Is there any other way of doing that?
Any advice is very welcome. Thank you!

Comments

kjv1611’s picture

I JUST, literally Just read about this one. Take a look here:
http://drupal.org/project/context

A video presentation about it here:
http://mustardseedmedia.com/podcast/episode38

See if you can get what you want there. It may be possible within Views, but it just sounded like something that might be in the Context module as well, and it sounds like a very powerful tool to me.

alexbond’s picture

Checked that video and the module... Doesn't help.
I'd really appreciate if anybosy can give any hint on where to dig in order to find a solution for that. Maybe I should create a custom filter instead?

Please help!!!

kjv1611’s picture

You said:

it uses the exact match which is not what I want. Is there any other way of doing that?

You did notice that you can set the granularity in date filters, right? When I set date filters, I can tell it to look "by the minute" which is the default on up to by day or maybe further out. If you change it from "by minute" to "by day" or maybe "by hour" would be good enough, perhaps that's all you need?

alexbond’s picture

I need a filtering logic related to date of the currently loaded node (which I can do only with argument) and not the specific date or current date (what filters do).
The only way I can do it, s to override the module logic and add a functionality myself.

kjv1611’s picture

I would have expected something like this to already be out there. Well, if it isn't, this'd be a good time to recommend it, I'm sure! Perhaps you can make a product recommendation/request on the Views module issues list. And if you know how to code, perhaps you could help some in making that happen...

RonanLK’s picture

You can use date intervals as arguments with a format like 2010-08-21--2010-08-28.
if necessary, you can set a reasonnably far away max date within the set range.

technikh’s picture

Thank you. This worked in my case.
I used php date functions to get current date.

$currentDate = date("Y-m-d");

//Add one year to current date
$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 year");
$endDate = date("Y-m-d",  $dateOneYearAdded);
return $currentDate. "--".$endDate;

Cheers,
TechNikh

In 30 seconds set up Automated Visual testing of your website. Zero coding. https://drupal.org/project/drulenium
Ever dreamed of styling your view, We have a solution for you. https://drupal.org/project/views_stylizer

herd45’s picture

charlie-s’s picture

This is very easy to include in a custom handler. Basically, views_handler_argument_numeric (and other argument handlers) do not provide any options for how the $operator should act outside of = or !=. Here is an example which allows you to filter against a node's published date by >=, <=, or = the argument provided.

example/example.module:

<?php
/**
 * Implements hook_views_api().
 */
function example_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'example') . '/views',
  );
}
?>

example/views/example.views.inc:

<?php
/**
 * Implements hook_views_data().
 */
function example_views_data() {
  $data['node']['created']['argument']['handler'] = 'example_handler_argument_numeric';
  return $data;
}

/**
 * Implements hook_views_handlers().
 */
function example_views_handlers() {
  $handlers = array(
    'info' => array(
      'path' => drupal_get_path('module', 'example') . '/views',
    ),
    'handlers' => array(
      'example_handler_argument_numeric' => array(
        'parent' => 'views_handler_argument',
      ),
    ),
  );
  return $handlers;
}
?>

Finally, in example/views/example_handler_argument_numeric.inc:

<?php
/**
 * This is really an override of views_handler_argument_numeric.inc which provides for >= and <=
 * filtering, which Views does not seem to provide for arguments.
 */

class example_handler_argument_numeric extends views_handler_argument {
  function option_definition() {
    $options = parent::option_definition();

    $options['break_phrase'] = array('default' => FALSE);
    $options['not'] = array('default' => FALSE);

    return $options;
  }

  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    
    $form['filter_type'] = array(
      '#type' => 'select',
      '#title' => t('Filter Type'),
      '#description' => t('Select how content should be filtered against this argument.'),
      '#options' => array(
        'equal' => '=',
        'gt_or_equal' => '>=',
        'lt_or_equal' => '<=',
      ),
      '#default_value' => !empty($this->options['filter_type']),
    );
  }

  function title() {
    if (!$this->argument) {
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
    }

    if (!empty($this->options['break_phrase'])) {
      views_break_phrase($this->argument, $this);
    }
    else {
      $this->value = array($this->argument);
      $this->operator = 'or';
    }

    if (empty($this->value)) {
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
    }

    if ($this->value === array(-1)) {
      return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
    }

    return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
  }

  /**
   * Override for specific title lookups.
   */
  function title_query() {
    return $this->value;
  }

  function query() {
    $this->ensure_my_table();

    if (!empty($this->options['break_phrase'])) {
      views_break_phrase($this->argument, $this);
    }
    else {
      $this->value = array($this->argument);
    }

    $null_check = empty($this->options['not']) ? '' : " OR $this->table_alias.$this->real_field IS NULL";

    if (count($this->value) > 1) {
      $operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
      $placeholders = implode(', ', array_fill(0, sizeof($this->value), '%d'));
      $this->query->add_where(0, "$this->table_alias.$this->real_field $operator ($placeholders) $null_check", $this->value);
    }
    else {
      switch ($this->options['filter_type']) {
        case 'equal':
        default:
          $operator = '=';
          break;
        case 'gt_or_equal':
          $operator = '>=';
          break;
        case 'lt_or_equal':
          $operator = '<=';
          break;
      }
      
      $this->query->add_where(0, "$this->table_alias.$this->real_field $operator %d $null_check", $this->argument);
    }
  }
}
?>