EntityBundle.php in Commerce Core 8.2
File
src/Plugin/views/filter/EntityBundle.php
View source
<?php
namespace Drupal\commerce\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\Bundle;
use Drupal\Core\Session\AccountInterface;
class EntityBundle extends Bundle {
public function defaultExposeOptions() {
parent::defaultExposeOptions();
$this->options['expose']['hide_single_bundle'] = TRUE;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['expose']['contains']['hide_single_bundle'] = [
'default' => TRUE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['expose']['hide_single_bundle'] = [
'#type' => 'checkbox',
'#title' => $this
->t("Hide if there's only one bundle."),
'#default_value' => $this->options['expose']['hide_single_bundle'],
];
}
public function access(AccountInterface $account) {
$bundles = $this->bundleInfoService
->getBundleInfo($this
->getEntityType());
if ($this->options['expose']['hide_single_bundle'] && count($bundles) <= 1) {
return FALSE;
}
return parent::access($account);
}
public function getValueOptions() {
if (!isset($this->valueOptions)) {
$types = $this->bundleInfoService
->getBundleInfo($this->entityTypeId);
$storage = $this->entityTypeManager
->getStorage($this->entityTypeId);
foreach ($types as $type => $info) {
if ($this
->isExposed()) {
$stub_entity = $storage
->create([
$this->entityType
->getKey('bundle') => $type,
]);
if (!$stub_entity
->access('view')) {
unset($types[$type]);
}
}
}
$this->valueTitle = $this
->t('@entity types', [
'@entity' => $this->entityType
->getLabel(),
]);
$options = [];
foreach ($types as $type => $info) {
$options[$type] = $info['label'];
}
asort($options);
$this->valueOptions = $options;
}
return $this->valueOptions;
}
public function calculateDependencies() {
$parents_parent = get_parent_class(get_parent_class($this));
$dependencies = $parents_parent::calculateDependencies();
$bundle_entity_type = $this->entityType
->getBundleEntityType();
if ($bundle_entity_type) {
$bundle_entity_storage = $this->entityTypeManager
->getStorage($bundle_entity_type);
foreach (array_keys($this->value) as $bundle) {
if ($bundle_entity = $bundle_entity_storage
->load($bundle)) {
$dependencies[$bundle_entity
->getConfigDependencyKey()][] = $bundle_entity
->getConfigDependencyName();
}
}
}
return $dependencies;
}
}