You are here

class LoadHelper in YAML Content 8

A helper class to support the content loading process.

Hierarchy

Expanded class hierarchy of LoadHelper

1 file declares its use of LoadHelper
YamlContentCommands.php in src/Commands/YamlContentCommands.php
1 string reference to 'LoadHelper'
yaml_content.services.yml in ./yaml_content.services.yml
yaml_content.services.yml
1 service uses LoadHelper
yaml_content.load_helper in ./yaml_content.services.yml
Drupal\yaml_content\Service\LoadHelper

File

src/Service/LoadHelper.php, line 13

Namespace

Drupal\yaml_content\Service
View source
class LoadHelper {
  use StringTranslationTrait;

  /**
   * The content loader to use for importing content.
   *
   * @var \Drupal\yaml_content\ContentLoader\ContentLoaderInterface
   */
  protected $loader;

  /**
   * The logging channel for recording import events.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Constructs the load helper service.
   *
   * @param \Drupal\yaml_content\ContentLoader\ContentLoaderInterface $content_loader
   *   The content loader service to use for content imports.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logging channel for recording import events.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   String translation service for message logging.
   */
  public function __construct(ContentLoaderInterface $content_loader, LoggerInterface $logger, TranslationInterface $translation) {
    $this->loader = $content_loader;
    $this->logger = $logger;
    $this
      ->setStringTranslation($translation);
  }

  /**
   * Import specified yaml content file(s).
   *
   * @param string $directory
   *   The directory path containing the yaml content file(s) to be imported.
   * @param string $file
   *   (Optional) The name of a file (without the trailing `.content.yml` to be
   *   imported or an array of files to import. If this argument is not
   *   provided then all files in the directory matching `*.content.yml`
   *   are queued for import.
   */
  public function importDirectory($directory, $file = NULL) {
    $this->loader
      ->setContentPath($directory);

    // Identify files for import.
    $mask = '/' . (isset($file) ? $file : '.*') . '\\.content\\.yml/';
    $files = $this
      ->discoverFiles($directory . '/content', $mask);
    $this
      ->importFiles($files);
  }

  /**
   * Import specified yaml content file(s) from a designated module.
   *
   * @param string $module
   *   The module to look for content files within.
   *
   *   This command assumes files will be contained within a `content/` directory
   *   at the top of the module's main directory. Any files within matching the
   *   pattern `*.content.yml` will then be imported.
   * @param string|string[] $file
   *   (Optional) The name of a file to be imported or an array of files to
   *   import. If this argument is not provided then all files in the directory
   *   matching `*.content.yml` are queued for import.
   */
  public function importModule($module, $file = NULL) {
    $path = drupal_get_path('module', $module);
    $this->loader
      ->setContentPath($path);

    // Identify files for import.
    $mask = '/' . (isset($file) ? $file : '.*') . '\\.content\\.yml/';
    $files = $this
      ->discoverFiles($path . '/content', $mask);
    $this
      ->importFiles($files);
  }

  /**
   * Import specified yaml content file(s) from a designated profile.
   *
   * @param string $profile
   *   The profile to look for content files within.
   *
   *   This command assumes files will be contained within a `content/` directory
   *   at the top of the module's main directory. Any files within matching the
   *   pattern `*.content.yml` will then be imported.
   * @param string|string[] $file
   *   (Optional) The name of a file to be imported or an array of files to
   *   import. If this argument is not provided then all files in the directory
   *   matching `*.content.yml` are queued for import.
   */
  public function importProfile($profile, $file = NULL) {
    $path = drupal_get_path('profile', $profile);
    $this->loader
      ->setContentPath($path);

    // Identify files for import.
    $mask = '/' . (isset($file) ? $file : '.*') . '\\.content\\.yml/';
    $files = $this
      ->discoverFiles($path . '/content', $mask);
    $this
      ->importFiles($files);
  }

  /**
   * Scan and discover content files for import.
   *
   * The scanner assumes all content files will follow the naming convention of
   * '*.content.yml'.
   *
   * @param string $path
   *   The directory path to be scanned for content files.
   * @param string $mask
   *   (Optional) A file name mask to limit matches in scanned files.
   *
   * @return array
   *   An associative array of objects keyed by filename with the following
   *   properties as returned by FileSystemInterface::scanDirectory():
   *
   *   - 'uri'
   *   - 'filename'
   *   - 'name'
   *
   * @see \Drupal\Core\File\FileSystemInterface::scanDirectory()
   */
  public function discoverFiles($path, $mask = '/.*\\.content\\.yml/') {

    // Identify files for import.
    $files = \Drupal::service('file_system')
      ->scanDirectory($path, $mask, [
      'key' => 'filename',
      'recurse' => FALSE,
    ]);

    // Sort the files to ensure consistent sequence during imports.
    ksort($files);
    return $files;
  }

  /**
   * Import content files using a Content Loader.
   *
   * @param array $files
   *   An array of file descriptors as loaded by
   *   FileSystemInterface::scanDirectory() keyed by filename. Each of the
   *   listed files will be imported.
   */
  protected function importFiles(array $files) {

    // @todo Verify files before loading for import.
    foreach ($files as $filename => $file) {

      // Log pre-import notices.
      \Drupal::messenger()
        ->addMessage($this
        ->t('Importing content: %file', [
        '%file' => $filename,
      ]));
      $this->logger
        ->notice('Importing content: %file', [
        '%file' => $filename,
      ]);
      $loaded = $this->loader
        ->loadContent($filename);

      // Log post-import summaries.
      \Drupal::messenger()
        ->addMessage($this
        ->t('Imported %count items from %file', [
        '%count' => count($loaded),
        '%file' => $filename,
      ]));
      $this->logger
        ->notice('Imported %count items from %file', [
        '%count' => count($loaded),
        '%file' => $filename,
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoadHelper::$loader protected property The content loader to use for importing content.
LoadHelper::$logger protected property The logging channel for recording import events.
LoadHelper::discoverFiles public function Scan and discover content files for import.
LoadHelper::importDirectory public function Import specified yaml content file(s).
LoadHelper::importFiles protected function Import content files using a Content Loader.
LoadHelper::importModule public function Import specified yaml content file(s) from a designated module.
LoadHelper::importProfile public function Import specified yaml content file(s) from a designated profile.
LoadHelper::__construct public function Constructs the load helper service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.