PosAddressAdvanceFilter.php
Tue Oct 18 2022 22:10:39 GMT+0000 (Coordinated Universal Time)
<?php
namespace Drupal\pos_field_address\Plugin\views\filter;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\pos_field_address\PosAddressConfigBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a custom filter for filtering by addresses.
*
* @ingroup views_filter_handlers
* @ViewsFilter("pos_address_advance_filter")
*/
class PosAddressAdvanceFilter extends FilterPluginBase {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Config builder.
*
* @var \Drupal\pos_field_address\PosAddressConfigBuilder
*/
protected $configBuilder;
/**
* {@inheritdoc}
*/
public $operator = 'LIKE';
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection, PosAddressConfigBuilder $config_builder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->connection = $connection;
$this->configBuilder = $config_builder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('database'),
$container->get('pos_field_address.config_builder')
);
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['operator'] = ['default' => 'LIKE'];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildExposedForm(&$form, FormStateInterface $form_state) {
$identifier = $this->options['expose']['identifier'];
$exposed_input = $this->view->getExposedInput();
$form[$identifier] = [
'#type' => 'fieldset',
'#title' => $this->options['expose']['label'],
'#attached' => [
'library' => [
'pos_field_address/view-exposed-filter',
],
],
];
$element = &$form[$identifier];
// Define classes.
$country_class = str_replace('_', '-', $identifier) . '-country';
$schema_class = str_replace('_', '-', $identifier) . '-schema';
$value_class = str_replace('_', '-', $identifier) . '-value';
// Country element.
$countries = $this->configBuilder->getCountries();
$country_code = isset($exposed_input[$identifier . '_country']) ? $exposed_input[$identifier . '_country'] : key($countries);
$element[$identifier . '_country'] = [
'#type' => 'select',
'#title' => 'Entitet',
'#options' => $this->configBuilder->getCountries(),
'#attributes' => [
'class' => ['address-filer-country', $country_class],
'data-schema-selector' => '.' . $schema_class,
'data-value-selector' => '.' . $value_class,
],
'#default_value' => $country_code,
];
// Country schema element.
$country_schema = $this->configBuilder->getCountrySchema($country_code);
$country_schema_key = isset($exposed_input[$identifier . '_country_schema']) ? $exposed_input[$identifier . '_country_schema'] : key($country_schema);
// $element[$identifier . '_country_schema'] = [
// '#type' => 'select',
// '#title' => $this->t('Search by'),
// '#options' => $country_schema,
// '#attributes' => [
// 'class' => ['address-filter-schema', $schema_class],
// 'data-country-selector' => '.' . $country_class,
// 'data-value-selector' => '.' . $value_class,
// ],
// '#default_value' => $country_schema_key,
// ];
// Value element.
$values = $this->configBuilder->getAllOptionsForCountrySchemaElement($country_code, $country_schema_key);
$options = ['' => $this->t('- None -')];
foreach ($values as $value) {
$serialized = serialize($value['value']);
$options[str_replace('}', '', substr($serialized, strpos($serialized, '{') + 1))] = $value['name'];
}
$default_value = isset($exposed_input[$identifier]) ? $exposed_input[$identifier] : NULL;
$element[$identifier] = [
'#title' => $this->options['expose']['label'],
'#type' => 'select',
'#attributes' => [
'class' => [$value_class, 'address-filter-city'],
'data-schema-selector' => '.' . $schema_class,
],
'#options' => $options,
'#default_value' => $default_value,
];
}
/**
* {@inheritdoc}
*/
public function query() {
if (!empty($this->value) && !empty(reset($this->value))) {
$this->ensureMyTable();
$this->query->addWhere($this->options['group'], "$this->tableAlias.$this->realField", '%' . $this->connection->escapeLike(reset($this->value)) . '%', $this->operator);
}
else if (!empty($this->view->getExposedInput()) && $this->view->getExposedInput()['main_company_address_country'] === 'BD') {
$no_settlement = 's:0:""';
$this->query->addWhere($this->options['group'], "$this->tableAlias.$this->realField", '%' . $this->connection->escapeLike($no_settlement) . '%', $this->operator);
}
}
}



Comments