You are here

function drush_yaml_content_import_module in YAML Content 8.2

Same name and namespace in other branches
  1. 8 yaml_content.drush.inc \drush_yaml_content_import_module()

Import specified yaml content file(s) from a designated module.

Parameters

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.

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.

File

./yaml_content.drush.inc, line 82
Drush commands for the yaml_content module.

Code

function drush_yaml_content_import_module($module, $file = NULL) {
  $loader = \Drupal::service('yaml_content.content_loader');
  $path = drupal_get_path('module', $module);
  if (!$path) {

    // @todo Handle when the module cannot be found.
  }
  else {
    $path .= '/content';
  }
  $loader
    ->setContentPath($path);

  // Identify files for import.
  if (is_null($file)) {
    $mask = '/.*\\.content\\.yml/';
  }
  else {

    // Scan only for the specific file name if it exists.
    $mask = '/' . $file . '\\.content\\.yml/';
  }
  $files = file_scan_directory($path, $mask, [
    'recurse' => FALSE,
  ]);

  // @todo Verify files before loading for import.
  foreach ($files as $filename => $file) {
    drush_print_r('Importing content: ' . $filename);
    $loader
      ->parseContent($file->filename);
    $loaded = $loader
      ->loadContent();
    drush_print_r('Imported ' . count($loaded) . ' items.');
  }
}