1

I am creating a custom filter for views. In *.views.inc file I am using hook_views_data_alter() to create the filter in views. Then creating a class for filter that extends NumericFilter filter class. Now, in defineOptions() method I want to feed the min and max numbers. These min and max data comes from a field in the view. Therefore, I want to know how can I get field data in that defineOptions() method.

class ItemQuantity extends NumericFilter {

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->definition['options callback'] = array($this, 'generateOptions');
  }

  protected function defineOptions() {
    $options = parent::defineOptions();

    $options['value'] = [
      'contains' => [
        'min' => ['default' => ''],
        'max' => ['default' => ''],
        'value' => ['default' => ''],
      ],
    ];

    return $options;
  }

  public function operators() {
    $operators = parent::operators();
    $operators['overlaps'] = [
      'title' => $this->t('Overlaps by'),
      'method' => 'opOverlap',
      'short' => $this->t('<>'),
      'values' => 1,
    ];
    return $operators;
  }

  protected function opOverlap($field) {
    $this->query->addWhere($this->options['group'], $field, [$this->value['min'], $this->value['max']], 'IN');
  }

  /**
   * Override the query so that no filtering takes place if the user doesn't
   * select any options.
   */
  public function query() {
    if (!empty($this->value)) {
      parent::query();
    }
  }

  /**
   * Skip validation if no options have been chosen so we can use it as a
   * non-filter.
   */
  public function validate() {
    if (!empty($this->value)) {
      parent::validate();
    }
  }

}

I want to get the value of min_quantity field in the view.

  • some advance with the problem? – Adrian Cid Almaguer Sep 12 '17 at 13:29
  • Yes, but now I am getting another error. "The configuration property display.default.display_options.filters.quantity_filter.value.min doesn't exist." . I am working to resolve this error, once this filter starts working, I'll post the solution so that others might get help :) – sorabh.v6 Sep 13 '17 at 10:51
  • I think that you should open another question because this question How to get views field data in defineOptions() method of custom filter? is answered with my answer, and now you have a different problem, that seems to be related with a config file. – Adrian Cid Almaguer Sep 13 '17 at 13:04
0

You can access with:

$this->options['value']

Read more about this in Building Views Query Plugins for Drupal 8, Part 3

|improve this answer|||||
  • Thanks for the answer, but $this->options is giving me a blank array. – sorabh.v6 Sep 6 '17 at 15:43
  • @sorabh.v6 can you add more code to the question? Remember that you need to implement the buildOptionsForm() method. – Adrian Cid Almaguer Sep 6 '17 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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