You are here

public function PathautoCommands::validateAliaseTypes in Pathauto 8

@hook validate

Throws

\InvalidArgumentException Thrown when one of the passed arguments is invalid

\Drupal\Component\Plugin\Exception\PluginException Thrown when an alias type can not be instantiated.

File

src/Commands/PathautoCommands.php, line 224

Class

PathautoCommands
Drush commands allowing to perform Pathauto tasks from the command line.

Namespace

Drupal\pathauto\Commands

Code

public function validateAliaseTypes(CommandData $commandData) {
  $input = $commandData
    ->input();

  // Convert the comma-separated list of types to an array with no duplicates.
  $types = explode(',', $input
    ->getArgument('types'));
  $types = array_map('trim', $types);
  sort($types);
  $types = array_unique($types);

  // Set all available types if the user chooses this option.
  if (in_array(static::TYPE_ALL, $types)) {
    $types = $this
      ->getAliasTypes();
  }

  // Check for invalid types.
  $available_types = $this
    ->getAliasTypes();
  $unsupported_types = array_diff($types, $available_types);
  if (!empty($unsupported_types)) {
    $message = dt('Invalid type argument "@invalid_types". Please choose from the following: @valid_types', [
      '@invalid_types' => '"' . implode('", "', $unsupported_types) . '"',
      '@valid_types' => '"' . implode('", "', [
        static::TYPE_ALL,
      ] + $available_types) . '"',
    ]);
    throw new \InvalidArgumentException($message);
  }

  // Pass the array of types to the command, rather than the comma-separated
  // string.
  $input
    ->setArgument('types', $types);
}