views_ui.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. <?php
  2. /**
  3. * @file
  4. * Provide structure for the administrative interface to Views.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function views_ui_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#views_ui':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Views UI module provides an interface for managing views for the Views module. For more information, see the <a href="@views" target="blank">online documentation for the Views UI module</a>.', array('@views' => 'https://www.drupal.org/documentation/modules/views')) . '</p>';
  15. $output .= '<h3>' . t('Uses') . '</h3>';
  16. $output .= '<dl>';
  17. $output .= '<dt>' . t('Creating and managing views') . '</dt>';
  18. $output .= '<dd>' . t('Views can be created from the <a href="/admin/structure/views">Views list page</a> by using the "Add view" action. Existing views can be managed from the <a href="/admin/structure/views">Views list page</a> by locating the view in the "Enabled" or "Disabled" list and selecting the desired operation action, for example "Edit".') . '</dd>';
  19. $output .= '<dt>' . t('Enabling and disabling views') . '</dt>';
  20. $output .= '<dd>' . t('Views can be enabled or disabled from the <a href="/admin/structure/views">Views list page</a>. To enable a view, find the view within the "Disabled" list and select the "Enable" operation. To disable a view find the view within the "Enabled" list and select the "Disable" operation.') . '</dd>';
  21. $output .= '<dt>' . t('Exporting and importing views') . '</dt>';
  22. $output .= '<dd>' . t('Views can be exported and imported as configuration files by using the Configuration Manager module.') . '</dd>';
  23. return $output;
  24. }
  25. }
  26. /**
  27. * Implements hook_menu().
  28. */
  29. function views_ui_menu() {
  30. $items = array();
  31. // Minor code reduction technique.
  32. $base = array(
  33. 'access callback' => 'user_access',
  34. 'access arguments' => array('administer views'),
  35. 'file' => 'includes/admin.inc',
  36. );
  37. // Top-level Views module pages (not tied to a particular View).
  38. $items['admin/structure/views/add'] = array(
  39. 'title' => 'Add new view',
  40. 'page callback' => 'views_ui_add_page',
  41. 'type' => MENU_LOCAL_ACTION,
  42. ) + $base;
  43. // Top-level Views module pages (not tied to a particular View).
  44. $items['admin/structure/views/add-template'] = array(
  45. 'title' => 'Add view from template',
  46. 'page callback' => 'views_ui_add_template_page',
  47. // Don't show a local action link if there aren't any templates.
  48. 'type' => views_get_all_templates() ? MENU_LOCAL_ACTION : MENU_VISIBLE_IN_BREADCRUMB,
  49. ) + $base;
  50. $items['admin/structure/views/import'] = array(
  51. 'title' => 'Import',
  52. 'page callback' => 'drupal_get_form',
  53. 'page arguments' => array('views_ui_import_page'),
  54. 'access callback' => 'views_import_access',
  55. 'type' => MENU_LOCAL_ACTION,
  56. ) + $base;
  57. $items['admin/structure/views/settings'] = array(
  58. 'title' => 'Settings',
  59. 'page callback' => 'drupal_get_form',
  60. 'page arguments' => array('views_ui_admin_settings_basic'),
  61. 'type' => MENU_LOCAL_TASK,
  62. ) + $base;
  63. $items['admin/structure/views/settings/basic'] = array(
  64. 'title' => 'Basic',
  65. 'page arguments' => array('views_ui_admin_settings_basic'),
  66. 'type' => MENU_DEFAULT_LOCAL_TASK,
  67. ) + $base;
  68. $items['admin/structure/views/settings/advanced'] = array(
  69. 'title' => 'Advanced',
  70. 'page arguments' => array('views_ui_admin_settings_advanced'),
  71. 'type' => MENU_LOCAL_TASK,
  72. 'weight' => 1,
  73. ) + $base;
  74. // The primary Edit View page. Secondary tabs for each Display are added in
  75. // views_ui_menu_local_tasks_alter().
  76. $items['admin/structure/views/view/%views_ui_cache'] = array(
  77. 'title callback' => 'views_ui_edit_page_title',
  78. 'title arguments' => array(4),
  79. 'page callback' => 'views_ui_edit_page',
  80. 'page arguments' => array(4),
  81. ) + $base;
  82. $items['admin/structure/views/view/%views_ui_cache/edit'] = array(
  83. 'title' => 'Edit view',
  84. 'type' => MENU_DEFAULT_LOCAL_TASK,
  85. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  86. 'weight' => -10,
  87. 'theme callback' => 'ajax_base_page_theme',
  88. ) + $base;
  89. $items['admin/structure/views/view/%views_ui_cache/edit/%/ajax'] = array(
  90. 'page callback' => 'views_ui_ajax_get_form',
  91. 'page arguments' => array('views_ui_edit_form', 4, 6),
  92. 'delivery callback' => 'ajax_deliver',
  93. 'theme callback' => 'ajax_base_page_theme',
  94. 'type' => MENU_CALLBACK,
  95. ) + $base;
  96. $items['admin/structure/views/view/%views_ui_cache/preview/%'] = array(
  97. 'page callback' => 'views_ui_build_preview',
  98. 'page arguments' => array(4, 6),
  99. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  100. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  101. ) + $base;
  102. $items['admin/structure/views/view/%views_ui_cache/preview/%/ajax'] = array(
  103. 'page callback' => 'views_ui_build_preview',
  104. 'page arguments' => array(4, 6),
  105. 'delivery callback' => 'ajax_deliver',
  106. 'theme callback' => 'ajax_base_page_theme',
  107. 'type' => MENU_CALLBACK,
  108. ) + $base;
  109. // Additional pages for acting on a View.
  110. $items['admin/structure/views/view/%views_ui_cache/break-lock'] = array(
  111. 'title' => 'Break lock',
  112. 'page callback' => 'drupal_get_form',
  113. 'page arguments' => array('views_ui_break_lock_confirm', 4),
  114. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  115. ) + $base;
  116. // NoJS/AJAX callbacks that can use the default Views AJAX form system.
  117. $items['admin/structure/views/nojs/%/%views_ui_cache'] = array(
  118. 'page callback' => 'views_ui_ajax_form',
  119. 'page arguments' => array(FALSE, 4, 5),
  120. 'type' => MENU_CALLBACK,
  121. ) + $base;
  122. $items['admin/structure/views/ajax/%/%views_ui_cache'] = array(
  123. 'page callback' => 'views_ui_ajax_form',
  124. 'page arguments' => array(TRUE, 4, 5),
  125. 'delivery callback' => 'ajax_deliver',
  126. 'type' => MENU_CALLBACK,
  127. ) + $base;
  128. // NoJS/AJAX callbacks that require custom page callbacks.
  129. $ajax_callbacks = array(
  130. 'preview' => 'views_ui_preview',
  131. );
  132. foreach ($ajax_callbacks as $menu => $menu_callback) {
  133. $items['admin/structure/views/nojs/' . $menu . '/%views_ui_cache/%'] = array(
  134. 'page callback' => $menu_callback,
  135. 'page arguments' => array(5, 6),
  136. ) + $base;
  137. $items['admin/structure/views/ajax/' . $menu . '/%views_ui_cache/%'] = array(
  138. 'page callback' => $menu_callback,
  139. 'page arguments' => array(5, 6),
  140. 'delivery callback' => 'ajax_deliver',
  141. ) + $base;
  142. }
  143. // Autocomplete callback for tagging a View.
  144. // Views module uses admin/views/... instead of admin/structure/views/... for
  145. // autocomplete paths, so be consistent with that.
  146. // @todo Change to admin/structure/views/... when the change can be made to
  147. // Views module as well.
  148. $items['admin/views/ajax/autocomplete/tag'] = array(
  149. 'page callback' => 'views_ui_autocomplete_tag',
  150. 'type' => MENU_CALLBACK,
  151. ) + $base;
  152. // A page in the Reports section to show usage of fields in all views.
  153. $items['admin/reports/fields/list'] = array(
  154. 'title' => 'List',
  155. 'type' => MENU_DEFAULT_LOCAL_TASK,
  156. 'weight' => -10,
  157. );
  158. $items['admin/reports/fields/views-fields'] = array(
  159. 'title' => 'Used in views',
  160. 'description' => 'Overview of fields used in all views.',
  161. 'page callback' => 'views_ui_field_list',
  162. 'type' => MENU_LOCAL_TASK,
  163. 'weight' => 0,
  164. ) + $base;
  165. // A page in the Reports section to show usage of plugins in all views.
  166. $items['admin/reports/views-plugins'] = array(
  167. 'title' => 'Views plugins',
  168. 'description' => 'Overview of plugins used in all views.',
  169. 'page callback' => 'views_ui_plugin_list',
  170. ) + $base;
  171. return $items;
  172. }
  173. /**
  174. * Implements hook_theme().
  175. */
  176. function views_ui_theme() {
  177. $path = drupal_get_path('module', 'views');
  178. require_once DRUPAL_ROOT . "/$path/includes/admin.inc";
  179. return array(
  180. // Edit a view.
  181. 'views_ui_display_tab_setting' => array(
  182. 'variables' => array(
  183. 'description' => '',
  184. 'link' => '',
  185. 'settings_links' => array(),
  186. 'overridden' => FALSE,
  187. 'defaulted' => FALSE,
  188. 'description_separator' => TRUE,
  189. 'class' => array(),
  190. ),
  191. 'template' => 'views-ui-display-tab-setting',
  192. 'path' => "$path/theme",
  193. ),
  194. 'views_ui_display_tab_bucket' => array(
  195. 'render element' => 'element',
  196. 'template' => 'views-ui-display-tab-bucket',
  197. 'path' => "$path/theme",
  198. ),
  199. 'views_ui_rearrange_form' => array(
  200. 'render element' => 'form',
  201. ),
  202. 'views_ui_rearrange_filter_form' => array(
  203. 'render element' => 'form',
  204. 'file' => 'includes/admin.inc',
  205. ),
  206. 'views_ui_expose_filter_form' => array(
  207. 'render element' => 'form',
  208. 'file' => 'includes/admin.inc',
  209. ),
  210. // List views.
  211. 'views_ui_view_info' => array(
  212. 'variables' => array('view' => NULL, 'base' => NULL),
  213. 'file' => "includes/admin.inc",
  214. ),
  215. // Group of filters.
  216. 'views_ui_build_group_filter_form' => array(
  217. 'render element' => 'form',
  218. 'file' => 'includes/admin.inc',
  219. ),
  220. // Tab themes.
  221. 'views_tabset' => array(
  222. 'variables' => array('tabs' => NULL),
  223. ),
  224. 'views_tab' => array(
  225. 'variables' => array('body' => NULL),
  226. ),
  227. 'views_ui_reorder_displays_form' => array(
  228. 'render element' => 'form',
  229. 'file' => 'includes/admin.inc',
  230. ),
  231. // On behalf of a plugin.
  232. 'views_ui_style_plugin_table' => array(
  233. 'render element' => 'form',
  234. ),
  235. // When previewing a view.
  236. 'views_ui_view_preview_section' => array(
  237. 'variables' => array(
  238. 'view' => NULL,
  239. 'section' => NULL,
  240. 'content' => NULL,
  241. 'links' => '',
  242. ),
  243. ),
  244. // Generic container wrapper, to use instead of theme_container when an id
  245. // is not desired.
  246. 'views_container' => array(
  247. 'render element' => 'element',
  248. 'file' => 'theme/theme.inc',
  249. ),
  250. );
  251. }
  252. /**
  253. * Implements hook_custom_theme().
  254. */
  255. function views_ui_custom_theme() {
  256. $theme = variable_get('views_ui_custom_theme', '_default');
  257. if ($theme != '_default') {
  258. $available = list_themes();
  259. if (isset($available[$theme]) && $available[$theme]->status && preg_match('/^admin\/structure\/views/', current_path())) {
  260. return $theme;
  261. }
  262. }
  263. }
  264. /**
  265. * Page title callback for the Edit View page.
  266. */
  267. function views_ui_edit_page_title($view) {
  268. module_load_include('inc', 'views_ui', 'includes/admin');
  269. $bases = views_fetch_base_tables();
  270. $name = $view->get_human_name();
  271. if (isset($bases[$view->base_table])) {
  272. $name .= ' (' . $bases[$view->base_table]['title'] . ')';
  273. }
  274. return $name;
  275. }
  276. /**
  277. * Specialized menu callback to load a view and check its locked status.
  278. *
  279. * @param string $name
  280. * The machine name of the view.
  281. *
  282. * @return object
  283. * The view object, with a "locked" property indicating whether or not
  284. * someone else is already editing the view.
  285. */
  286. function views_ui_cache_load($name) {
  287. ctools_include('object-cache');
  288. views_include('view');
  289. $view = ctools_object_cache_get('view', $name);
  290. $original_view = views_get_view($name);
  291. if (empty($view)) {
  292. $view = $original_view;
  293. if (!empty($view)) {
  294. // Check to see if someone else is already editing this view.
  295. $view->locked = ctools_object_cache_test('view', $view->name);
  296. // Set a flag to indicate that this view is being edited.
  297. // This flag will be used e.g. to determine whether strings
  298. // should be localized.
  299. $view->editing = TRUE;
  300. }
  301. }
  302. else {
  303. // Keep disabled/enabled status real.
  304. if ($original_view) {
  305. $view->disabled = !empty($original_view->disabled);
  306. }
  307. }
  308. if (empty($view)) {
  309. return FALSE;
  310. }
  311. else {
  312. return $view;
  313. }
  314. }
  315. /**
  316. * Cache set.
  317. *
  318. * Specialized cache function to add a flag to our view, include an appropriate
  319. * include, and cache more easily.
  320. */
  321. function views_ui_cache_set(&$view) {
  322. if (!empty($view->locked)) {
  323. drupal_set_message(t('Changes cannot be made to a locked view.'), 'error');
  324. return;
  325. }
  326. ctools_include('object-cache');
  327. // Let any future object know that this view has changed.
  328. $view->changed = TRUE;
  329. if (isset($view->current_display)) {
  330. // Add the knowledge of the changed display, too.
  331. $view->changed_display[$view->current_display] = TRUE;
  332. unset($view->current_display);
  333. }
  334. // Unset handlers; we don't want to write these into the cache.
  335. unset($view->display_handler);
  336. unset($view->default_display);
  337. $view->query = NULL;
  338. foreach (array_keys($view->display) as $id) {
  339. unset($view->display[$id]->handler);
  340. unset($view->display[$id]->default_display);
  341. }
  342. ctools_object_cache_set('view', $view->name, $view);
  343. }
  344. /**
  345. * Default Load.
  346. *
  347. * Specialized menu callback to load a view that is only a default
  348. * view.
  349. */
  350. function views_ui_default_load($name) {
  351. $view = views_get_view($name);
  352. if ($view->type == t('Default')) {
  353. return $view;
  354. }
  355. return FALSE;
  356. }
  357. /**
  358. * Theme preprocess for views-view.tpl.php.
  359. */
  360. function views_ui_preprocess_views_view(&$vars) {
  361. $view = $vars['view'];
  362. if (!empty($view->views_ui_context) && module_exists('contextual')) {
  363. $view->hide_admin_links = TRUE;
  364. $sections = array(
  365. 'title',
  366. 'header',
  367. 'exposed',
  368. 'rows',
  369. 'pager',
  370. 'more',
  371. 'footer',
  372. 'empty',
  373. 'attachment_after',
  374. 'attachment_before',
  375. );
  376. foreach ($sections as $section) {
  377. if (!empty($vars[$section])) {
  378. $vars[$section] = array(
  379. '#theme' => 'views_ui_view_preview_section',
  380. '#view' => $view,
  381. '#section' => $section,
  382. '#content' => is_array($vars[$section]) ? drupal_render($vars[$section]) : $vars[$section],
  383. '#theme_wrappers' => array('views_container'),
  384. '#attributes' => array('class' => 'contextual-links-region'),
  385. );
  386. $vars[$section] = drupal_render($vars[$section]);
  387. }
  388. }
  389. }
  390. }
  391. /**
  392. * Theme preprocess for theme_views_ui_view_preview_section().
  393. *
  394. * @todo Perhaps move this to includes/admin.inc or theme/theme.inc.
  395. */
  396. function template_preprocess_views_ui_view_preview_section(&$vars) {
  397. switch ($vars['section']) {
  398. case 'title':
  399. $vars['title'] = t('Title');
  400. $links = views_ui_view_preview_section_display_category_links($vars['view'], 'title', $vars['title']);
  401. break;
  402. case 'header':
  403. $vars['title'] = t('Header');
  404. $links = views_ui_view_preview_section_handler_links($vars['view'], $vars['section']);
  405. break;
  406. case 'empty':
  407. $vars['title'] = t('No results behavior');
  408. $links = views_ui_view_preview_section_handler_links($vars['view'], $vars['section']);
  409. break;
  410. case 'exposed':
  411. // @todo Sorts can be exposed too, so we may need a better title.
  412. $vars['title'] = t('Exposed Filters');
  413. $links = views_ui_view_preview_section_display_category_links($vars['view'], 'exposed_form_options', $vars['title']);
  414. break;
  415. case 'rows':
  416. // @todo The title needs to depend on what is being viewed.
  417. $vars['title'] = t('Content');
  418. $links = views_ui_view_preview_section_rows_links($vars['view']);
  419. break;
  420. case 'pager':
  421. $vars['title'] = t('Pager');
  422. $links = views_ui_view_preview_section_display_category_links($vars['view'], 'pager_options', $vars['title']);
  423. break;
  424. case 'more':
  425. $vars['title'] = t('More');
  426. $links = views_ui_view_preview_section_display_category_links($vars['view'], 'use_more', $vars['title']);
  427. break;
  428. case 'footer':
  429. $vars['title'] = t('Footer');
  430. $links = views_ui_view_preview_section_handler_links($vars['view'], $vars['section']);
  431. break;
  432. case 'attachment_before':
  433. // @todo Add links to the attachment configuration page.
  434. $vars['title'] = t('Attachment before');
  435. break;
  436. case 'attachment_after':
  437. // @todo Add links to the attachment configuration page.
  438. $vars['title'] = t('Attachment after');
  439. break;
  440. }
  441. if (isset($links)) {
  442. $build = array(
  443. '#prefix' => '<div class="contextual-links-wrapper">',
  444. '#suffix' => '</div>',
  445. '#theme' => 'links__contextual',
  446. '#links' => $links,
  447. '#attributes' => array('class' => array('contextual-links')),
  448. '#attached' => array(
  449. 'library' => array(array('contextual', 'contextual-links')),
  450. ),
  451. );
  452. $vars['links'] = drupal_render($build);
  453. }
  454. $vars['theme_hook_suggestions'][] = 'views_ui_view_preview_section__' . $vars['section'];
  455. }
  456. /**
  457. * Returns the HTML for a section of a View being previewed within the Views UI.
  458. */
  459. function theme_views_ui_view_preview_section($vars) {
  460. return '<h1 class="section-title">' . $vars['title'] . '</h1>'
  461. . $vars['links']
  462. . '<div class="preview-section">' . $vars['content'] . '</div>';
  463. }
  464. /**
  465. * Returns contextual links for each handler of a certain section.
  466. *
  467. * @param string $title
  468. * Add a bolded title of this section.
  469. *
  470. * @todo Bring in relationships.
  471. * @todo Refactor this function to use much of views_ui_edit_form_get_bucket.
  472. */
  473. function views_ui_view_preview_section_handler_links($view, $type, $title = FALSE) {
  474. $display = $view->display_handler->display;
  475. $handlers = $view->display_handler->get_handlers($type);
  476. $links = array();
  477. $types = views_object_types();
  478. if ($title) {
  479. $links[$type . '-title'] = array(
  480. 'title' => $types[$type]['title'],
  481. );
  482. }
  483. foreach ($handlers as $id => $handler) {
  484. $field_name = $handler->ui_name(TRUE);
  485. $links[$type . '-edit-' . $id] = array(
  486. 'title' => t('Edit @section', array('@section' => $field_name)),
  487. 'href' => "admin/structure/views/nojs/config-item/$view->name/$display->id/$type/$id",
  488. 'attributes' => array('class' => array('views-ajax-link')),
  489. );
  490. }
  491. $links[$type . '-add'] = array(
  492. 'title' => t('Add new'),
  493. 'href' => "admin/structure/views/nojs/add-item/$view->name/$display->id/$type",
  494. 'attributes' => array('class' => array('views-ajax-link')),
  495. );
  496. return $links;
  497. }
  498. /**
  499. * Returns a link to editing a certain display setting.
  500. */
  501. function views_ui_view_preview_section_display_category_links($view, $type, $title) {
  502. $display = $view->display_handler->display;
  503. $links = array(
  504. $type . '-edit' => array(
  505. 'title' => t('Edit @section', array('@section' => $title)),
  506. 'href' => "admin/structure/views/nojs/display/$view->name/$display->id/$type",
  507. 'attributes' => array('class' => array('views-ajax-link')),
  508. ),
  509. );
  510. return $links;
  511. }
  512. /**
  513. * Returns all contextual links for the main content part of the view.
  514. */
  515. function views_ui_view_preview_section_rows_links($view) {
  516. $links = array();
  517. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'filter', TRUE));
  518. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'field', TRUE));
  519. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'sort', TRUE));
  520. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'argument', TRUE));
  521. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'relationship', TRUE));
  522. return $links;
  523. }
  524. /**
  525. * Implments hook_ctools_plugin_directory().
  526. *
  527. * Views UI provides wizard plugins on behalf of core base tables.
  528. */
  529. function views_ui_ctools_plugin_directory($module, $plugin) {
  530. if ($module == 'views_ui' || ($module == 'ctools' && $plugin == 'export_ui')) {
  531. return 'plugins/' . $plugin;
  532. }
  533. }
  534. /**
  535. * Fetch metadata on a specific views ui wizard plugin.
  536. *
  537. * @param string $wizard_type
  538. * Name of a wizard, or name of a base table.
  539. *
  540. * @return array
  541. * An array with information about the requested wizard type.
  542. */
  543. function views_ui_get_wizard($wizard_type) {
  544. ctools_include('plugins');
  545. $wizard = ctools_get_plugins('views_ui', 'views_wizard', $wizard_type);
  546. // @todo - handle this via an alter hook instead.
  547. if (!$wizard) {
  548. // Must be a base table using the default wizard plugin.
  549. $base_tables = views_fetch_base_tables();
  550. if (!empty($base_tables[$wizard_type])) {
  551. $wizard = views_ui_views_wizard_defaults();
  552. $wizard['base_table'] = $wizard_type;
  553. $wizard['title'] = $base_tables[$wizard_type]['title'];
  554. }
  555. // The plugin is neither a base table nor an existing wizard.
  556. else {
  557. vpr('Views Wizard: @wizard does not exist. Be sure to implement hook_ctools_plugin_directory.', array('@wizard' => $wizard_type));
  558. }
  559. }
  560. return $wizard;
  561. }
  562. /**
  563. * Fetch metadata for all content_type plugins.
  564. *
  565. * @return array
  566. * An array of arrays with information about all available views wizards.
  567. */
  568. function views_ui_get_wizards() {
  569. ctools_include('plugins');
  570. $wizard_plugins = ctools_get_plugins('views_ui', 'views_wizard');
  571. $wizard_tables = array();
  572. foreach ($wizard_plugins as $info) {
  573. $wizard_tables[$info['base_table']] = TRUE;
  574. }
  575. $base_tables = views_fetch_base_tables();
  576. $default_wizard = views_ui_views_wizard_defaults();
  577. // Find base tables with no wizard.
  578. // @todo - handle this via an alter hook for plugins?
  579. foreach ($base_tables as $table => $info) {
  580. if (!isset($wizard_tables[$table])) {
  581. $wizard = $default_wizard;
  582. $wizard['title'] = $info['title'];
  583. $wizard['base_table'] = $table;
  584. $wizard_plugins[$table] = $wizard;
  585. }
  586. }
  587. return $wizard_plugins;
  588. }
  589. /**
  590. * Helper function to define the default values for a Views wizard plugin.
  591. *
  592. * @return array
  593. * An array of defaults for a views wizard.
  594. */
  595. function views_ui_views_wizard_defaults() {
  596. return array(
  597. // The children may, for example, be a different variant for each node type.
  598. 'get children' => NULL,
  599. 'get child' => NULL,
  600. // Title and base table must be populated. They are empty here just
  601. // so they are documented.
  602. 'title' => '',
  603. 'base_table' => NULL,
  604. // This is a callback that takes the wizard as argument and returns
  605. // an instantiazed Views UI form wizard object.
  606. 'get_instance' => 'views_ui_get_form_wizard_instance',
  607. 'form_wizard_class' => array(
  608. 'file' => 'views_ui_base_views_wizard',
  609. 'class' => 'ViewsUiBaseViewsWizard',
  610. ),
  611. );
  612. }
  613. /**
  614. * Inform CTools that the Views wizard plugin can have child plugins.
  615. */
  616. function views_ui_ctools_plugin_type() {
  617. return array(
  618. 'views_wizard' => array(
  619. 'child plugins' => TRUE,
  620. 'classes' => array(
  621. 'form_wizard_class',
  622. ),
  623. 'defaults' => views_ui_views_wizard_defaults(),
  624. ),
  625. );
  626. }
  627. /**
  628. * Get form wizard instance.
  629. */
  630. function views_ui_get_form_wizard_instance($wizard) {
  631. if (isset($wizard['form_wizard_class']['class'])) {
  632. $class = $wizard['form_wizard_class']['class'];
  633. return new $class($wizard);
  634. }
  635. else {
  636. return new ViewsUiBaseViewsWizard($wizard);
  637. }
  638. }
  639. /**
  640. * Implements hook_views_plugins_alter().
  641. */
  642. function views_ui_views_plugins_alter(&$plugins) {
  643. // Attach contextual links to each display plugin. The links will point to
  644. // paths underneath "admin/structure/views/view/{$view->name}" (i.e., paths
  645. // for editing and performing other contextual actions on the view).
  646. foreach ($plugins['display'] as &$display) {
  647. $display['contextual links']['views_ui'] = array(
  648. 'parent path' => 'admin/structure/views/view',
  649. 'argument properties' => array('name'),
  650. );
  651. }
  652. }
  653. /**
  654. * Implements hook_contextual_links_view_alter().
  655. */
  656. function views_ui_contextual_links_view_alter(&$element, $items) {
  657. // Remove contextual links from being rendered, when so desired, such as
  658. // within a View preview.
  659. if (views_ui_contextual_links_suppress()) {
  660. $element['#links'] = array();
  661. }
  662. // Append the display ID to the Views UI edit links, so that clicking on the
  663. // contextual link takes you directly to the correct display tab on the edit
  664. // screen.
  665. elseif (!empty($element['#links']['views-ui-edit']) && !empty($element['#element']['#views_contextual_links_info']['views_ui']['view_display_id'])) {
  666. $display_id = $element['#element']['#views_contextual_links_info']['views_ui']['view_display_id'];
  667. $element['#links']['views-ui-edit']['href'] .= '/' . $display_id;
  668. }
  669. }
  670. /**
  671. * Sets a static variable for controlling whether contextual links are rendered.
  672. *
  673. * @see views_ui_contextual_links_view_alter()
  674. */
  675. function views_ui_contextual_links_suppress($set = NULL) {
  676. $suppress = &drupal_static(__FUNCTION__);
  677. if (isset($set)) {
  678. $suppress = $set;
  679. }
  680. return $suppress;
  681. }
  682. /**
  683. * Increments the views_ui_contextual_links_suppress() static variable.
  684. *
  685. * When this function is added to the #pre_render of an element, and
  686. * 'views_ui_contextual_links_suppress_pop' is added to the #post_render of the
  687. * same element, then all contextual links within the element and its
  688. * descendants are suppressed from being rendered. This is used, for example,
  689. * during a View preview, when it is not desired for nodes in the Views result
  690. * to have contextual links.
  691. *
  692. * @see views_ui_contextual_links_suppress_pop()
  693. */
  694. function views_ui_contextual_links_suppress_push() {
  695. views_ui_contextual_links_suppress(((int) views_ui_contextual_links_suppress()) + 1);
  696. }
  697. /**
  698. * Decrements the views_ui_contextual_links_suppress() static variable.
  699. *
  700. * @see views_ui_contextual_links_suppress_push()
  701. */
  702. function views_ui_contextual_links_suppress_pop() {
  703. views_ui_contextual_links_suppress(((int) views_ui_contextual_links_suppress()) - 1);
  704. }
  705. /**
  706. * Menu callback.
  707. *
  708. * Handles AJAX form submissions similar to ajax_form_callback(), but can be
  709. * used for uncached forms.
  710. *
  711. * Ajax_form_callback(), the menu callback for the system/ajax path, requires
  712. * the form to be retrievable from the form cache, because it lacks a trusted
  713. * $form_id argument with which to call drupal_retrieve_form(). When AJAX is
  714. * wanted on a non-cacheable form, #ajax['path'] can be set to a path whose
  715. * menu router item's 'page callback' is this function, and whose
  716. * 'page arguments' is the form id, optionally followed by additional build
  717. * arguments, as expected by drupal_get_form().
  718. *
  719. * The same caution must be used when defining a hook_menu() entry with this
  720. * page callback as is used when defining a hook_menu() entry with the
  721. * 'drupal_get_form' page callback: a 'page arguments' must be specified with a
  722. * literal value as the first argument, because $form_id determines which form
  723. * builder function gets called, so must be safe from user tampering.
  724. *
  725. * @see drupal_get_form()
  726. * @see ajax_form_callback()
  727. * @see http://drupal.org/node/774876
  728. */
  729. function views_ui_ajax_get_form($form_id) {
  730. $args = func_get_args();
  731. array_shift($args);
  732. // @see ajax_get_form()
  733. $form_state = array(
  734. 'no_redirect' => TRUE,
  735. );
  736. $form_state['rebuild_info']['copy']['#build_id'] = TRUE;
  737. $form_state['rebuild_info']['copy']['#action'] = TRUE;
  738. // @see drupal_get_form()
  739. $form_state['build_info']['args'] = $args;
  740. $form = drupal_build_form($form_id, $form_state);
  741. // @see ajax_form_callback()
  742. if (!empty($form_state['triggering_element'])) {
  743. $callback = $form_state['triggering_element']['#ajax']['callback'];
  744. }
  745. if (!empty($callback) && function_exists($callback)) {
  746. return $callback($form, $form_state);
  747. }
  748. }
  749. /**
  750. * @todo move these when we can
  751. */
  752. /**
  753. * Helper function to get a list of paths assigned to a view.
  754. *
  755. * @param object $view
  756. * The view.
  757. *
  758. * @return array
  759. * An array of links to this view's display paths.
  760. */
  761. function _views_ui_get_paths($view) {
  762. $all_paths = array();
  763. if (empty($view->display)) {
  764. $all_paths[] = t('Edit this view to add a display.');
  765. }
  766. else {
  767. // Make sure all the handlers are set up.
  768. $view->init_display();
  769. foreach ($view->display as $display) {
  770. if (!empty($display->handler) && $display->handler->has_path()) {
  771. $one_path = $display->handler->get_option('path');
  772. if (empty($view->disabled) && strpos($one_path, '%') === FALSE) {
  773. // @codingStandardsIgnoreLine
  774. $all_paths[] = l('/' . $one_path, $one_path);
  775. }
  776. else {
  777. $all_paths[] = check_plain('/' . $one_path);
  778. }
  779. }
  780. }
  781. }
  782. return array_unique($all_paths);
  783. }
  784. /**
  785. * Helper function to get a list of displays included in a view.
  786. *
  787. * @param object $view
  788. * The view.
  789. *
  790. * @return array
  791. * An array of display types that this view includes.
  792. */
  793. function _views_ui_get_displays_list($view) {
  794. $displays = array();
  795. foreach ($view->display as $display) {
  796. if (!empty($display->handler->definition['admin'])) {
  797. $displays[$display->handler->definition['admin']] = TRUE;
  798. }
  799. }
  800. if ($displays) {
  801. ksort($displays);
  802. $displays = array_keys($displays);
  803. }
  804. return $displays;
  805. }
  806. /**
  807. * This is part of a patch to address a jQueryUI bug.
  808. *
  809. * The bug is responsible
  810. * for the inability to scroll a page when a modal dialog is active. If the
  811. * content of the dialog extends beyond the bottom of the viewport, the user is
  812. * only able to scroll with a mousewheel or up/down keyboard keys.
  813. *
  814. * @see http://bugs.jqueryui.com/ticket/4671
  815. * @see https://bugs.webkit.org/show_bug.cgi?id=19033
  816. * @see /js/jquery.ui.dialog.patch.js
  817. * @see /js/jquery.ui.dialog.min.js
  818. *
  819. * The JavaScript patch overwrites the $.ui.dialog.overlay.events object to
  820. * remove the mousedown, mouseup and click events from the list of events that
  821. * are bound in $.ui.dialog.overlay.create.
  822. */
  823. function views_ui_library_alter(&$libraries, $module) {
  824. if ($module == 'system' && isset($libraries['ui.dialog'])) {
  825. // Only apply the fix, if we don't have an up to date jQueryUI version.
  826. if (version_compare($libraries['ui.dialog']['version'], '1.7.2', '>=') && version_compare($libraries['ui.dialog']['version'], '1.10.0', '<')) {
  827. $libraries['ui.dialog']['js'][drupal_get_path('module', 'views') . '/js/jquery.ui.dialog.patch.js'] = array();
  828. }
  829. }
  830. }
  831. /**
  832. * Truncate strings to a set length and provide a ... if they truncated.
  833. *
  834. * This is often used in the UI to ensure long strings fit.
  835. */
  836. function views_ui_truncate($string, $length) {
  837. if (drupal_strlen($string) > $length) {
  838. $string = drupal_substr($string, 0, $length);
  839. $string .= '...';
  840. }
  841. return $string;
  842. }