You are here

abstract class CommerceAutoSkuGeneratorBase in Commerce AutoSKU 8.2

Places an order through a series of steps.

Checkout flows are multi-step forms that can be configured by the store administrator. This configuration is stored in the commerce_checkout_flow config entity and injected into the plugin at instantiation.

Hierarchy

Expanded class hierarchy of CommerceAutoSkuGeneratorBase

File

src/Plugin/CommerceAutoSkuGenerator/CommerceAutoSkuGeneratorBase.php, line 19

Namespace

Drupal\commerce_autosku\Plugin\CommerceAutoSkuGenerator
View source
abstract class CommerceAutoSkuGeneratorBase extends PluginBase implements CommerceAutoSkuGeneratorInterface, ContainerFactoryPluginInterface {

  /**
   * Entity type manager.
   *
   * @var EntityTypeManagerInterface
   */
  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;
  }

  /**
   * Validate if sku is unique.
   *
   * @param ProductVariationInterface $entity
   *   Product Variation.
   * @param string $sku
   *   SKU.
   *
   * @return bool
   *   TRUE if SKU unique FALSE otherwise.
   */
  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) {

    // Strip tags.
    $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;
  }

  /**
   * Generates the SKU according to the settings.
   *
   * @param ProductVariationInterface $entity
   *   Content entity.
   *
   * @return string
   *   A label string
   */
  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);
  }

  /**
   * Gets an alternative SKU.
   *
   * @return string
   *   Translated label string.
   */
  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;
  }

  /**
   * Gets the entity bundle label or the entity label.
   *
   * @return string
   *   The bundle label.
   */
  protected function getBundleLabel(ProductVariationInterface $entity) {
    $entity_type = $entity
      ->getEntityTypeId();
    $bundle = $entity
      ->bundle();

    // Use the the human readable name of the bundle type. If this entity has no
    // bundle, we use the name of the content entity type.
    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;
  }

  /**
   * {@inheritdoc}
   */
  protected abstract function getSku(ProductVariationInterface $entity);

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeep($this
      ->defaultConfiguration(), $configuration);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [
      'module' => [
        $this->pluginDefinition['provider'],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getErrors()) {
      $values = $form_state
        ->getValue($form['#parents']);
      $this
        ->setConfiguration($values);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceAutoSkuGeneratorBase::$entityTypeManager property Entity type manager.
CommerceAutoSkuGeneratorBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 1
CommerceAutoSkuGeneratorBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
CommerceAutoSkuGeneratorBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 1
CommerceAutoSkuGeneratorBase::generate public function Generates the SKU according to the settings. Overrides CommerceAutoSkuGeneratorInterface::generate
CommerceAutoSkuGeneratorBase::getAlternativeSku protected function Gets an alternative SKU.
CommerceAutoSkuGeneratorBase::getBundleLabel protected function Gets the entity bundle label or the entity label.
CommerceAutoSkuGeneratorBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
CommerceAutoSkuGeneratorBase::getSku abstract protected function 1
CommerceAutoSkuGeneratorBase::isUnique protected function Validate if sku is unique.
CommerceAutoSkuGeneratorBase::makeUnique protected function
CommerceAutoSkuGeneratorBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
CommerceAutoSkuGeneratorBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
CommerceAutoSkuGeneratorBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
CommerceAutoSkuGeneratorBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
ContainerFactoryPluginInterface::create public static function Creates an instance of the plugin. 112
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.