views_ui.module 28 KB

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