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.