You are here

function commerce_invoice_update_8201 in Commerce Invoice 8.2

Update the 'uid' field.

File

./commerce_invoice.install, line 43
Contains install and update functions for Commerce Invoice.

Code

function commerce_invoice_update_8201() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $base_field_override_storage = \Drupal::entityTypeManager()
    ->getStorage('base_field_override');
  $storage_definition = $definition_update_manager
    ->getFieldStorageDefinition('uid', 'commerce_invoice');
  $default_value_callback = Invoice::class . '::getCurrentUserId';
  $base_field_overrides = $base_field_override_storage
    ->loadByProperties([
    'entity_type' => 'commerce_invoice',
    'field_name' => 'uid',
  ]);

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $base_field_override */
  foreach ($base_field_overrides as $base_field_override) {
    if ($base_field_override
      ->getDefaultValueCallback() !== $storage_definition
      ->getDefaultValueCallback()) {
      continue;
    }

    // Update the "default_value_callback" for base field overrides, as long
    // as they're using the default one.
    $base_field_override
      ->setDefaultValueCallback($default_value_callback);
    $base_field_override
      ->save();
  }
  $storage_definition
    ->setDefaultValueCallback($default_value_callback);
  $definition_update_manager
    ->updateFieldStorageDefinition($storage_definition);
}