Skip to content

Commit

Permalink
Merge pull request #41 from axelerant-hardik/DDST-82
Browse files Browse the repository at this point in the history
DDST-82: Drush command to list missing IDs for identifier
  • Loading branch information
nchiasson-dgi authored May 22, 2024
2 parents e13e957 + 0e45194 commit 14287ee
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
8 changes: 8 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
106 changes: 106 additions & 0 deletions src/Commands/PrintMissingEntityIds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Drupal\dgi_actions\Commands;

use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\CommandError;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\dgi_actions\Utility\IdentifierUtils;
use Drush\Commands\DrushCommands;

/**
* Drush commands for generating identifiers for existing objects.
*/
class PrintMissingEntityIds extends DrushCommands {

use DependencySerializationTrait;

/**
* The Drupal entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;

/**
* Identifier utils service.
*
* @var \Drupal\dgi_actions\Utility\IdentifierUtils
*/
protected IdentifierUtils $identifierUtils;

/**
* Handle Drush commands.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The Drupal entity type manager.
* @param \Drupal\dgi_actions\Utility\IdentifierUtils $identifier_utils
* The identifier utils used to retrieve a DGI Actions Identifier.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, IdentifierUtils $identifier_utils) {
parent::__construct();

$this->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']]));
}
}

}

0 comments on commit 14287ee

Please sign in to comment.