views_ui.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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
  395. * Perhaps move this to includes/admin.inc or theme/theme.inc
  396. */
  397. function template_preprocess_views_ui_view_preview_section(&$vars) {
  398. switch ($vars['section']) {
  399. case 'title':
  400. $vars['title'] = t('Title');
  401. $links = views_ui_view_preview_section_display_category_links($vars['view'], 'title', $vars['title']);
  402. break;
  403. case 'header':
  404. $vars['title'] = t('Header');
  405. $links = views_ui_view_preview_section_handler_links($vars['view'], $vars['section']);
  406. break;
  407. case 'empty':
  408. $vars['title'] = t('No results behavior');
  409. $links = views_ui_view_preview_section_handler_links($vars['view'], $vars['section']);
  410. break;
  411. case 'exposed':
  412. // @todo Sorts can be exposed too, so we may need a better title.
  413. $vars['title'] = t('Exposed Filters');
  414. $links = views_ui_view_preview_section_display_category_links($vars['view'], 'exposed_form_options', $vars['title']);
  415. break;
  416. case 'rows':
  417. // @todo The title needs to depend on what is being viewed.
  418. $vars['title'] = t('Content');
  419. $links = views_ui_view_preview_section_rows_links($vars['view']);
  420. break;
  421. case 'pager':
  422. $vars['title'] = t('Pager');
  423. $links = views_ui_view_preview_section_display_category_links($vars['view'], 'pager_options', $vars['title']);
  424. break;
  425. case 'more':
  426. $vars['title'] = t('More');
  427. $links = views_ui_view_preview_section_display_category_links($vars['view'], 'use_more', $vars['title']);
  428. break;
  429. case 'footer':
  430. $vars['title'] = t('Footer');
  431. $links = views_ui_view_preview_section_handler_links($vars['view'], $vars['section']);
  432. break;
  433. case 'attachment_before':
  434. // @todo: Add links to the attachment configuration page.
  435. $vars['title'] = t('Attachment before');
  436. break;
  437. case 'attachment_after':
  438. // @todo: Add links to the attachment configuration page.
  439. $vars['title'] = t('Attachment after');
  440. break;
  441. }
  442. if (isset($links)) {
  443. $build = array(
  444. '#prefix' => '<div class="contextual-links-wrapper">',
  445. '#suffix' => '</div>',
  446. '#theme' => 'links__contextual',
  447. '#links' => $links,
  448. '#attributes' => array('class' => array('contextual-links')),
  449. '#attached' => array(
  450. 'library' => array(array('contextual', 'contextual-links')),
  451. ),
  452. );
  453. $vars['links'] = drupal_render($build);
  454. }
  455. $vars['theme_hook_suggestions'][] = 'views_ui_view_preview_section__' . $vars['section'];
  456. }
  457. /**
  458. * Returns the HTML for a section of a View being previewed within the Views UI.
  459. */
  460. function theme_views_ui_view_preview_section($vars) {
  461. return '<h1 class="section-title">' . $vars['title'] . '</h1>'
  462. . $vars['links']
  463. . '<div class="preview-section">' . $vars['content'] . '</div>';
  464. }
  465. /**
  466. * Returns contextual links for each handler of a certain section.
  467. *
  468. * @param string $title
  469. * Add a bolded title of this section.
  470. *
  471. * @TODO
  472. * Bring in relationships
  473. * Refactor this function to use much stuff of views_ui_edit_form_get_bucket.
  474. */
  475. function views_ui_view_preview_section_handler_links($view, $type, $title = FALSE) {
  476. $display = $view->display_handler->display;
  477. $handlers = $view->display_handler->get_handlers($type);
  478. $links = array();
  479. $types = views_object_types();
  480. if ($title) {
  481. $links[$type . '-title'] = array(
  482. 'title' => $types[$type]['title'],
  483. );
  484. }
  485. foreach ($handlers as $id => $handler) {
  486. $field_name = $handler->ui_name(TRUE);
  487. $links[$type . '-edit-' . $id] = array(
  488. 'title' => t('Edit @section', array('@section' => $field_name)),
  489. 'href' => "admin/structure/views/nojs/config-item/$view->name/$display->id/$type/$id",
  490. 'attributes' => array('class' => array('views-ajax-link')),
  491. );
  492. }
  493. $links[$type . '-add'] = array(
  494. 'title' => t('Add new'),
  495. 'href' => "admin/structure/views/nojs/add-item/$view->name/$display->id/$type",
  496. 'attributes' => array('class' => array('views-ajax-link')),
  497. );
  498. return $links;
  499. }
  500. /**
  501. * Returns a link to editing a certain display setting.
  502. */
  503. function views_ui_view_preview_section_display_category_links($view, $type, $title) {
  504. $display = $view->display_handler->display;
  505. $links = array(
  506. $type . '-edit' => array(
  507. 'title' => t('Edit @section', array('@section' => $title)),
  508. 'href' => "admin/structure/views/nojs/display/$view->name/$display->id/$type",
  509. 'attributes' => array('class' => array('views-ajax-link')),
  510. ),
  511. );
  512. return $links;
  513. }
  514. /**
  515. * Returns all contextual links for the main content part of the view.
  516. */
  517. function views_ui_view_preview_section_rows_links($view) {
  518. $links = array();
  519. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'filter', TRUE));
  520. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'field', TRUE));
  521. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'sort', TRUE));
  522. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'argument', TRUE));
  523. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'relationship', TRUE));
  524. return $links;
  525. }
  526. /**
  527. * Implments hook_ctools_plugin_directory().
  528. *
  529. * Views UI provides wizard plugins on behalf of core base tables.
  530. */
  531. function views_ui_ctools_plugin_directory($module, $plugin) {
  532. if ($module == 'views_ui' || ($module == 'ctools' && $plugin == 'export_ui')) {
  533. return 'plugins/' . $plugin;
  534. }
  535. }
  536. /**
  537. * Fetch metadata on a specific views ui wizard plugin.
  538. *
  539. * @param string $wizard_type
  540. * Name of a wizard, or name of a base table.
  541. *
  542. * @return array
  543. * An array with information about the requested wizard type.
  544. */
  545. function views_ui_get_wizard($wizard_type) {
  546. ctools_include('plugins');
  547. $wizard = ctools_get_plugins('views_ui', 'views_wizard', $wizard_type);
  548. // @todo - handle this via an alter hook instead.
  549. if (!$wizard) {
  550. // Must be a base table using the default wizard plugin.
  551. $base_tables = views_fetch_base_tables();
  552. if (!empty($base_tables[$wizard_type])) {
  553. $wizard = views_ui_views_wizard_defaults();
  554. $wizard['base_table'] = $wizard_type;
  555. $wizard['title'] = $base_tables[$wizard_type]['title'];
  556. }
  557. // The plugin is neither a base table nor an existing wizard.
  558. else {
  559. vpr('Views Wizard: @wizard does not exist. Be sure to implement hook_ctools_plugin_directory.', array('@wizard' => $wizard_type));
  560. }
  561. }
  562. return $wizard;
  563. }
  564. /**
  565. * Fetch metadata for all content_type plugins.
  566. *
  567. * @return array
  568. * An array of arrays with information about all available views wizards.
  569. */
  570. function views_ui_get_wizards() {
  571. ctools_include('plugins');
  572. $wizard_plugins = ctools_get_plugins('views_ui', 'views_wizard');
  573. $wizard_tables = array();
  574. foreach ($wizard_plugins as $info) {
  575. $wizard_tables[$info['base_table']] = TRUE;
  576. }
  577. $base_tables = views_fetch_base_tables();
  578. $default_wizard = views_ui_views_wizard_defaults();
  579. // Find base tables with no wizard.
  580. // @todo - handle this via an alter hook for plugins?
  581. foreach ($base_tables as $table => $info) {
  582. if (!isset($wizard_tables[$table])) {
  583. $wizard = $default_wizard;
  584. $wizard['title'] = $info['title'];
  585. $wizard['base_table'] = $table;
  586. $wizard_plugins[$table] = $wizard;
  587. }
  588. }
  589. return $wizard_plugins;
  590. }
  591. /**
  592. * Helper function to define the default values for a Views wizard plugin.
  593. *
  594. * @return array
  595. * An array of defaults for a views wizard.
  596. */
  597. function views_ui_views_wizard_defaults() {
  598. return array(
  599. // The children may, for example, be a different variant for each node type.
  600. 'get children' => NULL,
  601. 'get child' => NULL,
  602. // Title and base table must be populated. They are empty here just
  603. // so they are documented.
  604. 'title' => '',
  605. 'base_table' => NULL,
  606. // This is a callback that takes the wizard as argument and returns
  607. // an instantiazed Views UI form wizard object.
  608. 'get_instance' => 'views_ui_get_form_wizard_instance',
  609. 'form_wizard_class' => array(
  610. 'file' => 'views_ui_base_views_wizard',
  611. 'class' => 'ViewsUiBaseViewsWizard',
  612. ),
  613. );
  614. }
  615. /**
  616. * Inform CTools that the Views wizard plugin can have child plugins.
  617. */
  618. function views_ui_ctools_plugin_type() {
  619. return array(
  620. 'views_wizard' => array(
  621. 'child plugins' => TRUE,
  622. 'classes' => array(
  623. 'form_wizard_class',
  624. ),
  625. 'defaults' => views_ui_views_wizard_defaults(),
  626. ),
  627. );
  628. }
  629. /**
  630. * Get form wizard instance.
  631. */
  632. function views_ui_get_form_wizard_instance($wizard) {
  633. if (isset($wizard['form_wizard_class']['class'])) {
  634. $class = $wizard['form_wizard_class']['class'];
  635. return new $class($wizard);
  636. }
  637. else {
  638. return new ViewsUiBaseViewsWizard($wizard);
  639. }
  640. }
  641. /**
  642. * Implements hook_views_plugins_alter().
  643. */
  644. function views_ui_views_plugins_alter(&$plugins) {
  645. // Attach contextual links to each display plugin. The links will point to
  646. // paths underneath "admin/structure/views/view/{$view->name}" (i.e., paths
  647. // for editing and performing other contextual actions on the view).
  648. foreach ($plugins['display'] as &$display) {
  649. $display['contextual links']['views_ui'] = array(
  650. 'parent path' => 'admin/structure/views/view',
  651. 'argument properties' => array('name'),
  652. );
  653. }
  654. }
  655. /**
  656. * Implements hook_contextual_links_view_alter().
  657. */
  658. function views_ui_contextual_links_view_alter(&$element, $items) {
  659. // Remove contextual links from being rendered, when so desired, such as
  660. // within a View preview.
  661. if (views_ui_contextual_links_suppress()) {
  662. $element['#links'] = array();
  663. }
  664. // Append the display ID to the Views UI edit links, so that clicking on the
  665. // contextual link takes you directly to the correct display tab on the edit
  666. // screen.
  667. elseif (!empty($element['#links']['views-ui-edit']) && !empty($element['#element']['#views_contextual_links_info']['views_ui']['view_display_id'])) {
  668. $display_id = $element['#element']['#views_contextual_links_info']['views_ui']['view_display_id'];
  669. $element['#links']['views-ui-edit']['href'] .= '/' . $display_id;
  670. }
  671. }
  672. /**
  673. * Sets a static variable for controlling whether contextual links are rendered.
  674. *
  675. * @see views_ui_contextual_links_view_alter()
  676. */
  677. function views_ui_contextual_links_suppress($set = NULL) {
  678. $suppress = &drupal_static(__FUNCTION__);
  679. if (isset($set)) {
  680. $suppress = $set;
  681. }
  682. return $suppress;
  683. }
  684. /**
  685. * Increments the views_ui_contextual_links_suppress() static variable.
  686. *
  687. * When this function is added to the #pre_render of an element, and
  688. * 'views_ui_contextual_links_suppress_pop' is added to the #post_render of the
  689. * same element, then all contextual links within the element and its
  690. * descendants are suppressed from being rendered. This is used, for example,
  691. * during a View preview, when it is not desired for nodes in the Views result
  692. * to have contextual links.
  693. *
  694. * @see views_ui_contextual_links_suppress_pop()
  695. */
  696. function views_ui_contextual_links_suppress_push() {
  697. views_ui_contextual_links_suppress(((int) views_ui_contextual_links_suppress()) + 1);
  698. }
  699. /**
  700. * Decrements the views_ui_contextual_links_suppress() static variable.
  701. *
  702. * @see views_ui_contextual_links_suppress_push()
  703. */
  704. function views_ui_contextual_links_suppress_pop() {
  705. views_ui_contextual_links_suppress(((int) views_ui_contextual_links_suppress()) - 1);
  706. }
  707. /**
  708. * Menu callback.
  709. *
  710. * Handles AJAX form submissions similar to ajax_form_callback(), but can be
  711. * used for uncached forms.
  712. *
  713. * Ajax_form_callback(), the menu callback for the system/ajax path, requires
  714. * the form to be retrievable from the form cache, because it lacks a trusted
  715. * $form_id argument with which to call drupal_retrieve_form(). When AJAX is
  716. * wanted on a non-cacheable form, #ajax['path'] can be set to a path whose
  717. * menu router item's 'page callback' is this function, and whose
  718. * 'page arguments' is the form id, optionally followed by additional build
  719. * arguments, as expected by drupal_get_form().
  720. *
  721. * The same caution must be used when defining a hook_menu() entry with this
  722. * page callback as is used when defining a hook_menu() entry with the
  723. * 'drupal_get_form' page callback: a 'page arguments' must be specified with a
  724. * literal value as the first argument, because $form_id determines which form
  725. * builder function gets called, so must be safe from user tampering.
  726. *
  727. * @see drupal_get_form()
  728. * @see ajax_form_callback()
  729. * @see http://drupal.org/node/774876
  730. */
  731. function views_ui_ajax_get_form($form_id) {
  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. $args = func_get_args();
  740. array_shift($args);
  741. $form_state['build_info']['args'] = $args;
  742. $form = drupal_build_form($form_id, $form_state);
  743. // @see ajax_form_callback()
  744. if (!empty($form_state['triggering_element'])) {
  745. $callback = $form_state['triggering_element']['#ajax']['callback'];
  746. }
  747. if (!empty($callback) && function_exists($callback)) {
  748. return $callback($form, $form_state);
  749. }
  750. }
  751. /**
  752. * @todo move these when we can
  753. */
  754. /**
  755. * Helper function to get a list of paths assigned to a view.
  756. *
  757. * @param object $view
  758. * The view.
  759. *
  760. * @return array
  761. * An array of links to this view's display paths.
  762. */
  763. function _views_ui_get_paths($view) {
  764. $all_paths = array();
  765. if (empty($view->display)) {
  766. $all_paths[] = t('Edit this view to add a display.');
  767. }
  768. else {
  769. // Make sure all the handlers are set up.
  770. $view->init_display();
  771. foreach ($view->display as $display) {
  772. if (!empty($display->handler) && $display->handler->has_path()) {
  773. $one_path = $display->handler->get_option('path');
  774. if (empty($view->disabled) && strpos($one_path, '%') === FALSE) {
  775. // @codingStandardsIgnoreLine
  776. $all_paths[] = l('/' . $one_path, $one_path);
  777. }
  778. else {
  779. $all_paths[] = check_plain('/' . $one_path);
  780. }
  781. }
  782. }
  783. }
  784. return array_unique($all_paths);
  785. }
  786. /**
  787. * Helper function to get a list of displays included in a view.
  788. *
  789. * @param object $view
  790. * The view.
  791. *
  792. * @return array
  793. * An array of display types that this view includes.
  794. */
  795. function _views_ui_get_displays_list($view) {
  796. $displays = array();
  797. foreach ($view->display as $display) {
  798. if (!empty($display->handler->definition['admin'])) {
  799. $displays[$display->handler->definition['admin']] = TRUE;
  800. }
  801. }
  802. if ($displays) {
  803. ksort($displays);
  804. $displays = array_keys($displays);
  805. }
  806. return $displays;
  807. }
  808. /**
  809. * This is part of a patch to address a jQueryUI bug.
  810. *
  811. * The bug is responsible
  812. * for the inability to scroll a page when a modal dialog is active. If the
  813. * content of the dialog extends beyond the bottom of the viewport, the user is
  814. * only able to scroll with a mousewheel or up/down keyboard keys.
  815. *
  816. * @see http://bugs.jqueryui.com/ticket/4671
  817. * @see https://bugs.webkit.org/show_bug.cgi?id=19033
  818. * @see /js/jquery.ui.dialog.patch.js
  819. * @see /js/jquery.ui.dialog.min.js
  820. *
  821. * The javascript patch overwrites the $.ui.dialog.overlay.events object to
  822. * remove the mousedown, mouseup and click events from the list of events that
  823. * are bound in $.ui.dialog.overlay.create.
  824. */
  825. function views_ui_library_alter(&$libraries, $module) {
  826. if ($module == 'system' && isset($libraries['ui.dialog'])) {
  827. // Only apply the fix, if we don't have an up to date jQueryUI version.
  828. if (version_compare($libraries['ui.dialog']['version'], '1.7.2', '>=') && version_compare($libraries['ui.dialog']['version'], '1.10.0', '<')) {
  829. $libraries['ui.dialog']['js'][drupal_get_path('module', 'views') . '/js/jquery.ui.dialog.patch.js'] = array();
  830. }
  831. }
  832. }
  833. /**
  834. * Truncate strings to a set length and provide a ... if they truncated.
  835. *
  836. * This is often used in the UI to ensure long strings fit.
  837. */
  838. function views_ui_truncate($string, $length) {
  839. if (drupal_strlen($string) > $length) {
  840. $string = drupal_substr($string, 0, $length);
  841. $string .= '...';
  842. }
  843. return $string;
  844. }