views_ui.module 28 KB

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