You are here

class DefaultContentCommands in Default Content for D8 8

Same name and namespace in other branches
  1. 2.0.x src/Commands/DefaultContentCommands.php \Drupal\default_content\Commands\DefaultContentCommands

Class DefaultContentCommands.

@package Drupal\default_content

Hierarchy

Expanded class hierarchy of DefaultContentCommands

1 string reference to 'DefaultContentCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses DefaultContentCommands
default_content.commands in ./drush.services.yml
\Drupal\default_content\Commands\DefaultContentCommands

File

src/Commands/DefaultContentCommands.php, line 13

Namespace

Drupal\default_content\Commands
View source
class DefaultContentCommands extends DrushCommands {

  /**
   * The default content exporter.
   *
   * @var \Drupal\default_content\ExporterInterface
   */
  protected $defaultContentExporter;

  /**
   * SimplesitemapController constructor.
   *
   * @param \Drupal\default_content\ExporterInterface $default_content_exporter
   *   The default content exporter.
   */
  public function __construct(ExporterInterface $default_content_exporter) {
    $this->defaultContentExporter = $default_content_exporter;
  }

  /**
   * Exports a single entity.
   *
   * @param string $entity_type_id
   *   The entity type to export.
   * @param int $entity_id
   *   The ID of the entity to export.
   *
   * @command default-content:export
   * @option file Write out the exported content to a file instead of stdout.
   * @aliases dce
   */
  public function contentExport($entity_type_id, $entity_id, $options = [
    'file' => NULL,
  ]) {
    $export = $this->defaultContentExporter
      ->exportContent($entity_type_id, $entity_id);
    if ($file = $options['file']) {
      file_put_contents($file, $export);
    }
    else {
      $this
        ->output()
        ->write($export);
    }
  }

  /**
   * Exports an entity and all its referenced entities.
   *
   * @param string $entity_type_id
   *   The entity type to export.
   * @param int $entity_id
   *   The ID of the entity to export.
   *
   * @command default-content:export-references
   * @option folder Folder to export to, entities are grouped by entity type into directories.
   * @aliases dcer
   */
  public function contentExportReferences($entity_type_id, $entity_id = NULL, $options = [
    'folder' => NULL,
  ]) {
    $folder = $options['folder'];
    if (is_null($entity_id)) {
      $entities = \Drupal::entityQuery($entity_type_id)
        ->execute();
    }
    else {
      $entities = [
        $entity_id,
      ];
    }

    // @todo Add paging.
    foreach ($entities as $entity_id) {
      $serialized_by_type = $this->defaultContentExporter
        ->exportContentWithReferences($entity_type_id, $entity_id);
      $this->defaultContentExporter
        ->writeDefaultContent($serialized_by_type, $folder);
    }
  }

  /**
   * Exports all the content defined in a module info file.
   *
   * @param string $module
   *   The name of the module.
   *
   * @command default-content:export-module
   * @aliases dcem
   */
  public function contentExportModule($module) {
    $serialized_by_type = $this->defaultContentExporter
      ->exportModuleContent($module);
    $module_folder = \Drupal::moduleHandler()
      ->getModule($module)
      ->getPath() . '/content';
    $this->defaultContentExporter
      ->writeDefaultContent($serialized_by_type, $module_folder);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultContentCommands::$defaultContentExporter protected property The default content exporter.
DefaultContentCommands::contentExport public function Exports a single entity.
DefaultContentCommands::contentExportModule public function Exports all the content defined in a module info file.
DefaultContentCommands::contentExportReferences public function Exports an entity and all its referenced entities.
DefaultContentCommands::__construct public function SimplesitemapController constructor.