Friday, 19 February 2016

Drupal 7 Views - Views Filter Field Default value 'all' change.

The change is to the field handlers .

To do this we can extend the field handler class for this module which we have in /sites/all/modules/contrib/views_filterfield/handlers/ - which itself extends the ‘views_handler_field'

You’ll need to put this new class in a custom module of your own . We’ve got all ours in Custom module called reports and the folders are arranged like this.

Reports /
admin_views_defualt /
Controller /
Plugin /

At this point it you haven’t worked with classes before I’d need to go into more depth into namespacing and organisation but don’t want to get too sidetracked. So you’ll have to find this out yourself it you’re not sure.

In the class extension of ‘views_filterfield’ make the change to render the default dates correctly as they’ve been rendered into a sub-array.

We do a check to see if our filter is indeed empty. And we then go and get the value we want to fill it with and put that in the output. Unfortunately it’s not quite that straight forward as we also need to create a load of create_function calls to fill the array where needed.

Here’s the code


 * @file

 * Contains Drupal\report\Views\Filter\DateFilterFieldFilter.


 * The only purpose for extending these filters is to handle dates correctly;

 * by default they don't work because they've been rendered into a sub-array.


namespace Drupal\report\Views\Filter;

use views_filterfield;

class DateFilterFieldFilter extends views_filterfield {

  function render($values) {

    $output = ['all'];

    $filter = $this->get_filter($this->options['views_filterfield_field']);


    if (!empty($this->view->exposed_input[$filter['expose']['identifier']]['value'])) {

      if (is_array($this->view->exposed_input[$filter['expose']['identifier']]['value'])) {

        $output = $this->view->exposed_input[$filter['expose']['identifier']]['value'];


      else {

        // Turn a single value into an array, so the transform array_walk

        // function works.

        $output = [$this->view->exposed_input[$filter['expose']['identifier']]['value']];




    // Lots of create_function() calls next, as array_walk does not generally

    // play nice with internal functions, as it passes too many args. These

    // will simply throw a warning and not work.

    // @see

    // Transform spaces to dashes.

    if (!empty($options['views_filterfield_transform_dash'])) {

      array_walk($output, create_function('&$val', '$val = strtr($val, array(" " => "-"));'));


    // Transform case as needed by walking the array of values.

    switch ($this->options['views_filterfield_case']) {

      case 1: // Lower case.

        array_walk($output, create_function('&$val', '$val = drupal_strtolower($val);'));


      case 2: // Title case.

        array_walk($output, create_function('&$val', '$val = ucwords($val);'));


      case 3: // Upper case.

        array_walk($output, create_function('&$val', '$val = drupal_strtoupper($val);'));


      case 4: // Sentence case.

        array_walk($output, create_function('&$val', '$val = drupal_ucfirst($val);'));



    // Turn the transformed array values into a delimited string.

    $separator = $this->options['views_filterfield_separator'];

    $output = implode($separator, $output);

    // Do some basic sanity checking. We don't want crazy values, do we?

    $checked = filter_xss($output);

    return $checked;



No comments: