function drush_views_list

8.0.x views.d8.drush.inc drush_views_list()
7.x views.d8.drush.inc drush_views_list()

Callback function for views-list command.

File

commands/core/views.d8.drush.inc, line 191
Drush integration for views.

Code

function drush_views_list() {
  $disabled_views = array();
  $enabled_views = array();

  $format = drush_get_option('format', FALSE);

  $views = Drupal::entityManager()->getStorage('view')->loadMultiple();

  // Get the --name option.
  $name = array_filter(drush_get_option_list('name'));
  $with_name = !empty($name) ? TRUE : FALSE;

  // Get the --tags option.
  $tags = array_filter(drush_get_option_list('tags'));
  $with_tags = !empty($tags) ? TRUE : FALSE;

  // Get the --status option. Store user input appart to reuse it after.
  $status = drush_get_option('status', FALSE);

  // Throw an error if it's an invalid status.
  if ($status && !in_array($status, array('enabled', 'disabled'))) {
    return drush_set_error(dt('Invalid status: @status. Available options are "enabled" or "disabled"', array('@status' => $status)));
  }

  // Setup a row for each view.
  foreach ($views as $view) {
    // If options were specified, check that first mismatch push the loop to the
    // next view.
    if ($with_name && !stristr($view->id(), $name[0])) {
      continue;
    }
    if ($with_tags && !in_array($view->get('tag'), $tags)) {
      continue;
    }

    $status_bool = $status == 'enabled';
    if ($status && ($view->status() !== $status_bool)) {
      continue;
    }

    $row = array(
      'name' => $view->id(),
      'label' => $view->label(),
      'description' => $view->get('description'),
      'status' => $view->status() ? dt('Enabled') : dt('Disabled'),
      'tag' => $view->get('tag'),
    );

    // Place the row in the appropiate array, so we can have disabled views at
    // the bottom.
    if ($view->status()) {
      $enabled_views[] = $row;
    }
    else {
      $disabled_views[] = $row;
    }
  }

  // Sort alphabeticaly.
  asort($disabled_views);
  asort($enabled_views);

  if (count($enabled_views) || count($disabled_views)) {
    $rows = array_merge($enabled_views, $disabled_views);
    return $rows;
  }
  else {
    drush_log(dt('No views found.'));
  }
}