|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\unity_common\Commands; |
| 4 | + |
| 5 | +use Drush\Commands\DrushCommands; |
| 6 | +use Drupal\structure_sync\StructureSyncHelper; |
| 7 | + |
| 8 | +/** |
| 9 | + * Drush custom commands. |
| 10 | + */ |
| 11 | +class UnityDrushCommands extends DrushCommands { |
| 12 | + |
| 13 | + /** |
| 14 | + * Core EntityTypeManager instance. |
| 15 | + * |
| 16 | + * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
| 17 | + */ |
| 18 | + protected $entityTypeManager; |
| 19 | + |
| 20 | + /** |
| 21 | + * Class constructor. |
| 22 | + */ |
| 23 | + public function __construct() { |
| 24 | + parent::__construct(); |
| 25 | + $this->entityTypeManager = \Drupal::entityTypeManager(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Drush command import blocks, taxonomies and menus using structure_sync. |
| 30 | + * |
| 31 | + * @param string $option |
| 32 | + * Argument to select 'safe', 'full' or 'force'. |
| 33 | + * |
| 34 | + * @command import-all-if-installed |
| 35 | + */ |
| 36 | + public function importAllIfInstalled($option = 'safe') { |
| 37 | + // Only import if the structure_sync module is installed. |
| 38 | + if (\Drupal::moduleHandler()->moduleExists('structure_sync')) { |
| 39 | + $config = \Drupal::config('structure_sync.data'); |
| 40 | + // Import blocks if necessary. |
| 41 | + $blocks = $config->get('blocks'); |
| 42 | + if (!empty($blocks)) { |
| 43 | + StructureSyncHelper::importCustomBlocks([ |
| 44 | + 'style' => $option, |
| 45 | + 'drush' => TRUE, |
| 46 | + ]); |
| 47 | + } |
| 48 | + // Import taxonomies if necessary. |
| 49 | + $taxonomies = $config->get('taxonomies'); |
| 50 | + if (!empty($taxonomies)) { |
| 51 | + StructureSyncHelper::importTaxonomies([ |
| 52 | + 'style' => $option, |
| 53 | + 'drush' => TRUE, |
| 54 | + ]); |
| 55 | + } |
| 56 | + // Import menus if necessary. |
| 57 | + $menus = $config->get('menus'); |
| 58 | + if (!empty($menus)) { |
| 59 | + StructureSyncHelper::importMenuLinks([ |
| 60 | + 'style' => $option, |
| 61 | + 'drush' => TRUE, |
| 62 | + ]); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Drush command replace SITE_FOLDER_NAME and STARTERKIT text in a unity site |
| 69 | + * sub-theme. |
| 70 | + * |
| 71 | + * @param string $site_folder_name |
| 72 | + * Argument for user to enter the site directory they wish to target. |
| 73 | + * @param string $starterkit |
| 74 | + * Argument for user to add the new name of the sub-theme, replacing |
| 75 | + * STARTERKIT. |
| 76 | + * |
| 77 | + * @command update-starterkit-text |
| 78 | + * @aliases upst |
| 79 | + * @usage update-starterkit-text --site_folder_name --new_theme_name |
| 80 | + */ |
| 81 | + public function changeText($site_folder_name = '', $starterkit = '') { |
| 82 | + // Get the site folder name entered as the 1st parameter by the user. |
| 83 | + $site_path = getcwd() . '/sites/' . $site_folder_name; |
| 84 | + |
| 85 | + // Rename the STARTERKIT directory to the new sub-theme name entered by the user as the 2nd parameter. |
| 86 | + rename($site_path . '/themes/STARTERKIT', $site_path . '/themes/' . $starterkit); |
| 87 | + $new_theme_path = $site_path . '/themes/' . $starterkit; |
| 88 | + |
| 89 | + // Change the theme file names. |
| 90 | + $theme_file_names = glob($new_theme_path . '/*'); |
| 91 | + foreach ($theme_file_names as $theme_file_name) { |
| 92 | + $new_file_name = str_replace('STARTERKIT', $starterkit, $theme_file_name); |
| 93 | + rename($theme_file_name, $new_file_name); |
| 94 | + } |
| 95 | + |
| 96 | + // Loop through the new theme directories and update any STARTERKIT and SITE_DIRECTORY_TEXT text. |
| 97 | + $site_directories = [ |
| 98 | + glob($new_theme_path . '/config/*'), |
| 99 | + glob($new_theme_path . '/*'), |
| 100 | + ]; |
| 101 | + foreach ($site_directories as $site_directory) { |
| 102 | + foreach ($site_directory as $file) { |
| 103 | + $file_contents = file_get_contents($file); |
| 104 | + $strings = ['STARTERKIT', |
| 105 | + 'SITE_DIRECTORY_NAME', |
| 106 | + ]; |
| 107 | + $string_replacements = [$starterkit, |
| 108 | + $site_folder_name, |
| 109 | + ]; |
| 110 | + $file_contents = str_replace($strings, $string_replacements, $file_contents); |
| 111 | + if (!is_dir($file)) { |
| 112 | + file_put_contents($file, $file_contents); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Remove select content from the site. |
| 120 | + * |
| 121 | + * @command unity-migrate:content-purge |
| 122 | + * |
| 123 | + * @param string $content_type |
| 124 | + * Argument to select content type to purge (machine name). |
| 125 | + * |
| 126 | + * @aliases mig-purge |
| 127 | + * @usage mig-purge <machine name of content type> |
| 128 | + */ |
| 129 | + public function contentPurge($content_type = NULL) { |
| 130 | + if (empty($content_type)) { |
| 131 | + $this->io()->write("Please specify the machine name of a content type to purge", TRUE); |
| 132 | + return; |
| 133 | + } |
| 134 | + // Load all nodes of this content type. |
| 135 | + $storage = $this->entityTypeManager->getStorage('node'); |
| 136 | + $entities = $storage->loadByProperties(["type" => $content_type]); |
| 137 | + |
| 138 | + $rows[] = [ |
| 139 | + 'entity' => 'node', |
| 140 | + 'bundle' => $content_type, |
| 141 | + 'total' => count($entities), |
| 142 | + ]; |
| 143 | + // Show the user a count of nodes. |
| 144 | + $this->io()->table(['Entity', 'Bundle', 'Total'], $rows); |
| 145 | + |
| 146 | + // Ask user to confirm. |
| 147 | + if ($this->io()->confirm("Are you sure you want to delete all $content_type content", TRUE)) { |
| 148 | + $storage->delete($entities); |
| 149 | + $this->io()->write("<comment>$bundle content deleted</comment>", TRUE); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Drush command to disable Fastly logging if Fastly module installed. |
| 155 | + * |
| 156 | + * @command disable-fastly-logging |
| 157 | + */ |
| 158 | + public function disableFastlyLogging() { |
| 159 | + // Only disable logging if the Fastly module is installed. |
| 160 | + if (\Drupal::moduleHandler()->moduleExists('fastly')) { |
| 161 | + \Drupal::configFactory()->getEditable('fastly.settings') |
| 162 | + ->set('logging', FALSE)->save(); |
| 163 | + $this->io()->write("Fastly logging disabled", TRUE); |
| 164 | + } |
| 165 | + else { |
| 166 | + $this->io()->write("Fastly module not installed", TRUE); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Drush command to enable Fastly logging if Fastly module installed. |
| 172 | + * |
| 173 | + * @command enable-fastly-logging |
| 174 | + */ |
| 175 | + public function enableFastlyLogging() { |
| 176 | + // Only enable logging if the Fastly module is installed. |
| 177 | + if (\Drupal::moduleHandler()->moduleExists('fastly')) { |
| 178 | + \Drupal::configFactory()->getEditable('fastly.settings') |
| 179 | + ->set('logging', TRUE)->save(); |
| 180 | + $this->io()->write("Fastly logging enabled", TRUE); |
| 181 | + } |
| 182 | + else { |
| 183 | + $this->io()->write("Fastly module not installed", TRUE); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments