CommerceAutoSkuGeneratorBase.php in Commerce AutoSKU 8.2
File
src/Plugin/CommerceAutoSkuGenerator/CommerceAutoSkuGeneratorBase.php
View source
<?php
namespace Drupal\commerce_autosku\Plugin\CommerceAutoSkuGenerator;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
abstract class CommerceAutoSkuGeneratorBase extends PluginBase implements CommerceAutoSkuGeneratorInterface, ContainerFactoryPluginInterface {
var $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;
}
protected function isUnique(ProductVariationInterface $entity, $sku) {
$entities = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->loadByProperties([
'sku' => $sku,
]);
if (!$entity
->isNew()) {
unset($entities[$entity
->id()]);
}
return empty($entities);
}
protected function makeUnique(ProductVariationInterface $entity, $sku) {
$generated_sku = preg_replace('/[\\t\\n\\r\\0\\x0B]/', '', strip_tags($sku));
$output = $generated_sku;
$i = 0;
while (!$this
->isUnique($entity, $output)) {
$counter_length = mb_strlen($i) + 1;
$un_prefixed_max_length = 255 - $counter_length;
$sku = mb_strlen($generated_sku, 0, $un_prefixed_max_length);
$output = $sku . '_' . $i;
$i++;
}
return $output;
}
public function generate(ProductVariationInterface $entity) {
$generated_sku = $this
->getSku($entity);
if (empty($generated_sku)) {
$generated_sku = $this
->getAlternativeSku($entity);
}
return $this
->makeUnique($entity, $generated_sku);
}
protected function getAlternativeSku(ProductVariationInterface $entity) {
$content_type = $this
->getBundleLabel($entity);
if ($entity
->id()) {
$label = t('@type @id', array(
'@type' => $content_type,
'@id' => $entity
->id(),
));
}
else {
$label = $content_type;
}
return $label;
}
protected function getBundleLabel(ProductVariationInterface $entity) {
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
if ($bundle != $entity_type) {
$bundle_entity_type = $this->entityTypeManager
->getDefinition($entity_type)
->getBundleEntityType();
$label = $this->entityTypeManager
->getStorage($bundle_entity_type)
->load($bundle)
->label();
}
else {
$label = $this->entityTypeManager
->getDefinition($entity_type)
->getLabel();
}
return $label;
}
protected abstract function getSku(ProductVariationInterface $entity);
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
return $this;
}
public function calculateDependencies() {
return [
'module' => [
$this->pluginDefinition['provider'],
],
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this
->setConfiguration($values);
}
}
}