You are here

public function TermDevelGenerate::validateDrushParams in Devel 4.x

Same name and namespace in other branches
  1. 8.3 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::validateDrushParams()
  2. 8 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::validateDrushParams()
  3. 8.2 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::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/TermDevelGenerate.php, line 432

Class

TermDevelGenerate
Provides a TermDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

public function validateDrushParams(array $args, array $options = []) {

  // Get default settings from the annotated command definition.
  $defaultSettings = $this
    ->getDefaultSettings();
  $bundles = StringUtils::csvToarray($options['bundles']);
  if (count($bundles) < 1) {
    throw new \Exception(dt('Please provide a vocabulary machine name (--bundles).'));
  }
  foreach ($bundles as $bundle) {

    // Verify that each bundle is a valid vocabulary id.
    if (!$this->vocabularyStorage
      ->load($bundle)) {
      throw new \Exception(dt('Invalid vocabulary machine name: @name', [
        '@name' => $bundle,
      ]));
    }
  }
  $number = array_shift($args) ?: $defaultSettings['num'];
  if (!$this
    ->isNumber($number)) {
    throw new \Exception(dt('Invalid number of terms: @num', [
      '@num' => $number,
    ]));
  }
  $minimum_depth = $options['min-depth'] ?? $defaultSettings['minimum_depth'];
  $maximum_depth = $options['max-depth'] ?? $defaultSettings['maximum_depth'];
  if ($minimum_depth < 1 || $minimum_depth > 20 || $maximum_depth < 1 || $maximum_depth > 20 || $minimum_depth > $maximum_depth) {
    throw new \Exception(dt('The depth values must be in the range 1 to 20 and min-depth cannot be larger than max-depth (values given: min-depth @min, max-depth @max)', [
      '@min' => $minimum_depth,
      '@max' => $maximum_depth,
    ]));
  }
  $values = [
    'num' => $number,
    'kill' => $options['kill'],
    'title_length' => 12,
    'vids' => $bundles,
    'minimum_depth' => $minimum_depth,
    'maximum_depth' => $maximum_depth,
  ];
  $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);
  return $values;
}