You are here

function commerce_wishlist_field_widget_form_alter in Commerce Wishlist 8.3

Implements hook_field_widget_form_alter().

  • Changes the label of the purchasable_entity field to the label of the target type (e.g. 'Product variation').
  • Forbids editing the purchasable_entity once the wishlist item is no longer new.

File

./commerce_wishlist.module, line 94
Defines the Wishlist entity and associated features.

Code

function commerce_wishlist_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {

  /** @var \Drupal\Core\Field\FieldItemListInterface $items */
  $items = $context['items'];

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $items
    ->getFieldDefinition();
  $field_name = $field_definition
    ->getName();
  $entity_type = $field_definition
    ->getTargetEntityTypeId();
  if ($field_name == 'purchasable_entity' && $entity_type == 'commerce_wishlist_item') {
    if (!empty($element['target_id']['#target_type'])) {
      $target_type = \Drupal::entityTypeManager()
        ->getDefinition($element['target_id']['#target_type']);
      $element['target_id']['#title'] = $target_type
        ->getLabel();
      if (!$items
        ->getEntity()
        ->isNew()) {
        $element['#disabled'] = TRUE;
      }
    }
  }
}