EntityBundleBase.php in Commerce Core 8.2
File
src/Plugin/Commerce/Condition/EntityBundleBase.php
View source
<?php
namespace Drupal\commerce\Plugin\Commerce\Condition;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class EntityBundleBase extends ConditionBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
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'));
}
public function defaultConfiguration() {
return [
'bundles' => [],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$entity_type = $this->entityTypeManager
->getDefinition($this
->getEntityTypeId());
$bundle_entity_type = $this->entityTypeManager
->getDefinition($entity_type
->getBundleEntityType());
$form['bundles'] = [
'#type' => 'commerce_entity_select',
'#title' => $bundle_entity_type
->getLabel(),
'#default_value' => $this->configuration['bundles'],
'#target_type' => $bundle_entity_type
->id(),
'#hide_single_entity' => FALSE,
'#autocomplete_threshold' => 10,
'#multiple' => TRUE,
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state
->getValue($form['#parents']);
$this->configuration['bundles'] = $values['bundles'];
}
public function evaluate(EntityInterface $entity) {
$this
->assertEntity($entity);
return in_array($entity
->bundle(), $this->configuration['bundles']);
}
}