You are here

function consumers_update_8106 in Consumers 8

Update entity definition to add the "owner" key and adapt the field.

File

./consumers.install, line 102
Install, update and uninstall functions for Consumers.

Code

function consumers_update_8106() {
  $edum = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $edum
    ->getEntityType('consumer');
  $keys = $entity_type
    ->getKeys();
  $owner_key = 'owner_id';
  $entity_type
    ->set('entity_keys', $keys + [
    'owner' => $owner_key,
  ]);
  $edum
    ->updateEntityType($entity_type);
  $field_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(new TranslatableMarkup('User ID'))
    ->setSetting('target_type', 'user')
    ->setTranslatable($entity_type
    ->isTranslatable())
    ->setDefaultValueCallback(Consumer::class . '::getDefaultEntityOwner');
  $field_storage_definition = $field_definition
    ->getFieldStorageDefinition();
  $has_original = (bool) $edum
    ->getFieldStorageDefinition($owner_key, $field_storage_definition
    ->getTargetEntityTypeId());
  $has_original ? $edum
    ->updateFieldStorageDefinition($field_storage_definition) : $edum
    ->installFieldStorageDefinition('owner_id', 'consumer', 'consumers', $field_definition);
}