CountryAwareInOperatorBase.php in Address 8
File
src/Plugin/views/filter/CountryAwareInOperatorBase.php
View source
<?php
namespace Drupal\address\Plugin\views\filter;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Plugin\views\filter\InOperator;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class CountryAwareInOperatorBase extends InOperator {
protected $countryRepository;
protected $entityTypeManager;
protected $entityFieldManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CountryRepositoryInterface $country_repository, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->countryRepository = $country_repository;
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('address.country_repository'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'));
}
protected function getFieldName() {
if (isset($this->configuration['field_name'])) {
$field_name = $this->configuration['field_name'];
}
else {
$field_name = $this->configuration['entity field'];
}
return $field_name;
}
protected function getAvailableCountries(EntityTypeInterface $entity_type = NULL, $field_name = NULL) {
if (!isset($entity_type)) {
$entity_type_id = $this
->getEntityType();
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
}
if (!isset($field_name)) {
$field_name = $this
->getFieldName();
}
$bundles = $this
->getBundles($entity_type, $field_name);
$storage = $this->entityTypeManager
->getStorage($entity_type
->id());
$countries_by_bundle = [];
foreach ($bundles as $bundle) {
$values = [];
if ($bundle_key = $entity_type
->getKey('bundle')) {
$values[$bundle_key] = $bundle;
}
$entity = $storage
->create($values);
if ($entity
->hasField($field_name)) {
$countries_by_bundle[$bundle] = $entity
->get($field_name)
->appendItem()
->getAvailableCountries();
}
}
$countries = [];
$countries_by_bundle = array_filter($countries_by_bundle);
if (count($countries_by_bundle) === 1) {
$countries = reset($countries_by_bundle);
}
elseif (count($countries_by_bundle) > 1) {
$countries = array_pop($countries_by_bundle);
foreach ($countries_by_bundle as $list) {
$countries = array_intersect_key($countries, $list);
}
}
$available_countries = $this->countryRepository
->getList();
if (!empty($countries)) {
$available_countries = array_intersect_key($available_countries, $countries);
}
return $available_countries;
}
protected function getBundles(EntityTypeInterface $entity_type, $field_name) {
$bundles = [];
$bundle_key = $entity_type
->getKey('bundle');
if ($bundle_key && isset($this->view->filter[$bundle_key])) {
$filter = $this->view->filter[$bundle_key];
if (!$filter
->isExposed() && !empty($filter->value)) {
$bundles = array_diff($filter->value, [
'all',
]);
}
}
if (empty($bundles)) {
$map = $this->entityFieldManager
->getFieldMap();
$bundles = $map[$entity_type
->id()][$field_name]['bundles'];
}
return $bundles;
}
}