You are here

public function ContentDevelGenerate::validateDrushParams in Devel 4.x

Same name and namespace in other branches
  1. 8.3 devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::validateDrushParams()
  2. 8 devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::validateDrushParams()
  3. 8.2 devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::validateDrushParams()

Responsible for validating Drush params.

Parameters

array $args: The command arguments.

array $options: The commend options.

Return value

array An array of values ready to be used for generateElements().

Overrides DevelGenerateBaseInterface::validateDrushParams

File

devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php, line 517

Class

ContentDevelGenerate
Provides a ContentDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

public function validateDrushParams(array $args, array $options = []) {
  $add_language = StringUtils::csvToArray($options['languages']);

  // Intersect with the enabled languages to make sure the language args
  // passed are actually enabled.
  $valid_languages = array_keys($this->languageManager
    ->getLanguages(LanguageInterface::STATE_ALL));
  $values['add_language'] = array_intersect($add_language, $valid_languages);
  $translate_language = StringUtils::csvToArray($options['translations']);
  $values['translate_language'] = array_intersect($translate_language, $valid_languages);
  $values['add_type_label'] = $options['add-type-label'];
  $values['kill'] = $options['kill'];
  $values['feedback'] = $options['feedback'];
  $values['title_length'] = 6;
  $values['num'] = array_shift($args);
  $values['max_comments'] = array_shift($args);
  $all_types = array_keys(node_type_get_names());
  $default_types = array_intersect([
    'page',
    'article',
  ], $all_types);
  $selected_types = StringUtils::csvToArray($options['bundles'] ?: $default_types);
  if (empty($selected_types)) {
    throw new \Exception(dt('No content types available'));
  }
  $values['node_types'] = array_combine($selected_types, $selected_types);
  $node_types = array_filter($values['node_types']);
  if (!empty($values['kill']) && empty($node_types)) {
    throw new \Exception(dt('To delete content, please provide the content types (--bundles)'));
  }

  // Checks for any missing content types before generating nodes.
  if (array_diff($node_types, $all_types)) {
    throw new \Exception(dt('One or more content types have been entered that don\'t exist on this site'));
  }
  $values['authors'] = is_null($options['authors']) ? [] : explode(',', $options['authors']);
  if ($this
    ->isBatch($values['num'], $values['max_comments'])) {
    $this->drushBatch = TRUE;
    $this
      ->develGenerateContentPreNode($values);
  }
  return $values;
}