You are here

public function CliCommands::disable in Site Alert 8

Disables site alert(s).

Parameters

string|null $label: The label of a site alert to disable. If omitted, all site alerts will be disabled.

Return value

int The number of site alerts that were disabled.

Throws

\Drupal\Core\Entity\EntityStorageException Thrown when an error occurs while disabling a site alert.

Overrides CliCommandsInterface::disable

File

src/CliCommands.php, line 122

Class

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

Namespace

Drupal\site_alert

Code

public function disable(?string $label = NULL) : int {
  if (empty($label)) {
    $site_alerts = $this->entityTypeManager
      ->getStorage('site_alert')
      ->loadByProperties([
      'active' => 1,
    ]);
  }
  else {
    $site_alerts = $this
      ->getAlertsByLabel($label, TRUE);
    if (empty($site_alerts)) {
      throw new \InvalidArgumentException(sprintf("No active site alerts found with the label '%s'.", $label));
    }
  }
  $count = 0;
  foreach ($site_alerts as $site_alert) {
    $site_alert
      ->set('active', FALSE)
      ->save();
    $count++;
  }
  return $count;
}