You are here

function commerce_autosku_select_mode in Commerce AutoSKU 8.2

Selects the operating mode of the Automatic SKU module.

Parameters

\Drupal\commerce_autosku\CommerceAutoSkuManagerInterface $entity: The entity.

$form: Entity creation form.

3 calls to commerce_autosku_select_mode()
commerce_autosku_form_commerce_product_form_alter in ./commerce_autosku.module
Implements hook_form_BASE_FORM_ID_alter().
commerce_autosku_form_commerce_product_variation_form_alter in ./commerce_autosku.module
Implements hook_form_BASE_FORM_ID_alter().
commerce_autosku_inline_entity_form_entity_form_alter in ./commerce_autosku.module
Implements hook_inline_entity_form_entity_form_alter().

File

./commerce_autosku.module, line 67
Allows hiding of entity label fields and automatic label creation.

Code

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.');
      }
    }
  }
}