You are here

function commerce_order_field_widget_form_alter in Commerce Core 8.2

Implements hook_field_widget_form_alter().

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

File

modules/order/commerce_order.module, line 125
Defines the Order entity and associated features.

Code

function commerce_order_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $field_definition = $context['items']
    ->getFieldDefinition();
  $field_name = $field_definition
    ->getName();
  $entity_type = $field_definition
    ->getTargetEntityTypeId();
  if ($field_name == 'purchased_entity' && $entity_type == 'commerce_order_item') {
    if (!empty($element['target_id']['#target_type'])) {
      $target_type = \Drupal::service('entity_type.manager')
        ->getDefinition($element['target_id']['#target_type']);
      $element['target_id']['#title'] = $target_type
        ->getLabel();
      if (!$context['items']
        ->getEntity()
        ->isNew()) {
        $element['#disabled'] = TRUE;
      }
    }
  }
}