You are here

function profile_update_8004 in Profile 8

Add the uid entity key to profiles.

1 call to profile_update_8004()
profile_update_8005 in ./profile.install
Rerun uid entity key addition after incorrect entity type fixed.

File

./profile.install, line 95
Install, update and uninstall functions for the profile module.

Code

function profile_update_8004() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $definition_update_manager
    ->getEntityType('profile');

  // Fix any null `uid` field values.
  $database = \Drupal::database();
  $database
    ->update($entity_type
    ->getBaseTable())
    ->fields([
    'uid' => 0,
  ])
    ->isNull('uid')
    ->execute();
  $database
    ->update($entity_type
    ->getRevisionTable())
    ->fields([
    'uid' => 0,
  ])
    ->isNull('uid')
    ->execute();
  $entity_keys = $entity_type
    ->getKeys();
  $entity_keys['uid'] = 'uid';
  $entity_keys['owner'] = 'uid';
  $entity_type
    ->set('entity_keys', $entity_keys);
  $definition_update_manager
    ->updateEntityType($entity_type);
  $uid_definition = $definition_update_manager
    ->getFieldStorageDefinition('uid', 'profile');
  $definition_update_manager
    ->updateFieldStorageDefinition($uid_definition);
  return t('The uid entity key has been added to profiles.');
}