You are here

public function CliCommands::create in Site Alert 8

Creates a new site alert.

Parameters

string $label: The label of the site alert.

string $message: The message to put in the site alert.

array $options: Array of optional human readable values to set on the site alert, passed on the command line. The following values can be set:

  • start: The start date, in ISO 8601 format.
  • end: The end date, in ISO 8601 format.
  • active: Optional boolean value indicating if the created alert will be activated. If omitted or set to TRUE, the alert will be active. If set to FALSE it will be inactive.
  • severity: The severity level, can be 'low', 'medium' or 'high'. If omitted or any other value this will default to 'medium'.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException Thrown when the Site Alert entity definition is invalid.

\Drupal\Component\Plugin\Exception\PluginNotFoundException Thrown when the Site Alert entity type is not defined.

\Drupal\Core\Entity\EntityStorageException Thrown when an error occurs during the saving of the site alert.

\InvalidArgumentException Thrown in case one of the passed in arguments or options is invalid.

Overrides CliCommandsInterface::create

File

src/CliCommands.php, line 37

Class

CliCommands
Service with shared code for CLI tools to perform common tasks.

Namespace

Drupal\site_alert

Code

public function create(string $label, string $message, array $options) : void {
  $this
    ->validateCreateInput($label, $message, $options);

  // Set possible options.
  $start = $options['start'] ?? NULL;
  $end = $options['end'] ?? NULL;
  $active = $options['active'] ?? NULL !== FALSE;
  $severity = $this
    ->normalizeSeverity($options['severity'] ?? 'medium');
  $storage = $this->entityTypeManager
    ->getStorage('site_alert');
  $entity_values = [
    'active' => $active,
    'label' => $label,
    'severity' => $severity,
    'message' => $message,
  ];
  if (!empty($start) || !empty($end)) {
    $entity_values['scheduling'] = [
      'value' => $start,
      'end_value' => $end,
    ];
  }
  $site_alert = $storage
    ->create($entity_values);
  $storage
    ->save($site_alert);
}