From 5c3f4f6405972a017b33a51a3f1eaca1ccd3510e Mon Sep 17 00:00:00 2001 From: Ste Vaidis Date: Mon, 16 Dec 2024 14:50:14 +0200 Subject: [PATCH] drush custom command toolbox --- drush-toolbox/drush_toolbox.info.yml | 7 + .../Drush/Commands/DrushToolboxCommands.php | 296 ++++++++++++++++++ 2 files changed, 303 insertions(+) create mode 100644 drush-toolbox/drush_toolbox.info.yml create mode 100644 drush-toolbox/src/Drush/Commands/DrushToolboxCommands.php diff --git a/drush-toolbox/drush_toolbox.info.yml b/drush-toolbox/drush_toolbox.info.yml new file mode 100644 index 0000000..6516091 --- /dev/null +++ b/drush-toolbox/drush_toolbox.info.yml @@ -0,0 +1,7 @@ +name: 'drush-toolbox' +type: module +description: 'Drush commands for mass editing nodes' +package: Custom +core_version_requirement: ^10 +dependencies: + - drupal:path_alias diff --git a/drush-toolbox/src/Drush/Commands/DrushToolboxCommands.php b/drush-toolbox/src/Drush/Commands/DrushToolboxCommands.php new file mode 100644 index 0000000..4104f04 --- /dev/null +++ b/drush-toolbox/src/Drush/Commands/DrushToolboxCommands.php @@ -0,0 +1,296 @@ +select('path_alias', 'pa') + ->fields('pa', ['id']) + ->condition('alias', $pattern . '%', 'LIKE'); + $results = $query->execute()->fetchAll(); + foreach ($results as $result) { + $path_alias = PathAlias::load($result->id); + if ($path_alias) { + $node_array = $path_alias->get('path')->getValue(); + $alias_array = $path_alias->get('alias')->getValue(); + $node = $node_array[0]['value']; + $alias = $alias_array[0]['value']; + $this->output()->writeln(' 🍓 ' . $node . ' ' . $alias); + } + } + } + + /** + * @param string $pattern + * @return void + */ + public function pattern_delete($pattern):void { + $query = Database::getConnection()->select('path_alias', 'pa') + ->fields('pa', ['id']) + ->condition('alias', $pattern . '%', 'LIKE'); + $results = $query->execute()->fetchAll(); + foreach ($results as $result) { + $path_alias = PathAlias::load($result->id); + if ($path_alias) { + $node_array = $path_alias->get('path')->getValue(); + $alias_array = $path_alias->get('alias')->getValue(); + $node = $node_array[0]['value']; + $alias = $alias_array[0]['value']; + $this->output()->writeln(' ❌ ' . $node . ' ' . $alias); + $path_alias->delete(); + } + } + } + + /** + * @param string $node_type + * @return void + */ + function nodetype_show($node_type):void { + $query = Database::getConnection()->select('node_field_data', 'nfd') + ->fields('nfd', ['nid']) + ->condition('nfd.type', $node_type); + $alias_manager = \Drupal::service('path_alias.manager'); + $nids = $query->execute()->fetchCol(); + foreach ($nids as $nid) { + $node = Node::load($nid); + $url_alias = $alias_manager->getAliasByPath('/node/' . $nid); + if ($node) { + $this->output()->writeln(' 🍏 ' . $nid . ' ' . $url_alias); + } + } + } + + /** + * @param string $node_type + * @return void + */ + function nodetype_alias_delete($node_type):void { + $query = Database::getConnection()->select('node_field_data', 'nfd') + ->fields('nfd', ['nid']) + ->condition('nfd.type', $node_type); + /* $alias_manager = \Drupal::service('path_alias.manager'); */ + /* $nids = $query->execute()->fetchCol(); */ + /* foreach ($nids as $nid) { */ + /* $node = Node::load($nid); */ + /* if ($node) { */ + /* $node->save(); */ + /* $url_alias = $alias_manager->getAliasByPath('/node/' . $nid); */ + /* $this->output()->writeln(' 🍏 ' . $nid . ' ' . $url_alias); */ + /* } */ + /* } */ + + $alias_manager = \Drupal::service('path_alias.manager'); + $nids = $query->execute()->fetchCol(); + foreach ($nids as $nid) { + $node = Node::load($nid); + if ($node) { + $path_alias_manager = \Drupal::entityTypeManager()->getStorage('path_alias'); + $alias_objects = $path_alias_manager->loadByProperties([ + 'path' => '/node/' . $nid + ]); + foreach ($alias_objects as $alias_object) { + $alias_object->delete(); + } + $node->save(); + $url_alias = $alias_manager->getAliasByPath('/node/' . $nid); + $this->output()->writeln(' 🍏 ' . $nid . ' ' . $url_alias); + } + } + + + } + + /** + * @param string $node_type + * @return void + */ + function nodetype_save($node_type):void { + $query = Database::getConnection()->select('node_field_data', 'nfd') + ->fields('nfd', ['nid']) + ->condition('nfd.type', $node_type); + + $alias_repository = \Drupal::service('path_alias.repository'); + $alias_generator = \Drupal::service('path_alias.path_processor'); + $alias_manager = \Drupal::service('path_alias.manager'); + + $nids = $query->execute()->fetchCol(); + foreach ($nids as $nid) { + $node = Node::load($nid); + if ($node) { + $node->save(); + $node_path = '/node/' . $nid; + + $alias_storage = \Drupal::entityTypeManager()->getStorage('path_alias'); + $path_alias = $alias_storage->create([ + 'path' => $node_path, + 'alias' => $alias_manager->getAliasByPath($node_path, 'el'), + 'langcode' => $node->language()->getId(), + ]); + $path_alias->save(); + + $url_alias = $alias_manager->getAliasByPath('/node/' . $nid); + $this->output()->writeln(' 🍏 ' . $nid . ' ' . $url_alias); + } + } + } + + + + + + /** + * @param string $node_type + * @param string $value + * @return void + */ + function nodetype_pathauto($node_type, $value):void { + $query = Database::getConnection()->select('node_field_data', 'nfd') + ->fields('nfd', ['nid']) + ->condition('nfd.type', $node_type); + $nids = $query->execute()->fetchCol(); + foreach ($nids as $nid) { + $node = Node::load($nid); + if ($node) { + $node->path = ['pathauto' => $value]; + $node->save(); + $this->output()->writeln(' 🫐 ' . $nid); + } + } + } + + /** + * @param string $pattern + * @return void + */ + public function pattern_el_to_en($pattern):void { + $query = Database::getConnection()->select('path_alias', 'pa') + ->fields('pa', ['id']) + ->condition('alias', $pattern . '%', 'LIKE'); + $results = $query->execute()->fetchAll(); + foreach ($results as $result) { + $path_alias = PathAlias::load($result->id); + if ($path_alias) { + if (preg_match('/^\/node\/(\d+)$/', $path_alias->getPath(), $matches)) { + $node_id = $matches[1]; + $node = Node::load($node_id); + if ($node) { + $node_en = $node->getTranslation('en'); + $path_alias_en = PathAlias::create([ + 'path' => '/node/' . $node_en->id(), + 'alias' => $path_alias->alias, + 'langcode' => 'en', + ]); + $path_alias_en->save(); + $node_en->save(); + $node_array = $path_alias->get('path')->getValue(); + $alias_array = $path_alias->get('alias')->getValue(); + $node = $node_array[0]['value']; + $alias = $alias_array[0]['value']; + $this->output()->writeln(' 🍇 ' . $node . ' ' . $alias); + } + } + } + } + } + + /** + * Command description here. + */ + #[CLI\Command( + name: 'toolbox', + aliases: ['tbox'], + description: 'This command provides various utilities for managing aliases and node types.' + )] + #[CLI\Argument(name: 'pattern', description: '⭐ Pattern to match, used to match aliases or node types.')] + #[CLI\Option(name: 'aliases-show', description: '⭐ Show matched aliases')] + #[CLI\Option(name: 'aliases-delete', description: '⭐ Delete matched aliases')] + #[CLI\Option(name: 'aliases-el-to-en', description: '⭐ Copy Greek to English aliases')] + #[CLI\Option(name: 'nodetype-save', description: '⭐ Save all nodes of matched node type')] + #[CLI\Option(name: 'nodetype-pathauto-enable', description: '⭐ Enable path auto for node type')] + #[CLI\Option(name: 'nodetype-pathauto-disable', description: '⭐ Disable path auto for node type')] + #[CLI\Usage(name: 'toolbox "/articles/" --aliases-show', description: ' ⭐ Show aliases that contains /articles/')] + #[CLI\Usage(name: 'toolbox "/articles/" --aliases-delete', description: ' ⭐ Show aliases that contains /articles/')] + #[CLI\Usage(name: 'toolbox "/articles/" --aliases-el-to-en', description: ' ⭐ Copy Greek aliases that match with /articles/ to English')] + #[CLI\Usage(name: 'toolbox article --nodetype-show', description: ' ⭐ Show aliases from all nodes of type "article"')] + #[CLI\Usage(name: 'toolbox article --nodetype-save', description: ' ⭐ Re-save all node type "article"')] + #[CLI\Usage(name: 'toolbox article --nodetype-alias-delete', description: ' ⭐ Delete all aliases of node type "article"')] + #[CLI\Usage(name: 'toolbox article --nodetype-pathauto-enable', description: ' ⭐ Enable "Generate automatic URL alias" for "article" node type')] + #[CLI\Usage(name: 'toolbox article --nodetype-pathauto-disable', description: ' ⭐ Disable "Generate automatic URL alias" for "article" node type')] + public function commandName($pattern = '', $options = [ + 'pattern-show' => null, + 'pattern-delete' => null, + 'pattern-el-to-en' => null, + 'nodetype-show' => null, + 'nodetype-save' => null, + 'nodetype-alias-delete' => null, + 'nodetype-pathauto-enable' => null, + 'nodetype-pathauto-disable' => null + ]) { + /* $this->output()->writeln($pattern); */ + /* $this->output()->writeln($options); */ + /* if (empty($pattern) && empty($options)) { */ + if ($pattern == '') { + $this->output()->writeln(''); + $this->output()->writeln('⚙️ Command : toolbox'); + $this->output()->writeln(' Alias : tbox'); + $this->output()->writeln(' Description: This command provides various utilities for managing URL aliases'); + $this->output()->writeln(''); + $this->output()->writeln('⚙️ Examples by pattern'); + $this->output()->writeln('tbox "/articles/" --pattern-show ⭐ Show URL aliases that start with the string /articles/'); + $this->output()->writeln('tbox "/articles/" --pattern-delete ⭐ Delete URL aliases that start with the string /articles/'); + $this->output()->writeln('tbox "/articles/" --pattern-el-to-en ⭐ Copy URL aliases from Greek to English that contains the string /articles/'); + $this->output()->writeln(''); + $this->output()->writeln('⚙️ Examples by Node Type'); + $this->output()->writeln('tbox "article" --nodetype-show ⭐ Show URL aliases from all nodes of the node type articles'); + $this->output()->writeln('tbox "article" --nodetype-save ⭐ Re-Create URL aliases for all the nodes of the node type article'); + $this->output()->writeln('tbox "article" --nodetype-alias-delete ⭐ Delete all URL aliases of the node type article'); + $this->output()->writeln('tbox "article" --nodetype-pathauto-enable ⭐ Enable Path Auto for all nodes of the node type article'); + $this->output()->writeln('tbox "article" --nodetype-pathauto-disable ⭐ Disable Path Auro for all nodes of the node the article '); + return; + } + if (!empty($options['pattern-show'])) { + $this->pattern_show($pattern); + } + if (!empty($options['pattern-delete'])) { + $this->pattern_delete($pattern); + } + if (!empty($options['pattern-el-to-en'])) { + $this->pattern_el_to_en($pattern); + } + if (!empty($options['nodetype-show'])) { + $this->nodetype_show($pattern); + } + if (!empty($options['nodetype-alias-delete'])) { + $this->nodetype_alias_delete($pattern); + } + if (!empty($options['nodetype-save'])) { + $this->nodetype_save($pattern); + } + if (!empty($options['nodetype-pathauto-enable'])) { + $this->nodetype_pathauto($pattern, 1); + } + if (!empty($options['nodetype-pathauto-disable'])) { + $this->nodetype_pathauto($pattern, 0); + } + } + +} +