You are here

DefaultContentCommands.php in Default Content for D8 2.0.x

Same filename and directory in other branches
  1. 8 src/Commands/DefaultContentCommands.php

File

src/Commands/DefaultContentCommands.php
View source
<?php

namespace Drupal\default_content\Commands;

use Drupal\default_content\ExporterInterface;
use Drush\Commands\DrushCommands;

/**
 * Class DefaultContentCommands.
 *
 * @package Drupal\default_content
 */
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 (must end with .yml) 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, $options['file']);
    if (!$options['file']) {
      $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)
        ->accessCheck(FALSE)
        ->execute();
    }
    else {
      $entities = [
        $entity_id,
      ];
    }

    // @todo Add paging.
    foreach ($entities as $entity_id) {
      $this->defaultContentExporter
        ->exportContentWithReferences($entity_type_id, $entity_id, $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) {
    $module_folder = \Drupal::moduleHandler()
      ->getModule($module)
      ->getPath() . '/content';
    $this->defaultContentExporter
      ->exportModuleContent($module, $module_folder);
  }

}

Classes

Namesort descending Description
DefaultContentCommands Class DefaultContentCommands.