diff --git a/pathauto.drush.inc b/pathauto.drush.inc new file mode 100644 index 0000000..a52cf54 --- /dev/null +++ b/pathauto.drush.inc @@ -0,0 +1,125 @@ + dt('Generate aliases for all unaliased 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.') + ], + '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.') + ], + 'aliases' => ['pad'] + ]; + + return $items; +} + +/** + * Command callback for 'drush pathauto-alias-generate' + */ +function drush_pathauto_aliases_generate($type = NULL) { + $types = _drush_pathauto_aliases_get_batch_types(); + + // Check if the provided type ($type) is a valid bulk generate type. + if ($type) { + if ($type !== 'all' && !isset($types[$type])) { + return drush_log(dt('\'!type\' type is not a valid 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 generate.', '!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(); + } +} + +/** + * Command callback for 'drush pathauto-alias-delete' + */ +function drush_pathauto_aliases_delete($type = NULL) { + $types = _drush_pathauto_aliases_get_batch_types(); + + // Check if the provided type ($type) is a valid bulk generate type. + if ($type) { + if ($type !== 'all' && !isset($types[$type])) { + return drush_log(dt('\'!type\' type is not a valid type for bulk deleting.', [ + '!type' => $type + ]), LogLevel::ERROR); + } + } + else { + $type = drush_choice(['all' => 'all'] + $types, 'Enter a number to choose which type of alias to bulk delete.', '!key'); + } + + if ($type !== FALSE) { + /** @var \Drupal\pathauto\AliasStorageHelperInterface $storage_helper */ + $storage_helper = \Drupal::service('pathauto.alias_storage_helper'); + if ($type === 'all') { + $storage_helper->deleteAll(); + + 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); + } + } +} + +/** + * @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(); + } + } + + return $types; +}