commerce_autosku.module in Commerce AutoSKU 8.2
Same filename and directory in other branches
Allows hiding of entity label fields and automatic label creation.
File
commerce_autosku.moduleView source
<?php
/**
* @file
* Allows hiding of entity label fields and automatic label creation.
*/
use Drupal\commerce_autosku\CommerceAutoSkuManagerInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_entity_type_alter().
*
* Adds the Auto Label tab to the entity configuration page.
*/
function commerce_autosku_entity_type_alter(array &$entity_types) {
$entity_types['commerce_product_variation_type']
->setLinkTemplate('auto-sku', '/admin/commerce/config/product-variation-types/{commerce_product_variation_type}/auto-sku');
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* Integrates AutoSKU into the single variation product widget.
*/
function commerce_autosku_form_commerce_product_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['variations']['widget']['entity']['#inline_form'])) {
$entity_form = $form['variations']['widget']['entity']['#inline_form'];
if ($entity_form) {
$entity = $entity_form
->getEntity();
$entity_form =& $form['variations']['widget']['entity'];
commerce_autosku_select_mode($entity, $entity_form);
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function commerce_autosku_form_commerce_product_variation_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$entity = $form_state
->getFormObject()
->getEntity();
commerce_autosku_select_mode($entity, $form);
}
/**
* Implements hook_inline_entity_form_entity_form_alter().
*/
function commerce_autosku_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
if ($entity_form['#entity_type'] == 'commerce_product_variation') {
$entity = $entity_form['#entity'];
commerce_autosku_select_mode($entity, $entity_form);
}
}
/**
* Selects the operating mode of the Automatic SKU module.
*
* @param \Drupal\commerce_autosku\CommerceAutoSkuManagerInterface $entity
* The entity.
*
* @param $form
* Entity creation form.
*/
function commerce_autosku_select_mode(&$entity, &$form) {
$decorator = \Drupal::service('commerce_autosku.entity_decorator');
$entity = $decorator
->decorate($entity);
if ($entity instanceof CommerceAutoSkuManagerInterface) {
if ($entity
->hasAutoSku()) {
$sku = $entity
->getSkuName();
$widget =& $form[$sku]['widget'][0];
// Hide the label field. It will be automatically generated in
// hook_entity_presave().
$widget['value']['#type'] = 'hidden';
$widget['value']['#required'] = FALSE;
if (empty($widget['value']['#default_value'])) {
$widget['value']['#default_value'] = '%AutoSku%';
}
}
else {
if ($entity
->hasOptionalAutoSku()) {
$sku_property = $entity
->getSkuName();
$widget =& $form[$sku_property]['widget'][0];
// Allow label field to be empty. It will be automatically generated
// in hook_entity_presave().
$widget['value']['#required'] = FALSE;
$widget['value']['#attributes']['placeholder'] = t('Leave blank for automatic generation.');
}
}
}
}
/**
* Implements hook_entity_prepare_view().
*/
function commerce_autosku_entity_prepare_view($entity_type_id, array $entities, array $displays, $view_mode) {
if ($entity_type_id != 'commerce_product_variation') {
return;
}
foreach ($entities as $entity) {
if ($entity->in_preview === TRUE && $entity instanceof ContentEntityInterface) {
$decorator = \Drupal::service('commerce_autosku.entity_decorator');
/** @var \Drupal\commerce_autosku\CommerceAutoSkuManager $decorated_entity */
$decorated_entity = $decorator
->decorate($entity);
if ($decorated_entity
->hasSku() && $decorated_entity
->autoSkuNeeded()) {
$decorated_entity
->setSku();
}
}
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*
* Since some tokens like variation_id do not exist in hook_entity_presave, we need to set it here.
*/
function commerce_autosku_commerce_product_variation_insert(EntityInterface $entity) {
commerce_autosku_commerce_product_variation_set_sku($entity);
}
/**
* Implements hook_ENTITY_TYPE_update().
*
* Since some tokens like variation_id do not exist in hook_entity_presave, we need to set it here.
*/
function commerce_autosku_commerce_product_variation_update(EntityInterface $entity) {
commerce_autosku_commerce_product_variation_set_sku($entity);
}
/**
* Generate and set the SKU of a product variation.
*
* @param \Drupal\commerce_product\Entity\ProductVariationInterface $entity
* The product variation entity.
* @param bool $save
* Whether the entity should be saved or not.
*/
function commerce_autosku_commerce_product_variation_set_sku(ProductVariationInterface $entity, $save = TRUE) {
if ($entity instanceof EntityInterface && $entity
->hasField('sku')) {
$decorator = \Drupal::service('commerce_autosku.entity_decorator');
/** @var \Drupal\commerce_autosku\CommerceAutoSkuManager $decorated_entity */
$decorated_entity = $decorator
->decorate($entity);
if ($decorated_entity
->hasSku() && $decorated_entity
->autoSkuNeeded()) {
$decorated_entity
->setSku();
if ($save) {
$entity
->save();
}
}
}
}
/**
* Implements hook_validation_constraint_alter().
*
* Override core NotNull constraint to allow entities that use Auto Entity
* Labels to validate when their label is empty before being set automatically.
*/
function commerce_autosku_validation_constraint_alter(array &$definitions) {
$definitions['NotNull']['class'] = 'Drupal\\commerce_autosku\\Plugin\\Validation\\CommerceSkuNotNullConstraint';
}
/**
* Implements hook_entity_operation().
*/
function commerce_autosku_entity_operation(EntityInterface $entity) {
$operations = array();
$entity_type = $entity
->getEntityType();
$entity_type_id = $entity_type
->id();
$entity_id = $entity
->id();
if ($entity
->hasLinkTemplate('auto-sku') && \Drupal::currentUser()
->hasPermission('administer ' . $entity_type_id . ' SKU')) {
$operations['auto-sku'] = array(
'title' => t('Manage automatic SKU'),
'weight' => 100,
'url' => Url::fromRoute("entity.{$entity_type_id}.auto_sku", array(
$entity_type_id => $entity_id,
)),
);
}
return $operations;
}
Functions
Name | Description |
---|---|
commerce_autosku_commerce_product_variation_insert | Implements hook_ENTITY_TYPE_insert(). |
commerce_autosku_commerce_product_variation_set_sku | Generate and set the SKU of a product variation. |
commerce_autosku_commerce_product_variation_update | Implements hook_ENTITY_TYPE_update(). |
commerce_autosku_entity_operation | Implements hook_entity_operation(). |
commerce_autosku_entity_prepare_view | Implements hook_entity_prepare_view(). |
commerce_autosku_entity_type_alter | Implements hook_entity_type_alter(). |
commerce_autosku_form_commerce_product_form_alter | Implements hook_form_BASE_FORM_ID_alter(). |
commerce_autosku_form_commerce_product_variation_form_alter | Implements hook_form_BASE_FORM_ID_alter(). |
commerce_autosku_inline_entity_form_entity_form_alter | Implements hook_inline_entity_form_entity_form_alter(). |
commerce_autosku_select_mode | Selects the operating mode of the Automatic SKU module. |
commerce_autosku_validation_constraint_alter | Implements hook_validation_constraint_alter(). |