ProductCategoryTrait.php in Commerce Core 8.2
File
modules/product/src/Plugin/Commerce/Condition/ProductCategoryTrait.php
View source
<?php
namespace Drupal\commerce_product\Plugin\Commerce\Condition;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\Core\Form\FormStateInterface;
trait ProductCategoryTrait {
protected $entityFieldManager;
protected $entityTypeManager;
protected $entityUuidMapper;
public function defaultConfiguration() {
return [
'terms' => [],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$terms = NULL;
$ids = $this
->getTermIds();
if (!empty($ids)) {
$term_storage = $this->entityTypeManager
->getStorage('taxonomy_term');
$terms = $term_storage
->loadMultiple($ids);
}
$form['terms'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Categories'),
'#default_value' => $terms,
'#target_type' => 'taxonomy_term',
'#selection_settings' => [
'target_bundles' => $this
->getVocabularyIds(),
],
'#tags' => TRUE,
'#required' => TRUE,
'#maxlength' => NULL,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state
->getValue($form['#parents']);
$term_ids = array_column($values['terms'], 'target_id');
$this->configuration['terms'] = $this->entityUuidMapper
->mapFromIds('taxonomy_term', $term_ids);
$this->configuration['terms'] = array_values($this->configuration['terms']);
}
protected function getTermIds() {
return $this->entityUuidMapper
->mapToIds('taxonomy_term', $this->configuration['terms']);
}
protected function getVocabularyIds() {
$vocabulary_ids = [];
foreach ($this
->getEntityReferenceFieldMap() as $field_name => $field_info) {
foreach ($field_info['bundles'] as $bundle) {
$field_definitions = $this->entityFieldManager
->getFieldDefinitions('commerce_product', $bundle);
$field_definition = $field_definitions[$field_name];
if ($field_definition
->getSetting('target_type') == 'taxonomy_term') {
$target_bundles = $field_definition
->getSetting('handler_settings')['target_bundles'];
if (!empty($target_bundles)) {
$vocabulary_ids = array_merge($vocabulary_ids, $target_bundles);
}
}
}
}
$vocabulary_ids = array_unique($vocabulary_ids);
return $vocabulary_ids;
}
protected function getReferencedIds(ProductInterface $product) {
$ids = [];
foreach ($this
->getEntityReferenceFieldMap() as $field_name => $field_info) {
if ($product
->hasField($field_name)) {
$field = $product
->get($field_name);
$field_definition = $field
->getFieldDefinition();
$target_type = $field_definition
->getSetting('target_type');
if ($target_type === 'taxonomy_term' && !$field
->isEmpty()) {
foreach ($field
->getValue() as $index => $field_item) {
$ids[] = $field_item['target_id'];
}
}
}
}
return $ids;
}
protected function getEntityReferenceFieldMap() {
$ignore_fields = [
'type',
'uid',
'stores',
'variations',
];
$ignore_fields = array_combine($ignore_fields, $ignore_fields);
$field_map = $this->entityFieldManager
->getFieldMapByFieldType('entity_reference');
$field_map = array_diff_key($field_map['commerce_product'], $ignore_fields);
return $field_map;
}
}