From 6ed644c8d340444b3c744fa96d7fca940b9bf075 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 20 May 2020 01:20:22 +0100 Subject: [PATCH] Add custom command for exporting body content Add a custom Drush command that exports the body field values for node and block body fields into a file. This file can then be included within Tailwind's `purge` settings to prevent classes used within the body fields from being purged. References #55 --- .gitignore | 1 + web/modules/custom/custom/drush.services.yml | 6 +++ ...ExportBodyValuesForThemePurgingCommand.php | 49 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 web/modules/custom/custom/drush.services.yml create mode 100644 web/modules/custom/custom/src/Command/ExportBodyValuesForThemePurgingCommand.php diff --git a/.gitignore b/.gitignore index 96eb775..b0748b1 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ !/web/themes/custom/** /.idea/workspace.xml /vendor/ +/web/themes/custom/*/body-field-values.txt /web/themes/custom/*/dist/ diff --git a/web/modules/custom/custom/drush.services.yml b/web/modules/custom/custom/drush.services.yml new file mode 100644 index 0000000..455638b --- /dev/null +++ b/web/modules/custom/custom/drush.services.yml @@ -0,0 +1,6 @@ +services: + Drupal\custom\Command\ExportBodyValuesForThemePurgingCommand: + arguments: ['@database'] + autowire: true + tags: + - { name: drush.command } diff --git a/web/modules/custom/custom/src/Command/ExportBodyValuesForThemePurgingCommand.php b/web/modules/custom/custom/src/Command/ExportBodyValuesForThemePurgingCommand.php new file mode 100644 index 0000000..73c2353 --- /dev/null +++ b/web/modules/custom/custom/src/Command/ExportBodyValuesForThemePurgingCommand.php @@ -0,0 +1,49 @@ +database = $database; + } + + /** + * Drush command to export body field values into a file. + * + * @command opdavies:export-body-values-for-theme-purging + */ + public function handle() { + $values = Collection::make(self::$tableNames) + ->flatMap(fn(string $tableName) => $this->getValuesFromTable($tableName)) + ->implode(PHP_EOL); + + file_put_contents($this->getFilePath(), $values); + } + + private function getFilePath(): string { + return drupal_get_path('theme', 'opdavies') . DIRECTORY_SEPARATOR . $this->filename; + } + + private function getValuesFromTable(string $tableName): array { + return $this->database->select($tableName) + ->fields($tableName, ['body_value']) + ->execute() + ->fetchCol(); + } + +}