You are here

function drush_rules_list in Rules 8.3

Same name and namespace in other branches
  1. 7.2 rules.drush.inc \drush_rules_list()

Gets a list of all rules.

File

./rules.drush.inc, line 160
Rules module integration with Drush 8 and earlier.

Code

function drush_rules_list() {

  // Type is 'rule', or 'component'. Any other value (or no value) will
  // list both Reaction Rules and Rules Components.
  switch (drush_get_option('type')) {
    case 'rule':
      $types = [
        'reaction',
      ];
      break;
    case 'component':
      $types = [
        'component',
      ];
      break;
    default:
      $types = [
        'reaction',
        'component',
      ];
      break;
  }

  // Loop over type option.
  $rows = [];
  foreach ($types as $type) {
    $rules = \Drupal::configFactory()
      ->listAll('rules.' . $type);
    $event_manager = \Drupal::service('plugin.manager.rules_event');

    // Loop over configuration entities for this $type.
    foreach ($rules as $config) {
      $rule = \Drupal::configFactory()
        ->get($config);
      if (!empty($rule
        ->get('id')) && !empty($rule
        ->get('label'))) {
        $events = [];
        $active = '';

        // Components don't have events and can't be enabled/disabled.
        if ($type == 'reaction') {
          foreach ($rule
            ->get('events') as $event) {
            $plugin = $event_manager
              ->getDefinition($event['event_name']);
            $events[] = (string) $plugin['label'];
          }
          $active = $rule
            ->get('status') ? dt('Enabled') : dt('Disabled');
        }
        $rows[(string) $rule
          ->get('id')] = [
          'rule' => (string) $rule
            ->get('id'),
          'label' => (string) $rule
            ->get('label'),
          'event' => implode(', ', $events),
          'active' => (string) $active,
        ];
      }
    }
  }
  return $rows;
}