diff --git a/pathauto.drush.inc b/pathauto.drush.inc index e717143..2d0d49c 100644 --- a/pathauto.drush.inc +++ b/pathauto.drush.inc @@ -1,124 +1,130 @@ dt('Generate aliases for all unaliased entities of a specific type, or all possible entities.'), 'arguments' => [ - 'type' => dt('The particular type to process. Omit this argument to choose from available types.') + 'type' => dt('The particular type to process. Omit this argument to choose from available types.'), + 'action' => dt('The URL aliases to generate. Omit this argument to choose from available actions.'), ], - 'aliases' => ['pag'] + 'aliases' => ['pag'], ]; $items['pathauto-aliases-delete'] = [ 'description' => dt('Delete aliases for all aliased entities of a specific type, or all possible entities.'), - 'core' => ['8+'], 'arguments' => [ - 'type' => dt('The particular type to process. Omit this argument to choose from available types.') + 'type' => dt('The particular type to process. Omit this argument to choose from available types.'), + 'action' => dt('The URL aliases to delete., Omit this argument to choose from available actions.'), ], - 'aliases' => ['pad'] + 'aliases' => ['pad'], ]; return $items; } /** - * Command callback for 'drush pathauto-alias-generate' + * Command callback for 'drush pathauto-alias-generate'. */ -function drush_pathauto_aliases_generate($type = NULL) { - $types = _drush_pathauto_aliases_get_batch_types(); +function drush_pathauto_aliases_generate($type = NULL, $action = NULL) { + /** @var \Drupal\pathauto\Cli\AliasGenerateAction $alias_generate_action */ + $alias_generate_action = \Drupal::service('pathauto.cli.alias_generate'); + + $types = $alias_generate_action->getAliasTypeIds(); + $actions = $alias_generate_action->getActions(); - // Check if the provided type ($type) is a valid bulk generate type. + // Check of the provided alias type id is valid. if ($type) { - if ($type !== 'all' && !isset($types[$type])) { - return drush_log(dt('\'!type\' type is not a valid type for bulk generation.', [ - '!type' => $type + if ($type !== AliasCliActionInterface::ALIAS_TYPE_ID_ALL && !isset($types[$type])) { + return drush_log(dt('\'!type\' type is not a valid alias type for bulk generation.', [ + '!type' => $type, + ]), LogLevel::ERROR); + } + } + else { + $type = drush_choice($types, 'Choose the alias type for which to generate URL aliases.', '!key'); + } + + if (!$type) { + return; + } + + // Check if the provided action is valid. + if ($action) { + if ($action !== AliasGenerateAction::ACTION_ALL && !isset($actions[$action])) { + return drush_log(dt('\'!action\' action is not a valid action for bulk generation.', [ + '!action' => $action, ]), LogLevel::ERROR); } } else { - $type = drush_choice(['all' => 'all'] + $types, 'Enter a number to choose which type of alias to bulk generate.', '!key'); + $action = drush_choice($actions, 'Choose the action to perform.', '!key'); } - if ($type !== FALSE) { - $batch = [ - 'title' => dt('Bulk updating URL aliases'), - 'operations' => [ - ['Drupal\pathauto\Form\PathautoBulkUpdateForm::batchStart', []], - ['Drupal\pathauto\Form\PathautoBulkUpdateForm::batchProcess', [$type]] - ], - 'finished' => 'Drupal\pathauto\Form\PathautoBulkUpdateForm::batchFinished' - ]; - - batch_set($batch); - $batch =& batch_get(); - $batch['progressive'] = FALSE; - - // Process the batch - drush_backend_batch_process(); + if (!$action) { + return; + } + + if ($type) { + $alias_generate_action->execute($type, $action); } } /** - * Command callback for 'drush pathauto-alias-delete' + * Command callback for 'drush pathauto-alias-delete'. */ -function drush_pathauto_aliases_delete($type = NULL) { - $types = _drush_pathauto_aliases_get_batch_types(); +function drush_pathauto_aliases_delete($type = NULL, $action = NULL) { + /** @var \Drupal\pathauto\Cli\AliasCliActionInterface $alias_delete_action */ + $alias_delete_action = \Drupal::service('pathauto.cli.alias_delete'); + + $types = $alias_delete_action->getAliasTypeIds(); + $actions = $alias_delete_action->getActions(); - // Check if the provided type ($type) is a valid bulk generate type. + // Check of the provided alias type id is valid. if ($type) { - if ($type !== 'all' && !isset($types[$type])) { - return drush_log(dt('\'!type\' type is not a valid type for bulk deleting.', [ - '!type' => $type + if ($type !== AliasCliActionInterface::ALIAS_TYPE_ID_ALL && !isset($types[$type])) { + return drush_log(dt('\'!type\' type is not a valid alias type for bulk generation.', [ + '!type' => $type, ]), LogLevel::ERROR); } } else { - $type = drush_choice(['all' => 'all'] + $types, 'Enter a number to choose which type of alias to bulk delete.', '!key'); + $type = drush_choice($types, 'Choose the alias type for which to delete URL aliases.', '!key'); } - if ($type !== FALSE) { - /** @var \Drupal\pathauto\AliasStorageHelperInterface $storage_helper */ - $storage_helper = \Drupal::service('pathauto.alias_storage_helper'); - if ($type === 'all') { - $storage_helper->deleteAll(); + if (!$type) { + return; + } - return drush_log(dt('All of your path aliases have been deleted.'), LogLevel::NOTICE); - } - else { - /** @var AliasTypeManager $alias_type_manager */ - $alias_type_manager = \Drupal::service('plugin.manager.alias_type'); - /** @var AliasTypeInterface $alias_type */ - $alias_type = $alias_type_manager->createInstance($type); - $storage_helper->deleteBySourcePrefix((string) $alias_type->getSourcePrefix()); - - return drush_log(dt('All of your path aliases for type \'type\' have been deleted.', [ - '!type' => $type - ]), LogLevel::NOTICE); + // Check of the provided action is valid. + if ($action) { + if ($action !== AliasDeleteAction::ACTION_ALL && !isset($actions[$action])) { + return drush_log(dt('\'!action\' action is not a valid action for bulk deletion.', [ + '!action' => $action, + ]), LogLevel::ERROR); } } -} + else { + $action = drush_choice($actions, 'Choose the action to perform.', '!key'); + } -/** - * @return array - */ -function _drush_pathauto_aliases_get_batch_types() { - /** @var AliasTypeManager $alias_type_manager */ - $alias_type_manager = \Drupal::service('plugin.manager.alias_type'); - $types = []; - foreach ($alias_type_manager->getVisibleDefinitions() as $id => $definition) { - /** @var AliasTypeInterface $alias_type */ - $alias_type = $alias_type_manager->createInstance($id); - if ($alias_type instanceof \Drupal\pathauto\AliasTypeBatchUpdateInterface) { - $types[$alias_type->getPluginId()] = $alias_type->getLabel(); - } + if (!$action) { + return; } - return $types; + if ($type) { + $alias_delete_action->execute($type, $action); + } } diff --git a/pathauto.services.yml b/pathauto.services.yml index 3ce704e..bc28304 100644 --- a/pathauto.services.yml +++ b/pathauto.services.yml @@ -24,3 +24,9 @@ services: arguments: ['@entity_field.manager', '@plugin.manager.alias_type'] tags: - { name: event_subscriber } + pathauto.cli.alias_generate: + class: Drupal\pathauto\Cli\AliasGenerateAction + arguments: ['@config.factory', '@plugin.manager.alias_type'] + pathauto.cli.alias_delete: + class: Drupal\pathauto\Cli\AliasDeleteAction + arguments: ['@config.factory', '@plugin.manager.alias_type', '@pathauto.alias_storage_helper'] diff --git a/src/Cli/AbstractAliasCliAction.php b/src/Cli/AbstractAliasCliAction.php new file mode 100644 index 0000000..77c4667 --- /dev/null +++ b/src/Cli/AbstractAliasCliAction.php @@ -0,0 +1,67 @@ +configFactory = $configFactory; + $this->aliasTypeManager = $aliasTypeManager; + } + + /** + * {@inheritdoc} + */ + public function getAliasTypeIds() { + $types = [ + self::ALIAS_TYPE_ID_ALL => $this->t('All'), + ]; + foreach ($this->aliasTypeManager->getVisibleDefinitions() as $id => $definition) { + /** @var \Drupal\pathauto\AliasTypeInterface $aliasType */ + $aliasType = $this->aliasTypeManager->createInstance($id); + if ($aliasType instanceof AliasTypeBatchUpdateInterface) { + $types[$aliasType->getPluginId()] = $aliasType->getLabel(); + } + } + + return $types; + } + +} diff --git a/src/Cli/AliasCliActionInterface.php b/src/Cli/AliasCliActionInterface.php new file mode 100644 index 0000000..d3a2f75 --- /dev/null +++ b/src/Cli/AliasCliActionInterface.php @@ -0,0 +1,40 @@ +aliasStorageHelper = $aliasStorageHelper; + } + + /** + * {@inheritdoc} + */ + public function execute($aliasTypeId, $action = self::ACTION_KEEP_CUSTOM) { + $deleteAll = ($aliasTypeId === self::ALIAS_TYPE_ID_ALL); + + if ($action === self::ACTION_KEEP_CUSTOM) { + $batch = [ + 'title' => $this->t('Bulk deleting URL aliases'), + 'operations' => [ + [ + sprintf('%s::batchStart', PathautoAdminDelete::class), + [ + $deleteAll + ], + ], + ], + 'finished' => sprintf('%s::batchFinished', PathautoAdminDelete::class), + ]; + + if ($deleteAll) { + foreach ($this->getAliasTypeIds() as $id => $label) { + $batch['operations'][] = [ + sprintf('%s::batchProcess', PathautoAdminDelete::class), + [ + $id + ], + ]; + } + } + else { + $batch['operations'][] = [ + sprintf('%s::batchProcess', PathautoAdminDelete::class), + [ + $aliasTypeId + ], + ]; + } + + $batch['progressive'] = FALSE; + batch_set($batch); + + // Process the batch. + drush_bachend_batch_process(); + } + elseif ($deleteAll) { + $this->aliasStorageHelper->deleteAll(); + } + else { + /** @var \Drupal\pathauto\AliasTypeInterface $aliasTypeId */ + $aliasTypeId = $this->aliasTypeManager->createInstance($aliasTypeId); + $this->aliasStorageHelper->deleteBySourcePrefix((string) $aliasTypeId->getSourcePrefix()); + } + } + + /** + * {@inheritdoc} + */ + public function getActions() { + return[ + self::ACTION_ALL => $this->t('All of your path aliases will be deleted.'), + self::ACTION_KEEP_CUSTOM => $this->t('Only delete automatically generated aliases.') + ]; + } +} diff --git a/src/Cli/AliasGenerateAction.php b/src/Cli/AliasGenerateAction.php new file mode 100644 index 0000000..7ee36b5 --- /dev/null +++ b/src/Cli/AliasGenerateAction.php @@ -0,0 +1,66 @@ + $this->t('Bulk updating URL aliases'), + 'operations' => [ + [ + sprintf('%s::batchStart', PathautoBulkUpdateForm::class), + [], + ], + [ + sprintf('%s::batchProcess', PathautoBulkUpdateForm::class), + [ + $aliasTypeId, + $action, + ], + ], + ], + 'finished' => sprintf('%s::batchFinished', PathautoBulkUpdateForm::class), + 'progressive' => FALSE, + ]; + + $batch['progressive'] = FALSE; + batch_set($batch); + + // Process the batch. + drush_backend_batch_process(); + } + + /** + * {@inheritdoc} + */ + public function getActions() { + $actions = [ + self::ACTION_CREATE => $this->t('Generate a URL alias for un-aliased paths only.'), + ]; + + $config = $this->configFactory->get('pathauto.settings'); + if ($config->get('update_action') !== PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW) { + $actions[self::ACTION_UPDATE] = $this->t('Update the URL alias for paths having an old URL alias'); + $actions[self::ACTION_ALL] = $this->t('Regenerate URL aliases for all paths'); + } + + return $actions; + } + +}