From 0e45194d7397468999dac2302ea1d2cebd1e7080 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Fri, 10 May 2024 16:17:20 +0530 Subject: [PATCH] DDST-82: Drush command to list missing IDs for identifier --- drush.services.yml | 8 ++ src/Commands/PrintMissingEntityIds.php | 106 +++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/Commands/PrintMissingEntityIds.php diff --git a/drush.services.yml b/drush.services.yml index 75e52fb..fe76863 100644 --- a/drush.services.yml +++ b/drush.services.yml @@ -11,6 +11,14 @@ services: ] tags: - { name: drush.command } + dgi_actions.print_missing_identifier_entity_ids: + class: \Drupal\dgi_actions\Commands\PrintMissingEntityIds + arguments: [ + '@entity_type.manager', + '@dgi_actions.utils' + ] + tags: + - { name: drush.command } logger.dgi_actions: parent: logger.channel_base arguments: [ 'dgi_actions' ] diff --git a/src/Commands/PrintMissingEntityIds.php b/src/Commands/PrintMissingEntityIds.php new file mode 100644 index 0000000..3d35359 --- /dev/null +++ b/src/Commands/PrintMissingEntityIds.php @@ -0,0 +1,106 @@ +entityTypeManager = $entity_type_manager; + $this->identifierUtils = $identifier_utils; + } + + /** + * Prints entity IDs with missing identifiers. + * + * @param array $options + * An array containing options passed to the print_ids command containing: + * -identifier_id: The DGI Actions Identifier being targeted for use. + * + * @command dgi_actions:print_ids + * + * @option identifier_id + * A string pointing to the DGI Actions Identifier ID to be used. + * + * @aliases da:print_ids + * + * @usage dgi_actions:print_ids --identifier_id=handle + * Prints entity IDs by searching all entities for the "handle" + * DGI Actions Identifier entity. + */ + public function printIds(array $options = [ + 'identifier_id' => self::REQ, + ]) { + $identifier = $this->entityTypeManager->getStorage('dgiactions_identifier')->load($options['identifier_id']); + + $entity_type = $identifier->get('entity'); + $entity_bundle = $identifier->get('bundle'); + $entity_field = $identifier->get('field'); + + // Get an array of IDs for which this identifier is missing. + $ids = $this->entityTypeManager->getStorage($entity_type)->getQuery()->accessCheck(FALSE) + ->condition('type', $entity_bundle) + ->notExists($entity_field) + ->execute(); + + // If no IDs are returned, we log a message. + if (empty($ids)) { + return $this->logger()->log('notice', dt('No IDs found.')); + } + + return $this->logger()->log('success', dt(implode(',', $ids))); + } + + /** + * Validates the print_ids command. + * + * @hook validate dgi_actions:print_ids + */ + public function printIdsValidate(CommandData $data) { + $options = $data->getArgsAndOptions(); + + if (empty($options['options']['identifier_id'])) { + return new CommandError(dt('An "identifier_id" must be specified.')); + } + $identifiers = $this->identifierUtils->getIdentifiers(); + if (!isset($identifiers[$options['options']['identifier_id']])) { + return new CommandError(dt('The DGI Actions identifier entity (!id) does not exist.', ['!id' => $options['options']['identifier_id']])); + } + } + +}