Add command for updating old tag names
This commit is contained in:
parent
bea6553cd8
commit
4ac087b88c
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -2,7 +2,8 @@
|
||||||
!*/
|
!*/
|
||||||
!/.gitignore
|
!/.gitignore
|
||||||
!/scripts/**
|
!/scripts/**
|
||||||
|
!/web/modules/custom/**
|
||||||
!/web/sites/default/environments/settings.*.php
|
!/web/sites/default/environments/settings.*.php
|
||||||
!/web/sites/default/settings.php
|
!/web/sites/default/settings.php
|
||||||
!/web/themes/custom/**
|
!/web/themes/custom/**
|
||||||
/web/themes/custom/*/dist/
|
/web/themes/custom/*/dist/
|
||||||
|
|
5
web/modules/custom/custom/custom.info.yml
Normal file
5
web/modules/custom/custom/custom.info.yml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
name: Oliver Davies custom module
|
||||||
|
type: module
|
||||||
|
core: 8.x
|
||||||
|
core_version_requirements: ^8 || ^9
|
||||||
|
package: Custom
|
5
web/modules/custom/custom/drush.services.yml
Normal file
5
web/modules/custom/custom/drush.services.yml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
services:
|
||||||
|
Drupal\custom\Command\FormatTagNamesCommand:
|
||||||
|
autowire: true
|
||||||
|
tags:
|
||||||
|
- { name: drush.command }
|
136
web/modules/custom/custom/src/Command/FormatTagNamesCommand.php
Normal file
136
web/modules/custom/custom/src/Command/FormatTagNamesCommand.php
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Drupal\custom\Command;
|
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
|
use Drush\Commands\DrushCommands;
|
||||||
|
|
||||||
|
final class FormatTagNamesCommand extends DrushCommands {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The taxonomy term storage.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||||
|
*/
|
||||||
|
private $termStorage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A lookup table for new name overrides.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* An associative array, keyed by the original tag name. The value is either
|
||||||
|
* an overridden tag name or FALSE if the tag name is not to be changed.
|
||||||
|
*/
|
||||||
|
static $tagNames = [
|
||||||
|
'accessible-bristol' => 'Accessible Bristol',
|
||||||
|
'admin:hover' => FALSE,
|
||||||
|
'aria' => 'ARIA',
|
||||||
|
'cck' => 'CCK',
|
||||||
|
'centos' => 'CentOS',
|
||||||
|
'css' => 'CSS',
|
||||||
|
'dcbristol' => FALSE,
|
||||||
|
'ddev' => 'DDEV',
|
||||||
|
'drupal-association' => 'Drupal Association',
|
||||||
|
'drupal-bristol' => 'Drupal Bristol',
|
||||||
|
'drupal-commerce' => 'Drupal Commerce',
|
||||||
|
'drupal-planet' => 'Drupal Planet',
|
||||||
|
'drupal-vm' => 'Drupal VM',
|
||||||
|
'drupal-vm-generator' => 'Drupal VM Generator',
|
||||||
|
'drupalcamp' => 'DrupalCamp',
|
||||||
|
'drupalcamp-bristol' => 'DrupalCamp Bristol',
|
||||||
|
'drupalcamp-london' => 'DrupalCamp London',
|
||||||
|
'drupalcamp-north' => 'DrupalCamp North',
|
||||||
|
'drupalcon' => 'DrupalCon',
|
||||||
|
'entity-api' => 'Entity API',
|
||||||
|
'fancy-slide' => 'Fancy Slide',
|
||||||
|
'field-collection' => 'Field Collection',
|
||||||
|
'filefield' => 'FileField',
|
||||||
|
'form-api' => 'Form API',
|
||||||
|
'git-flow' => 'Git Flow',
|
||||||
|
'github' => 'GitHub',
|
||||||
|
'illuminate-collections' => 'Illuminate Collections',
|
||||||
|
'image-caption' => 'Image Caption',
|
||||||
|
'imagecache' => 'ImageCache',
|
||||||
|
'imagefield' => 'ImageField',
|
||||||
|
'imagefield-import' => 'ImageField Import',
|
||||||
|
'javascript' => 'JavaScript',
|
||||||
|
'laravel-collections' => 'Laravel Collections',
|
||||||
|
'laravel-mix' => 'Laravel Mix',
|
||||||
|
'linux-journal' => 'Linux Journal',
|
||||||
|
'mac-os-x' => 'macOS',
|
||||||
|
'mamp' => 'MAMP',
|
||||||
|
'mod_rewrite' => FALSE,
|
||||||
|
'npm' => FALSE,
|
||||||
|
'oliverdavies.co.uk' => FALSE,
|
||||||
|
'php' => 'PHP',
|
||||||
|
'php-south-wales' => 'PHP South Wales',
|
||||||
|
'phpstorm' => 'PhpStorm',
|
||||||
|
'phpsw' => 'PHPSW',
|
||||||
|
'phpunit' => 'PHPUnit',
|
||||||
|
'postcss' => 'PostCSS',
|
||||||
|
'psr' => 'PSR',
|
||||||
|
'regular-expression' => 'Regular expressions',
|
||||||
|
'sequel-pro' => 'Sequel Pro',
|
||||||
|
'settings.php' => FALSE,
|
||||||
|
'sql' => 'SQL',
|
||||||
|
'ssh' => 'SSH',
|
||||||
|
'sublime-text' => 'Sublime Text',
|
||||||
|
'svn' => 'SVN',
|
||||||
|
'swdug' => 'SWDUG',
|
||||||
|
'symfonylive' => 'SymfonyLive',
|
||||||
|
'tailwind-css' => 'Tailwind CSS',
|
||||||
|
'tdd' => 'TDD',
|
||||||
|
'views-attach' => 'Views Attach',
|
||||||
|
'virtualbox' => 'VirtualBox',
|
||||||
|
'vuejs' => 'VueJS',
|
||||||
|
'virtualhostx' => 'VirtualHostX',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->termStorage = $entityTypeManager->getStorage('taxonomy_term');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drush command for updating legacy tag names.
|
||||||
|
*
|
||||||
|
* @command opdavies:update-tag-names
|
||||||
|
*/
|
||||||
|
public function updateTagNames(): void {
|
||||||
|
foreach ($this->getTags() as $tag) {
|
||||||
|
$name = $tag->label();
|
||||||
|
$newName = $this->getNewTagName($name);
|
||||||
|
|
||||||
|
if ($newName === NULL) {
|
||||||
|
$this->writeln(sprintf('Skipping %s.', $name));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->writeln(sprintf('Updating %s to %s.', $name, $newName));
|
||||||
|
$tag->name = $newName;
|
||||||
|
$tag->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTags(): array {
|
||||||
|
return $this->termStorage->loadByProperties([
|
||||||
|
'vid' => 'tags',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getNewTagName(string $tagName): ?string {
|
||||||
|
if (!array_key_exists($tagName, static::$tagNames)) {
|
||||||
|
return str_replace('-', ' ', ucfirst($tagName));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (static::$tagNames[$tagName] === FALSE) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return static::$tagNames[$tagName];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue