ProductAttributeValue.php in Commerce Core 8.2
File
modules/product/src/Plugin/views/filter/ProductAttributeValue.php
View source
<?php
namespace Drupal\commerce_product\Plugin\views\filter;
use Drupal\commerce_product\Entity\ProductAttributeInterface;
use Drupal\commerce_product\Entity\ProductAttributeValueInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Plugin\views\filter\InOperator;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProductAttributeValue extends InOperator {
protected $entityTypeManager;
protected $entityRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity.repository'));
}
public function getValueOptions() {
if (!isset($this->valueOptions)) {
$attribute_storage = $this->entityTypeManager
->getStorage('commerce_product_attribute');
$attribute = $attribute_storage
->load($this->definition['attribute']);
assert($attribute instanceof ProductAttributeInterface);
$attribute_values = array_map(function (ProductAttributeValueInterface $value) {
return $this->entityRepository
->getTranslationFromContext($value);
}, $attribute
->getValues());
foreach ($attribute_values as $value) {
$this->valueOptions[$value
->id()] = $value
->label();
}
}
return $this->valueOptions;
}
}