TODO:

  1. Add "uid" entity key to contacts.
  2. Fix key check in CRMCorePermissions::entityTypePermissions.
  3. Rename "user" entity key to "uid" in activity entity.
  4. Add test coverage for new permissions
  5. ...
CommentFileSizeAuthor
#4 crm_core-2928452-3.patch11.96 KBRoSk0
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

RoSk0 created an issue. See original summary.

RoSk0’s picture

Assigned: Unassigned » RoSk0

Starting...

RoSk0’s picture

Issue summary: View changes
Status: Active » Needs review

First increment, no tests for new permissions but with fixes for current tests.
Decided to leave activity owner for now.

RoSk0’s picture

FileSize
11.96 KB

Patch itself

grahl’s picture

Version: 8.x-1.x-dev » 8.x-3.x-dev
Assigned: RoSk0 » Unassigned

Need to check if still relevant, obviously no longer applies

  • grahl committed 1b0468e on 8.x-3.x authored by RoSk0
    Issue #2928452 by RoSk0, grahl: Add "uid" entity key to contacts and fix...
grahl’s picture

Status: Needs review » Fixed
Issue tags: +ContributionWeekend2020, +ContributionWeekendCH

EntityOwnerTrait is now in core and could be removed from crm_core, key switched to "owner" for that.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

grahl’s picture

To update an existing site you can run this:

  $db = \Drupal::database();
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  $types = [
    'crm_core_individual' => Individual::class,
    'crm_core_organization' => Organization::class,
  ];

  // Column changes not allowed in schema, copy and reinstall.
  foreach ($types as $type => $class) {
    $db->query("ALTER TABLE $type ADD `uid_legacy` int(10) NOT NULL");
    $db->query("UPDATE $type set `uid_legacy` = `uid`");
    $entity_type = $definition_update_manager->getEntityType($type);
    $old_storage = $definition_update_manager->getFieldStorageDefinition('uid', $type);
    $provider = $old_storage->getProvider();
    $definition_update_manager->uninstallFieldStorageDefinition($old_storage);

    $new_storage = BaseFieldDefinition::create('entity_reference')
      ->setLabel(new TranslatableMarkup('User ID'))
      ->setSetting('target_type', 'user')
      ->setTranslatable($entity_type->isTranslatable())
      ->setDefaultValueCallback($class . '::getDefaultEntityOwner');
    $definition_update_manager->installFieldStorageDefinition('uid', $type, $provider, $new_storage);
    $db->query("UPDATE $type set `uid` = `uid_legacy`");
    $db->query("ALTER TABLE $type DROP COLUMN `uid_legacy`");
  }

  // Activities can be modified directly.
  $entity_type = $definition_update_manager->getEntityType('crm_core_activity');
  /** @var \Drupal\entity_test\FieldStorageDefinition $old_storage */
  $old_storage = $definition_update_manager->getFieldStorageDefinition('uid', 'crm_core_activity');
  $old_storage->setLabel(new TranslatableMarkup('User ID'))
    ->setSetting('target_type', 'user')
    ->setTranslatable($entity_type->isTranslatable())
    ->setRevisionable(FALSE)
    ->setDefaultValueCallback(Activity::class . '::getDefaultEntityOwner');
  $definition_update_manager->updateFieldStorageDefinition($old_storage);
RoSk0’s picture

In case someone would stumble upon comment #9 - it is not required. "Mismatched entity and/or field definitions" on a status report was because of the revisionable behaviour not set by default in core's EntityOwnerTrait::ownerBaseFieldDefinitions() for "uid" (owner) base field.

Revisionable behaviour was restored in https://git.drupalcode.org/project/crm_core/-/commit/22c40d0563521f73498...

sahaj’s picture

I'm still having:

Mismatched entity and/or field definitions
The following changes were detected in the entity type and field definitions.
Individual
The User ID field needs to be updated.
Organization
The User ID field needs to be updated.

If #9 not required, then what is required to get ride of these errors? I'm on Drupal 9.2.7 and the last crm_core dev.

Also tried the unrecommended (for that purpose) https://www.drupal.org/project/devel_entity_updates

bluegeek9’s picture

I was able to fix the error with the following code. The first time I ran it I got an error about uid being null, but it did create the column. I updated the uid field manually, and execute the code below again.

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;


$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
foreach(['crm_core_individual', 'crm_core_organization'] as $entity_name) {
  $entity_type = $definition_update_manager->getEntityType($entity_name);
  $entity_type_id = $entity_type->id();

  $uid =  BaseFieldDefinition::create('entity_reference')
        ->setLabel(new TranslatableMarkup('User ID'))
        ->setSetting('target_type', 'user')
        ->setTranslatable($entity_type
        ->isTranslatable())->setRevisionable(TRUE)
        ->setDefaultValueCallback(static::class . '::getDefaultEntityOwner');

  $definition_update_manager->installFieldStorageDefinition('uid', $entity_type_id, $entity_type_id, $uid);
}