Skip to content
Snippets Groups Projects

Update Drush commands to support Drush 12+

2 files
+ 44
16
Compare changes
  • Side-by-side
  • Inline
Files
2
<?php
namespace Drupal\default_content\Commands;
namespace Drupal\default_content\Drush\Commands;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\default_content\ExporterInterface;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
/**
* Class DefaultContentCommands.
* Drush 12+ commands for the Default Content module.
*
* @package Drupal\default_content
*/
class DefaultContentCommands extends DrushCommands {
class DefaultContentDrushCommands extends DrushCommands {
/**
* The default content exporter.
*
* @var \Drupal\default_content\ExporterInterface
*/
protected $defaultContentExporter;
protected ExporterInterface $defaultContentExporter;
/**
* SimplesitemapController constructor.
* The module extension list.
*
* @var \Drupal\Core\Extension\ModuleExtensionList
*/
protected ModuleExtensionList $moduleExtensionlist;
/**
* DefaultContentDrushCommands constructor.
*
* @param \Drupal\default_content\ExporterInterface $default_content_exporter
* The default content exporter.
* @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list
* The module extension list.
*/
public function __construct(ExporterInterface $default_content_exporter) {
public function __construct(ExporterInterface $default_content_exporter, ModuleExtensionList $module_extension_list) {
parent::__construct();
$this->defaultContentExporter = $default_content_exporter;
$this->moduleExtensionlist = $module_extension_list;
}
/**
@@ -36,15 +49,21 @@ class DefaultContentCommands extends DrushCommands {
* The entity type to export.
* @param int $entity_id
* The ID of the entity to export.
* @param null[] $options
* Extra options.
*
* @command default-content:export
* @option file Write out the exported content to a file (must end with .yml) instead of stdout.
* @aliases dce
*
* @option file
* Write out the exported content to a file (must end with .yml) instead of
* stdout.
*/
public function contentExport($entity_type_id, $entity_id, $options = ['file' => NULL]) {
#[CLI\Command(name: 'default-content:export', aliases: ['dce'])]
public function contentExport(string $entity_type_id, int $entity_id, array $options = ['file' => NULL]): void {
$export = $this->defaultContentExporter->exportContent($entity_type_id, $entity_id, $options['file']);
if (!$options['file']) {
if (is_null($options['file'])) {
$this->output()->write($export);
}
}
@@ -54,14 +73,20 @@ class DefaultContentCommands extends DrushCommands {
*
* @param string $entity_type_id
* The entity type to export.
* @param int $entity_id
* @param ?int $entity_id
* The ID of the entity to export.
* @param null[] $options
* Extra options.
*
* @command default-content:export-references
* @option folder Folder to export to, entities are grouped by entity type into directories.
* @aliases dcer
*
* @option folder
* Folder to export to, entities are grouped by entity type into
* directories.
*/
public function contentExportReferences($entity_type_id, $entity_id = NULL, $options = ['folder' => NULL]) {
#[CLI\Command(name: 'default-content:export-references', aliases: ['dcer'])]
public function contentExportReferences(string $entity_type_id, int $entity_id = NULL, array $options = ['folder' => NULL]): void {
$folder = $options['folder'];
if (is_null($entity_id)) {
$entities = \Drupal::entityQuery($entity_type_id)->accessCheck(FALSE)->execute();
@@ -84,8 +109,9 @@ class DefaultContentCommands extends DrushCommands {
* @command default-content:export-module
* @aliases dcem
*/
public function contentExportModule($module) {
$module_folder = \Drupal::service('extension.list.module')
#[CLI\Command(name: 'default-content:export-module', aliases: ['dcem'])]
public function contentExportModule(string $module): void {
$module_folder = $this->moduleExtensionlist
->get($module)
->getPath() . '/content';
$this->defaultContentExporter->exportModuleContent($module, $module_folder);
Loading