function ContentDevelGenerate::validateDrushParams

Same name in other branches
  1. 8.x-1.x devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::validateDrushParams()
  2. 4.x devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::validateDrushParams()

Overrides DevelGenerateBaseInterface::validateDrushParams

File

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

Class

ContentDevelGenerate
Provides a ContentDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

public function validateDrushParams(array $args, array $options = []) : array {
    $add_language = self::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 = self::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['skip_fields'] = is_null($options['skip-fields']) ? [] : self::csvToArray($options['skip-fields']);
    $values['base_fields'] = is_null($options['base-fields']) ? [] : self::csvToArray($options['base-fields']);
    $values['title_length'] = 6;
    $values['num'] = array_shift($args);
    $values['max_comments'] = array_shift($args);
    // Do not use csvToArray for 'authors' because it removes '0' values.
    $values['authors'] = is_null($options['authors']) ? [] : explode(',', $options['authors']);
    $values['roles'] = self::csvToArray($options['roles']);
    $all_types = array_keys(node_type_get_names());
    $default_types = array_intersect([
        'page',
        'article',
    ], $all_types);
    $selected_types = self::csvToArray($options['bundles'] ?: $default_types);
    if ($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']) && $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'));
    }
    if ($this->isBatch($values['num'], $values['max_comments'])) {
        $this->drushBatch = TRUE;
        $this->develGenerateContentPreNode($values);
    }
    return $values;
}