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
This commit is contained in:
Oliver Davies 2020-05-20 01:20:22 +01:00
parent f83e04a12b
commit 6ed644c8d3
3 changed files with 56 additions and 0 deletions

1
.gitignore vendored
View file

@ -15,4 +15,5 @@
!/web/themes/custom/**
/.idea/workspace.xml
/vendor/
/web/themes/custom/*/body-field-values.txt
/web/themes/custom/*/dist/

View file

@ -0,0 +1,6 @@
services:
Drupal\custom\Command\ExportBodyValuesForThemePurgingCommand:
arguments: ['@database']
autowire: true
tags:
- { name: drush.command }

View file

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Drupal\custom\Command;
use Drupal\Core\Database\Connection;
use Illuminate\Support\Collection;
final class ExportBodyValuesForThemePurgingCommand {
private static array $tableNames = [
'block_content__body',
'node__body',
];
private string $filename = 'body-field-values.txt';
private Connection $database;
public function __construct(Connection $database) {
$this->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();
}
}