views.d8.drush.inc

  1. 8.0.x commands/core/views.d8.drush.inc
  2. 7.x commands/core/views.d8.drush.inc

Drush integration for views.

Functions

Namesort descending Description
drush_views_analyze Drush views analyze command.
drush_views_dev Command callback function for views-dev command.
drush_views_disable Drush views disable command.
drush_views_enable Drush views enable command.
drush_views_execute Drush views execute command.
drush_views_list Callback function for views-list command.
views_drush_cache_clear Adds a cache clear option for views.
views_drush_command Implements hook_drush_command().
views_drush_help Implements hook_drush_help().
views_views_disable_complete Command argument complete callback.
views_views_enable_complete Command argument complete callback.
_drush_views_complete Helper function to return a list of view names for complete callbacks.
_views_drush_op Perform operations on view objects.
_views_drush_op_types Returns an array of op types that can be performed on views.
_views_drush_view_is_disabled Returns whether a view is disabled.
_views_drush_view_is_enabled Returns whether a view is enabled.

File

commands/core/views.d8.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration for views.
  5. */
  6. use Drush\Log\LogLevel;
  7. use Drupal\views\Analyzer;
  8. use Drupal\views\Entity\View;
  9. use Drupal\views\Views;
  10. use Drupal\Component\Utility\MapArray;
  11. /**
  12. * Implements hook_drush_help().
  13. */
  14. function views_drush_help($section) {
  15. switch ($section) {
  16. case 'meta:views:title':
  17. return dt('Views commands');
  18. case 'meta:views:summary':
  19. return dt('Views drush commands.');
  20. }
  21. }
  22. /**
  23. * Implements hook_drush_command().
  24. */
  25. function views_drush_command() {
  26. $items = array();
  27. $base = array(
  28. 'core' => array('8+'),
  29. 'drupal dependencies' => array('views'),
  30. );
  31. $items['views-dev'] = array(
  32. 'description' => 'Set the Views settings to more developer-oriented values.',
  33. 'aliases' => array('vd'),
  34. ) + $base;
  35. $items['views-list'] = array(
  36. 'description' => 'Get a list of all views in the system.',
  37. 'aliases' => array('vl'),
  38. 'options' => array(
  39. 'name' => array(
  40. 'description' => 'A string contained in the view\'s name to filter the results with.',
  41. 'example-value' => 'node',
  42. 'value' => 'required',
  43. ),
  44. 'tags' => array(
  45. 'description' => 'A comma-separated list of views tags by which to filter the results.',
  46. 'example-value' => 'default',
  47. 'value' => 'required',
  48. ),
  49. 'status' => array(
  50. 'description' => 'Status of the views by which to filter the results. Choices: enabled, disabled.',
  51. 'example-value' => 'enabled',
  52. 'value' => 'required',
  53. ),
  54. ),
  55. 'examples' => array(
  56. 'drush vl' => 'Show a list of all available views.',
  57. 'drush vl --name=blog' => 'Show a list of views which names contain "blog".',
  58. 'drush vl --tags=tag1,tag2' => 'Show a list of views tagged with "tag1" or "tag2".',
  59. 'drush vl --status=enabled' => 'Show a list of enabled views.',
  60. ),
  61. 'outputformat' => array(
  62. 'default' => 'table',
  63. 'pipe-format' => 'list',
  64. 'field-default' => array('name', 'label', 'description', 'status', 'tag'),
  65. 'field-labels' => array('name' => 'Machine Name', 'label' => 'Name', 'description' => 'Description', 'status' => 'Status', 'tag' => 'Tag'),
  66. 'output-data-type' => 'format-table',
  67. ),
  68. ) + $base;
  69. $items['views-execute'] = array(
  70. 'description' => 'Execute a view and get the results.',
  71. 'aliases' => array('vex'),
  72. 'arguments' => array(
  73. 'view' => 'The name of the view to execute.',
  74. 'display' => 'The display ID to execute. If none specified, the default display will be used.',
  75. ),
  76. 'required-arguments' => 1,
  77. 'options' => array(
  78. 'count' => array(
  79. 'description' => 'Display a count of the results instead of each row.',
  80. ),
  81. 'rendered' => array(
  82. 'description' => 'Return the results as rendered HTML output for the display.',
  83. ),
  84. 'show-admin-links' => array(
  85. 'description' => 'Show contextual admin links in the rendered markup.',
  86. ),
  87. ),
  88. 'outputformat' => array(
  89. 'default' => 'print-r',
  90. 'pipe-format' => 'var_export',
  91. ),
  92. 'examples' => array(
  93. 'drush views-execute my_view' => 'Show the result set of the default display for the my_view view.',
  94. 'drush views-execute my_view page_1 --rendered' => 'Show the rendered output of the my_view:page_1 view.',
  95. 'drush views-execute my_view page_1 3 --count' => 'Show a count of my_view:page_1 with an agument of 3 being passed.',
  96. ),
  97. ) + $base;
  98. $items['views-analyze'] = array(
  99. 'drupal dependencies' => array('views', 'views_ui'),
  100. 'description' => 'Get a list of all Views analyze warnings',
  101. 'aliases' => array('va'),
  102. 'options' => array(
  103. 'format' => array(
  104. 'description' => 'Define the output format. Known formats are: json, print_r, and export.',
  105. ),
  106. ),
  107. ) + $base;
  108. $items['views-enable'] = array(
  109. 'description' => 'Enable the specified views.',
  110. 'arguments' => array(
  111. 'views' => 'A space delimited list of view names.',
  112. ),
  113. 'required-arguments' => 1,
  114. 'aliases' => array('ven'),
  115. 'examples' => array(
  116. 'drush ven frontpage taxonomy_term' => 'Enable the frontpage and taxonomy_term views.',
  117. ),
  118. ) + $base;
  119. $items['views-disable'] = array(
  120. 'description' => 'Disable the specified views.',
  121. 'arguments' => array(
  122. 'views' => 'A space delimited list of view names.',
  123. ),
  124. 'required-arguments' => 1,
  125. 'aliases' => array('vdis'),
  126. 'examples' => array(
  127. 'drush vdis frontpage taxonomy_term' => 'Disable the frontpage and taxonomy_term views.',
  128. ),
  129. ) + $base;
  130. return $items;
  131. }
  132. /**
  133. * Command callback function for views-dev command.
  134. *
  135. * Changes the settings to more developer oriented values.
  136. */
  137. function drush_views_dev() {
  138. $settings = array(
  139. 'ui.show.listing_filters' => TRUE,
  140. 'ui.show.master_display' => TRUE,
  141. 'ui.show.advanced_column' => TRUE,
  142. 'ui.always_live_preview' => FALSE,
  143. 'ui.always_live_preview_button' => TRUE,
  144. 'ui.show.preview_information' => TRUE,
  145. 'ui.show.sql_query.enabled' => TRUE,
  146. 'ui.show.sql_query.where' => 'above',
  147. 'ui.show.performance_statistics' => TRUE,
  148. 'ui.show.additional_queries' => TRUE,
  149. 'debug.output' => TRUE,
  150. 'debug.region' => 'message',
  151. 'ui.show.display_embed' => TRUE,
  152. );
  153. $config = \Drupal::configFactory()->getEditable('views.settings');
  154. foreach ($settings as $setting => $value) {
  155. $config->set($setting, $value);
  156. // Convert boolean values into a string to print.
  157. if (is_bool($value)) {
  158. $value = $value ? 'TRUE' : 'FALSE';
  159. }
  160. // Wrap string values in quotes.
  161. elseif (is_string($value)) {
  162. $value = "\"$value\"";
  163. }
  164. drush_log(dt('!setting set to !value', array('!setting' => $setting, '!value' => $value)));
  165. }
  166. // Save the new config.
  167. $config->save();
  168. drush_log(dt('New views configuration saved.'), LogLevel::SUCCESS);
  169. }
  170. /**
  171. * Callback function for views-list command.
  172. */
  173. function drush_views_list() {
  174. $disabled_views = array();
  175. $enabled_views = array();
  176. $format = drush_get_option('format', FALSE);
  177. $views = Drupal::entityManager()->getStorage('view')->loadMultiple();
  178. // Get the --name option.
  179. $name = array_filter(drush_get_option_list('name'));
  180. $with_name = !empty($name) ? TRUE : FALSE;
  181. // Get the --tags option.
  182. $tags = array_filter(drush_get_option_list('tags'));
  183. $with_tags = !empty($tags) ? TRUE : FALSE;
  184. // Get the --status option. Store user input appart to reuse it after.
  185. $status = drush_get_option('status', FALSE);
  186. // Throw an error if it's an invalid status.
  187. if ($status && !in_array($status, array('enabled', 'disabled'))) {
  188. return drush_set_error(dt('Invalid status: @status. Available options are "enabled" or "disabled"', array('@status' => $status)));
  189. }
  190. // Setup a row for each view.
  191. foreach ($views as $view) {
  192. // If options were specified, check that first mismatch push the loop to the
  193. // next view.
  194. if ($with_name && !stristr($view->id(), $name[0])) {
  195. continue;
  196. }
  197. if ($with_tags && !in_array($view->get('tag'), $tags)) {
  198. continue;
  199. }
  200. $status_bool = $status == 'enabled';
  201. if ($status && ($view->status() !== $status_bool)) {
  202. continue;
  203. }
  204. $row = array(
  205. 'name' => $view->id(),
  206. 'label' => $view->label(),
  207. 'description' => $view->get('description'),
  208. 'status' => $view->status() ? dt('Enabled') : dt('Disabled'),
  209. 'tag' => $view->get('tag'),
  210. );
  211. // Place the row in the appropiate array, so we can have disabled views at
  212. // the bottom.
  213. if ($view->status()) {
  214. $enabled_views[] = $row;
  215. }
  216. else{
  217. $disabled_views[] = $row;
  218. }
  219. }
  220. // Sort alphabeticaly.
  221. asort($disabled_views);
  222. asort($enabled_views);
  223. if (count($enabled_views) || count($disabled_views)) {
  224. $rows = array_merge($enabled_views, $disabled_views);
  225. return $rows;
  226. }
  227. else {
  228. drush_log(dt('No views found.'));
  229. }
  230. }
  231. /**
  232. * Drush views execute command.
  233. */
  234. function drush_views_execute($view_name, $display_id = NULL) {
  235. $args = func_get_args();
  236. $view_args = array();
  237. // If it's more than 2, we have arguments. A display has to be specified in
  238. // that case.
  239. if (count($args) > 2) {
  240. $view_args = array_slice($args, 2);
  241. }
  242. if (!$view = Views::getView($view_name)) {
  243. return drush_set_error(dt('View: "@view" not found.', array('@view' => $view_name)));
  244. }
  245. // Set the display and execute the view.
  246. $view->setDisplay($display_id);
  247. $view->preExecute($view_args);
  248. $view->execute();
  249. if (drush_get_option('count', FALSE)) {
  250. drush_set_default_outputformat('string');
  251. return count($view->result);
  252. }
  253. elseif (!empty($view->result)) {
  254. if (drush_get_option('rendered', FALSE)) {
  255. drush_set_default_outputformat('string');
  256. // Don't show admin links in markup by default.
  257. $view->hide_admin_links = !drush_get_option('show-admin-links', FALSE);
  258. $output = $view->preview();
  259. return drupal_render($output);
  260. }
  261. else {
  262. return $view->result;
  263. }
  264. }
  265. else {
  266. drush_log(dt('No results returned for this view.') ,LogLevel::WARNING);
  267. return NULL;
  268. }
  269. }
  270. /**
  271. * Drush views analyze command.
  272. */
  273. function drush_views_analyze() {
  274. $messages = NULL;
  275. $messages_count = 0;
  276. $format = drush_get_option('format', FALSE);
  277. $views = Drupal::entityManager()->getStorage('view')->loadMultiple();
  278. if (!empty($views)) {
  279. $analyzer = Drupal::service('views.analyzer');
  280. foreach ($views as $view_name => $view) {
  281. $view = $view->getExecutable();
  282. if ($messages = $analyzer->getMessages($view)) {
  283. if ($format) {
  284. $output = drush_format($messages, $format);
  285. drush_print($output);
  286. return $output;
  287. }
  288. else {
  289. drush_print($view_name);
  290. foreach ($messages as $message) {
  291. $messages_count++;
  292. drush_print($message['type'] .': '. $message['message'], 2);
  293. }
  294. }
  295. }
  296. }
  297. drush_log(dt('A total of @total views were analyzed and @messages problems were found.', array('@total' => count($views), '@messages' => $messages_count)), LogLevel::OK);
  298. return $messages;
  299. }
  300. else {
  301. return drush_set_error(dt('There are no views to analyze'));
  302. }
  303. }
  304. /**
  305. * Drush views enable command.
  306. */
  307. function drush_views_enable() {
  308. $view_names = func_get_args();
  309. _views_drush_op('enable', $view_names);
  310. }
  311. /**
  312. * Drush views disable command.
  313. */
  314. function drush_views_disable() {
  315. $view_names = func_get_args();
  316. _views_drush_op('disable', $view_names);
  317. }
  318. /**
  319. * Perform operations on view objects.
  320. *
  321. * @param string $op
  322. * The operation to perform.
  323. * @param array $view_names
  324. * An array of view names to load and perform this operation on.
  325. */
  326. function _views_drush_op($op = '', array $view_names = array()) {
  327. $op_types = _views_drush_op_types();
  328. if (!in_array($op, array_keys($op_types))) {
  329. return drush_set_error(dt('Invalid op type'));
  330. }
  331. $view_names = MapArray::copyValuesToKeys($view_names);
  332. if ($views = Drupal::entityManager()->getStorageController('view')->loadMultiple($view_names)) {
  333. foreach ($views as $view) {
  334. $tokens = array('@view' => $view->id(), '@action' => $op_types[$op]['action']);
  335. if ($op_types[$op]['validate']($view)) {
  336. $function = 'views_' . $op . '_view';
  337. drush_op($function, $view);
  338. drush_log(dt('View: @view has been @action', $tokens), LogLevel::SUCCESS);
  339. }
  340. else {
  341. drush_log(dt('View: @view is already @action', $tokens), LogLevel::NOTICE);
  342. }
  343. // Remove this view from the viewnames input list.
  344. unset($view_names[$view->id()]);
  345. }
  346. return $views;
  347. }
  348. else {
  349. drush_set_error(dt('No views have been loaded'));
  350. }
  351. // If we have some unmatched/leftover view names that weren't loaded.
  352. if (!empty($view_names)) {
  353. foreach ($view_names as $viewname) {
  354. drush_log(dt('View: @view could not be found.', array('@view' => $viewname)), LogLevel::ERROR);
  355. }
  356. }
  357. }
  358. /**
  359. * Returns an array of op types that can be performed on views.
  360. *
  361. * @return array
  362. * An associative array keyed by op type => action name.
  363. */
  364. function _views_drush_op_types() {
  365. return array(
  366. 'enable' => array(
  367. 'action' => dt('enabled'),
  368. 'validate' => '_views_drush_view_is_disabled',
  369. ),
  370. 'disable' => array(
  371. 'action' => dt('disabled'),
  372. 'validate' => '_views_drush_view_is_enabled',
  373. ),
  374. );
  375. }
  376. /**
  377. * Returns whether a view is enabled.
  378. *
  379. * @param Drupal\views\Entity\ViewDrupal\views\ $view
  380. * The view object to check.
  381. *
  382. * @return bool
  383. * TRUE if the View is enabled, FALSE otherwise.
  384. */
  385. function _views_drush_view_is_enabled(View $view) {
  386. return $view->status();
  387. }
  388. /**
  389. * Returns whether a view is disabled.
  390. *
  391. * @param Drupal\views\Entity\View $view
  392. * The view object to check.
  393. *
  394. * @return bool
  395. * TRUE if the View is disabled, FALSE otherwise.
  396. */
  397. function _views_drush_view_is_disabled(View $view) {
  398. return !$view->status();
  399. }
  400. /**
  401. * Adds a cache clear option for views.
  402. */
  403. function views_drush_cache_clear(&$types) {
  404. if (\Drupal::moduleHandler()->moduleExists('views')) {
  405. $types['views'] = 'views_invalidate_cache';
  406. }
  407. }
  408. /**
  409. * Command argument complete callback.
  410. */
  411. function views_views_enable_complete() {
  412. return _drush_views_complete();
  413. }
  414. /**
  415. * Command argument complete callback.
  416. */
  417. function views_views_disable_complete() {
  418. return _drush_views_complete();
  419. }
  420. /**
  421. * Helper function to return a list of view names for complete callbacks.
  422. *
  423. * @return array
  424. * An array of available view names.
  425. */
  426. function _drush_views_complete() {
  427. drush_bootstrap_max();
  428. return array('values' => array_keys(Drupal::entityManager()->getStorageController('view')->loadMultiple()));
  429. }