public function ViewsCommands::execute

master ViewsCommands.php public ViewsCommands::execute($view, $display = NULL, $view_args = NULL, $options =['count' 0 'show-admin-links' 0])

Execute a view and show a count of the results, or the rendered HTML.

@command views-execute

@option count Display a count of the results instead of each row. @option show-admin-links Show contextual admin links in the rendered markup. @bootstrap DRUSH_BOOTSTRAP_DRUPAL_FULL @usage drush views-execute my_view Show the rendered HTML for the default display for the my_view View. @usage drush views-execute my_view page_1 3 --count Show a count of my_view:page_1 where the first contextual filter value is 3. @usage drush views-execute my_view page_1 3,foo Show the rendered HTML of my_view:page_1 where the first two contextual filter values are 3 and 'foo' respectively. @bootstrap DRUSH_BOOTSTRAP_DRUPAL_FULL @complete \Drush\CommandFiles\core\ViewsCommands::complete @validate-entity-load view views @aliases vex

Parameters

string $view The name of the view to execute.:

string $display The display ID to execute. If none specified, the default display will be used.:

string $view_args A comma delimited list of values, corresponding to contextual filters.:

Return value

string

File

lib/Drush/CommandFiles/core/ViewsCommands.php, line 182

Namespace

Drush\CommandFiles\core

Class

ViewsCommands

Code

public function execute($view, $display = NULL, $view_args = NULL, $options =['count' 0 'show-admin-links' 0]) {

  $view = Views::getView($view);

  // Set the display and execute the view.
  $view->setDisplay($display);
  $view->preExecute(_convert_csv_to_array($view_args));
  $view->execute();

  if (empty($view->result)) {
    $this->logger->log(LogLevel::WARNING, dt('No results returned for this view.'));
    return NULL;
  }
  elseif ($options['count']) {
    drush_backend_set_result(count($view->result));
    drush_print(count($view->result));
    return NULL;
  }
  else {
    // Don't show admin links in markup by default.
    $view->hide_admin_links = !$options['show-admin-links'];
    $build = $view->preview();
    return (string) \Drupal::service('renderer')->renderPlain($build);
  }
}