0

I want to contextually filter my View with a boolean field, for which the "On" value is "New," so using a URL like /view/url/new.

I first tried basic validation, but that would only work if my URL were /view/url/1.

So I wrote my own validation plugin to validate "New" as if it were "1".

<?php

namespace Drupal\my_module_validation\Plugin\views\argument_validator;

use Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase;

/**
 * Validate the New boolean.
 *
 * @ingroup views_argument_validate_plugins
 *
 * @ViewsArgumentValidator(
 *   id = "my_module_validation",
 *   title = @Translation("MYMODULE Validation")
 * )
 */
class MyModuleValidation extends ArgumentValidatorPluginBase {

  public function validateArgument($argument) {
    if($argument == 'new' || $argument == 'New' || $argument == '1') {
      return TRUE;
    }
    return FALSE;
  }
}

So I'm able to apply this fine (and I can tell it's being used with a drupal_set_message() thrown inside that block), but it still doesn't work. Results only appear if I add the /view/url/1 and not the /view/url/new like I want.

Please help me understand what I've done wrong and how I can fix it.

2
  • Have you checked that $argument is what you expect it to be at that point?
    – Clive
    Commented Sep 7, 2018 at 10:05
  • Yes, it's whatever is in the URL, whether that's "new", "all", etc.
    – andileco
    Commented Sep 7, 2018 at 12:21

1 Answer 1

0

If I add $this->argument->argument = '1'; to the above code, it works:

<?php

namespace Drupal\my_module_validation\Plugin\views\argument_validator;

use Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase;

/**
 * Validate the New boolean.
 *
 * @ingroup views_argument_validate_plugins
 *
 * @ViewsArgumentValidator(
 *   id = "my_module_validation",
 *   title = @Translation("MYMODULE Validation")
 * )
 */
class MyModuleValidation extends ArgumentValidatorPluginBase {

  public function validateArgument($argument) {
    if($argument == 'new' || $argument == 'New' || $argument == '1') {
      $this->argument->argument = '1';
      return TRUE;
    }
    return FALSE;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.