diff --git a/src/Entity/VoteResult.php b/src/Entity/VoteResult.php index e7999125b6719bd5fb0017294272c5d9101d63bf..1981604ce93f057552ad4e42478ef239a0bc28c0 100644 --- a/src/Entity/VoteResult.php +++ b/src/Entity/VoteResult.php @@ -32,7 +32,8 @@ use Drupal\votingapi\VoteResultInterface; * }, * base_table = "votingapi_result", * entity_keys = { - * "id" = "id" + * "id" = "id", + * "uuid" = "uuid", * } * ) */ @@ -127,11 +128,10 @@ class VoteResult extends ContentEntityBase implements VoteResultInterface { */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array { /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ - $fields['id'] = BaseFieldDefinition::create('integer') - ->setLabel(new TranslatableMarkup('ID')) - ->setDescription(new TranslatableMarkup('The vote result ID.')) - ->setReadOnly(TRUE) - ->setSetting('unsigned', TRUE); + $fields = parent::baseFieldDefinitions($entity_type); + + // Override parent id description. + $fields['id']->setDescription(new TranslatableMarkup('The vote result ID.')); $fields['type'] = BaseFieldDefinition::create('entity_reference') ->setLabel(new TranslatableMarkup('Type')) diff --git a/src/VoteResultFunctionManager.php b/src/VoteResultFunctionManager.php index 595ba2adb436291fec1480e4455082e86a0af677..999a1a358b76275759fad20752848e6c2949bbc0 100644 --- a/src/VoteResultFunctionManager.php +++ b/src/VoteResultFunctionManager.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Drupal\votingapi; use Drupal\Component\Datetime\TimeInterface; +use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -46,6 +47,11 @@ class VoteResultFunctionManager extends DefaultPluginManager implements VoteResu */ protected $datetime; + /** + * @var \Drupal\Component\Uuid\UuidInterface + */ + protected $uuid; + /** * Constructs a new VoteResultFunctionManager. * @@ -62,12 +68,15 @@ class VoteResultFunctionManager extends DefaultPluginManager implements VoteResu * The entity_type.manager service. * @param \Drupal\Component\Datetime\TimeInterface $datetime * The datetime.time service. + * @param \Drupal\Component\Uuid\UuidInterface $uuid + * The UUID service. */ - public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, Connection $database, EntityTypeManagerInterface $entity_type_manager, TimeInterface $datetime) { + public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, Connection $database, EntityTypeManagerInterface $entity_type_manager, TimeInterface $datetime, UuidInterface $uuid) { parent::__construct('Plugin/VoteResultFunction', $namespaces, $module_handler, VoteResultFunctionInterface::class, VoteResultFunction::class, 'Drupal\votingapi\Annotation\VoteResultFunction'); $this->database = $database; $this->entityTypeManager = $entity_type_manager; $this->datetime = $datetime; + $this->uuid = $uuid; $this->alterInfo('vote_result_info'); $this->setCacheBackend($cache_backend, 'vote_result_plugins'); } @@ -167,6 +176,7 @@ class VoteResultFunctionManager extends DefaultPluginManager implements VoteResu $plugin = $this->createInstance($plugin_id); $vote_results[] = [ 'entity_id' => $entity_id, + 'uuid' => $this->uuid->generate(), 'entity_type' => $entity_type_id, 'type' => $vote_type, 'function' => $plugin_id, diff --git a/votingapi.install b/votingapi.install index b27ebd6443308e9f757f14dc72e1d515d5625e67..e2915c6f5f850abae35155591db80f05d001ba20 100644 --- a/votingapi.install +++ b/votingapi.install @@ -11,3 +11,16 @@ function votingapi_update_last_removed() { return 8304; } + +/** + * Install the definition of 'uuid' field Vote Result entity. + */ +function votingapi_update_10400(&$sandbox) { + $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + if ($vote_result_fields = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('vote_result')) { + /** @var \Drupal\Core\Field\BaseFieldDefinition $function_field_definition */ + $function_field_definition = $vote_result_fields['uuid']; + $function_field_definition->getSchema(); + $definition_update_manager->installFieldStorageDefinition('uuid', 'vote_result', 'votingapi', $function_field_definition); + } +} diff --git a/votingapi.post_update.php b/votingapi.post_update.php new file mode 100644 index 0000000000000000000000000000000000000000..6795717088ecd68a9276e1399a68c9cb4a2d65ef --- /dev/null +++ b/votingapi.post_update.php @@ -0,0 +1,28 @@ +<?php + +/** + * @file + * Post update functions for votingapi. + */ + +/** + * Generate uuids for existing vote_result entities with no uuids. + */ +function votingapi_post_update_generate_vote_result_uuids(&$sandbox) { + $table_name = 'votingapi_result'; + $connection = \Drupal::service('database'); + $rows = $connection->select($table_name, 'votingapi_result') + ->fields('votingapi_result', ['id']) + ->isNull('uuid') + ->execute() + ->fetchAll(); + + $uuidGenerator = \Drupal::service('uuid'); + + foreach ($rows as $row) { + $connection->update($table_name) + ->fields(['uuid' => $uuidGenerator->generate()]) + ->condition('id', $row->id) + ->execute(); + } +}