views.module 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828
  1. <?php
  2. /**
  3. * @file
  4. * Primarily Drupal hooks and global API functions to manipulate views.
  5. *
  6. * This is the main module file for Views. The main entry points into
  7. * this module are views_page() and views_block(), where it handles
  8. * incoming page and block requests.
  9. */
  10. /**
  11. * Implements hook_help().
  12. */
  13. function views_help($path, $arg) {
  14. switch ($path) {
  15. case 'admin/help#views':
  16. $output = '';
  17. $output .= '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('The Views module provides a back end to fetch information from content, user accounts, taxonomy terms, and other entities from the database and present it to the user as a grid, HTML list, table, unformatted list, etc. The resulting displays are known generally as views.') . '</p>';
  19. $output .= '<p>' . t('For more information, see the <a href="@views" target="blank">online documentation for the Views</a>.', array('@views' => 'https://www.drupal.org/documentation/modules/views')) . '</p>';
  20. $output .= '<p>' . t('In order to create and modify your own views using the administration and configuration user interface, you will need to enable either the Views UI module in core or a contributed module that provides a user interface for Views. See the <a href="/admin/structure/views">Views UI module help page</a> for more information.') . '</p>';
  21. $output .= '<h3>' . t('Uses') . '</h3>';
  22. $output .= '<dl>';
  23. $output .= '<dt>' . t('Adding functionality to administrative pages') . '</dt>';
  24. $output .= '<dd>' . t('The Views module adds functionality to some core administration pages. For example, <em>admin/content</em> uses Views to filter and sort content. With Views uninstalled, <em>admin/content</em> is more limited.') . '</dd>';
  25. $output .= '<dt>' . t('Expanding Views functionality') . '</dt>';
  26. $output .= '<dd>' . t('Contributed projects that support the Views module can be found in the <a href="@views-related" target="blank">online documentation for Views-related contributed modules.</a>.', array('@views-related' => 'https://www.drupal.org/documentation/modules/views/add-ons')) . '</dd>';
  27. $output .= '<dt>' . t('Improving table accessibility') . '</dt>';
  28. $output .= '<dd>' . t('Views tables include semantic markup to improve accessibility. Data cells are automatically associated with header cells through id and header attributes. To improve the accessibility of your tables you can add descriptive elements within the Views table settings. The caption element can introduce context for a table, making it easier to understand. The summary element can provide an overview of how the data has been organized and how to navigate the table. Both the caption and summary are visible by default and also implemented according to HTML5 guidelines.') . '</dd>';
  29. return $output;
  30. }
  31. }
  32. /**
  33. * Advertise the current views api version.
  34. */
  35. function views_api_version() {
  36. return '3.0';
  37. }
  38. /**
  39. * Implements hook_forms().
  40. *
  41. * To provide distinct form IDs for Views forms, the View name and
  42. * specific display name are appended to the base ID,
  43. * views_form_views_form. When such a form is built or submitted, this
  44. * function will return the proper callback function to use for the given form.
  45. */
  46. function views_forms($form_id, $args) {
  47. if (strpos($form_id, 'views_form_') === 0) {
  48. return array(
  49. $form_id => array(
  50. 'callback' => 'views_form',
  51. ),
  52. );
  53. }
  54. }
  55. /**
  56. * Returns a form ID for a Views form using the name and display of the View.
  57. */
  58. function views_form_id($view) {
  59. $parts = array(
  60. 'views_form',
  61. $view->name,
  62. $view->current_display,
  63. );
  64. return implode('_', $parts);
  65. }
  66. /**
  67. * Views will not load plugins advertising a version older than this.
  68. */
  69. function views_api_minimum_version() {
  70. return '2';
  71. }
  72. /**
  73. * Implements hook_theme().
  74. *
  75. * Register views theming functions.
  76. */
  77. function views_theme($existing, $type, $theme, $path) {
  78. $path = drupal_get_path('module', 'views');
  79. ctools_include('theme', 'views', 'theme');
  80. // Some quasi clever array merging here.
  81. $base = array(
  82. 'file' => 'theme.inc',
  83. 'path' => $path . '/theme',
  84. );
  85. // Our extra version of pager from pager.inc.
  86. $hooks['views_mini_pager'] = $base + array(
  87. 'variables' => array(
  88. 'tags' => array(),
  89. 'element' => 0,
  90. 'parameters' => array(),
  91. ),
  92. 'pattern' => 'views_mini_pager__',
  93. );
  94. $variables = array(
  95. // For displays, we pass in a dummy array as the first parameter, since
  96. // $view is an object but the core contextual_preprocess() function only
  97. // attaches contextual links when the primary theme argument is an array.
  98. 'display' => array('view_array' => array(), 'view' => NULL),
  99. 'style' => array(
  100. 'view' => NULL,
  101. 'options' => NULL,
  102. 'rows' => NULL,
  103. 'title' => NULL,
  104. ),
  105. 'row' => array(
  106. 'view' => NULL,
  107. 'options' => NULL,
  108. 'row' => NULL,
  109. 'field_alias' => NULL,
  110. ),
  111. 'exposed_form' => array('view' => NULL, 'options' => NULL),
  112. 'pager' => array(
  113. 'view' => NULL,
  114. 'options' => NULL,
  115. 'tags' => array(),
  116. 'quantity' => 10,
  117. 'element' => 0,
  118. 'parameters' => array(),
  119. ),
  120. );
  121. // Default view themes.
  122. $hooks['views_view_field'] = $base + array(
  123. 'pattern' => 'views_view_field__',
  124. 'variables' => array('view' => NULL, 'field' => NULL, 'row' => NULL),
  125. );
  126. $hooks['views_view_grouping'] = $base + array(
  127. 'pattern' => 'views_view_grouping__',
  128. 'variables' => array(
  129. 'view' => NULL,
  130. 'grouping' => NULL,
  131. 'grouping_level' => NULL,
  132. 'rows' => NULL,
  133. 'title' => NULL,
  134. ),
  135. );
  136. $plugins = views_fetch_plugin_data();
  137. // Register theme functions for all style plugins.
  138. foreach ($plugins as $type => $info) {
  139. foreach ($info as $plugin => $def) {
  140. if (isset($def['theme']) && (!isset($def['register theme']) || !empty($def['register theme']))) {
  141. $hooks[$def['theme']] = array(
  142. 'pattern' => $def['theme'] . '__',
  143. 'file' => $def['theme file'],
  144. 'path' => $def['theme path'],
  145. 'variables' => $variables[$type],
  146. );
  147. $include = DRUPAL_ROOT . '/' . $def['theme path'] . '/' . $def['theme file'];
  148. if (file_exists($include)) {
  149. require_once $include;
  150. }
  151. if (!function_exists('theme_' . $def['theme'])) {
  152. $hooks[$def['theme']]['template'] = drupal_clean_css_identifier($def['theme']);
  153. }
  154. }
  155. if (isset($def['additional themes'])) {
  156. foreach ($def['additional themes'] as $theme => $theme_type) {
  157. if (empty($theme_type)) {
  158. $theme = $theme_type;
  159. $theme_type = $type;
  160. }
  161. $hooks[$theme] = array(
  162. 'pattern' => $theme . '__',
  163. 'file' => $def['theme file'],
  164. 'path' => $def['theme path'],
  165. 'variables' => $variables[$theme_type],
  166. );
  167. if (!function_exists('theme_' . $theme)) {
  168. $hooks[$theme]['template'] = drupal_clean_css_identifier($theme);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. $hooks['views_form_views_form'] = $base + array(
  175. 'render element' => 'form',
  176. );
  177. $hooks['views_exposed_form'] = $base + array(
  178. 'template' => 'views-exposed-form',
  179. 'pattern' => 'views_exposed_form__',
  180. 'render element' => 'form',
  181. );
  182. $hooks['views_more'] = $base + array(
  183. 'template' => 'views-more',
  184. 'pattern' => 'views_more__',
  185. 'variables' => array(
  186. 'more_url' => NULL,
  187. 'link_text' => 'more',
  188. 'view' => NULL,
  189. ),
  190. );
  191. // Add theme suggestions which are part of modules.
  192. foreach (views_get_module_apis() as $info) {
  193. if (isset($info['template path'])) {
  194. $hooks += _views_find_module_templates($hooks, $info['template path']);
  195. }
  196. }
  197. return $hooks;
  198. }
  199. /**
  200. * Scans a directory of a module for template files.
  201. *
  202. * @param array $cache
  203. * The existing cache of theme hooks to test against.
  204. * @param string $path
  205. * The path to search.
  206. *
  207. * @see drupal_find_theme_templates()
  208. */
  209. function _views_find_module_templates($cache, $path) {
  210. $templates = array();
  211. $regex = '/\.tpl\.php$/';
  212. // Because drupal_system_listing works the way it does, we check for real
  213. // templates separately from checking for patterns.
  214. $files = drupal_system_listing($regex, $path, 'name', 0);
  215. foreach ($files as $template => $file) {
  216. // Chop off the remaining extensions if there are any. $template already
  217. // has the rightmost extension removed, but there might still be more,
  218. // such as with .tpl.php, which still has .tpl in $template at this point.
  219. if (($pos = strpos($template, '.')) !== FALSE) {
  220. $template = substr($template, 0, $pos);
  221. }
  222. // Transform - in filenames to _ to match function naming scheme
  223. // for the purposes of searching.
  224. $hook = strtr($template, '-', '_');
  225. if (isset($cache[$hook])) {
  226. $templates[$hook] = array(
  227. 'template' => $template,
  228. 'path' => dirname($file->filename),
  229. 'includes' => isset($cache[$hook]['includes']) ? $cache[$hook]['includes'] : NULL,
  230. );
  231. }
  232. // Ensure that the pattern is maintained from base themes to its sub-themes.
  233. // Each sub-theme will have their templates scanned so the pattern must be
  234. // held for subsequent runs.
  235. if (isset($cache[$hook]['pattern'])) {
  236. $templates[$hook]['pattern'] = $cache[$hook]['pattern'];
  237. }
  238. }
  239. $patterns = array_keys($files);
  240. foreach ($cache as $hook => $info) {
  241. if (!empty($info['pattern'])) {
  242. // Transform _ in pattern to - to match file naming scheme
  243. // for the purposes of searching.
  244. $pattern = strtr($info['pattern'], '_', '-');
  245. $matches = preg_grep('/^' . $pattern . '/', $patterns);
  246. if ($matches) {
  247. foreach ($matches as $match) {
  248. $file = substr($match, 0, strpos($match, '.'));
  249. // Put the underscores back in for the hook name and register this
  250. // pattern.
  251. $templates[strtr($file, '-', '_')] = array(
  252. 'template' => $file,
  253. 'path' => dirname($files[$match]->uri),
  254. 'variables' => isset($info['variables']) ? $info['variables'] : NULL,
  255. 'render element' => isset($info['render element']) ? $info['render element'] : NULL,
  256. 'base hook' => $hook,
  257. 'includes' => isset($info['includes']) ? $info['includes'] : NULL,
  258. );
  259. }
  260. }
  261. }
  262. }
  263. return $templates;
  264. }
  265. /**
  266. * Returns a list of plugins and metadata about them.
  267. *
  268. * @return array
  269. * An array keyed by PLUGIN_TYPE:PLUGIN_NAME, like 'display:page' or
  270. * 'pager:full', containing an array with the following keys:
  271. * - title: The plugin's title.
  272. * - type: The plugin type.
  273. * - module: The module providing the plugin.
  274. * - views: An array of enabled Views that are currently using this plugin,
  275. * keyed by machine name.
  276. */
  277. function views_plugin_list() {
  278. $plugin_data = views_fetch_plugin_data();
  279. $plugins = array();
  280. foreach (views_get_enabled_views() as $view) {
  281. foreach ($view->display as $display_id => $display) {
  282. foreach ($plugin_data as $type => $info) {
  283. if ($type == 'display' && isset($display->display_plugin)) {
  284. $name = $display->display_plugin;
  285. }
  286. elseif (isset($display->display_options["{$type}_plugin"])) {
  287. $name = $display->display_options["{$type}_plugin"];
  288. }
  289. elseif (isset($display->display_options[$type]['type'])) {
  290. $name = $display->display_options[$type]['type'];
  291. }
  292. else {
  293. continue;
  294. }
  295. // Key first by the plugin type, then the name.
  296. $key = $type . ':' . $name;
  297. // Add info for this plugin.
  298. if (!isset($plugins[$key])) {
  299. $plugins[$key] = array(
  300. 'type' => $type,
  301. 'title' => check_plain($info[$name]['title']),
  302. 'module' => check_plain($info[$name]['module']),
  303. 'views' => array(),
  304. );
  305. }
  306. // Add this view to the list for this plugin.
  307. $plugins[$key]['views'][$view->name] = $view->name;
  308. }
  309. }
  310. }
  311. return $plugins;
  312. }
  313. /**
  314. * Preprocess a node.
  315. *
  316. * A theme preprocess function to automatically allow view-based node
  317. * templates if called from a view.
  318. *
  319. * The 'modules/node.views.inc' file is a better place for this, but
  320. * we haven't got a chance to load that file before Drupal builds the
  321. * node portion of the theme registry.
  322. */
  323. function views_preprocess_node(&$vars) {
  324. // The 'view' attribute of the node is added in views_preprocess_node()
  325. if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) {
  326. $vars['view'] = $vars['node']->view;
  327. $vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->name;
  328. if (!empty($vars['node']->view->current_display)) {
  329. $vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->name . '__' . $vars['node']->view->current_display;
  330. // If a node is being rendered in a view, and the view does not have a
  331. // path, prevent drupal from accidentally setting the $page variable.
  332. if ($vars['page'] && $vars['view_mode'] == 'full' && !$vars['view']->display_handler->has_path()) {
  333. $vars['page'] = FALSE;
  334. }
  335. }
  336. }
  337. // Allow to alter comments and links based on the settings in the row plugin.
  338. if (!empty($vars['view']->style_plugin->row_plugin) && get_class($vars['view']->style_plugin->row_plugin) == 'views_plugin_row_node_view') {
  339. node_row_node_view_preprocess_node($vars);
  340. }
  341. }
  342. /**
  343. * Preprocess a comment.
  344. *
  345. * A theme preprocess function to automatically allow view-based node
  346. * templates if called from a view.
  347. */
  348. function views_preprocess_comment(&$vars) {
  349. // The 'view' attribute of the node is added in
  350. // template_preprocess_views_view_row_comment().
  351. if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) {
  352. $vars['view'] = &$vars['node']->view;
  353. $vars['theme_hook_suggestions'][] = 'comment__view__' . $vars['node']->view->name;
  354. if (!empty($vars['node']->view->current_display)) {
  355. $vars['theme_hook_suggestions'][] = 'comment__view__' . $vars['node']->view->name . '__' . $vars['node']->view->current_display;
  356. }
  357. }
  358. }
  359. /**
  360. * Implements hook_permission().
  361. */
  362. function views_permission() {
  363. return array(
  364. 'administer views' => array(
  365. 'title' => t('Administer views'),
  366. 'description' => t('Access the views administration pages.'),
  367. 'restrict access' => TRUE,
  368. ),
  369. 'access all views' => array(
  370. 'title' => t('Bypass views access control'),
  371. 'description' => t('Bypass access control when accessing views.'),
  372. 'restrict access' => TRUE,
  373. ),
  374. );
  375. }
  376. /**
  377. * Implements hook_menu().
  378. */
  379. function views_menu() {
  380. $items = array();
  381. $items['views/ajax'] = array(
  382. 'title' => 'Views',
  383. 'page callback' => 'views_ajax',
  384. 'theme callback' => 'ajax_base_page_theme',
  385. 'delivery callback' => 'ajax_deliver',
  386. 'access callback' => TRUE,
  387. 'description' => 'Ajax callback for view loading.',
  388. 'type' => MENU_CALLBACK,
  389. 'file' => 'includes/ajax.inc',
  390. );
  391. // Path is not admin/structure/views due to menu complications with the
  392. // wildcards from the generic ajax callback.
  393. $items['admin/views/ajax/autocomplete/user'] = array(
  394. 'page callback' => 'views_ajax_autocomplete_user',
  395. 'theme callback' => 'ajax_base_page_theme',
  396. 'access callback' => 'user_access',
  397. 'access arguments' => array('access user profiles'),
  398. 'type' => MENU_CALLBACK,
  399. 'file' => 'includes/ajax.inc',
  400. );
  401. // Define another taxonomy autocomplete because the default one of drupal
  402. // does not support a vid a argument anymore.
  403. $items['admin/views/ajax/autocomplete/taxonomy'] = array(
  404. 'page callback' => 'views_ajax_autocomplete_taxonomy',
  405. 'theme callback' => 'ajax_base_page_theme',
  406. 'access callback' => 'user_access',
  407. 'access arguments' => array('access content'),
  408. 'type' => MENU_CALLBACK,
  409. 'file' => 'includes/ajax.inc',
  410. );
  411. return $items;
  412. }
  413. /**
  414. * Implements hook_menu_alter().
  415. */
  416. function views_menu_alter(&$callbacks) {
  417. $our_paths = array();
  418. $views = views_get_applicable_views('uses hook menu');
  419. foreach ($views as $data) {
  420. list($view, $display_id) = $data;
  421. $result = $view->execute_hook_menu($display_id, $callbacks);
  422. if (is_array($result)) {
  423. // The menu system doesn't support having two otherwise identical paths
  424. // with different placeholders. So we want to remove the existing items
  425. // from the menu whose paths would conflict with ours. First, we must find
  426. // any existing menu items that may conflict. We use a regular expression
  427. // because we don't know what placeholders they might use. Note that we
  428. // first construct the regex itself by replacing %views_arg in the display
  429. // path, then we use this constructed regex (which will be something like
  430. // '#^(foo/%[^/]*/bar)$#') to search through the existing paths.
  431. $regex = '#^(' . preg_replace('#%views_arg#', '%[^/]*', implode('|', array_keys($result))) . ')$#';
  432. $matches = preg_grep($regex, array_keys($callbacks));
  433. // Remove any conflicting items that were found.
  434. foreach ($matches as $path) {
  435. // Don't remove the paths we just added!
  436. if (!isset($our_paths[$path])) {
  437. unset($callbacks[$path]);
  438. }
  439. }
  440. foreach ($result as $path => $item) {
  441. if (!isset($callbacks[$path])) {
  442. // Add a new item, possibly replacing (and thus effectively
  443. // overriding) one that we removed above.
  444. $callbacks[$path] = $item;
  445. }
  446. else {
  447. // This item already exists, so it must be one that we added.
  448. // We change the various callback arguments to pass an array
  449. // of possible display IDs instead of a single ID.
  450. $callbacks[$path]['page arguments'][1] = (array) $callbacks[$path]['page arguments'][1];
  451. $callbacks[$path]['page arguments'][1][] = $display_id;
  452. $callbacks[$path]['access arguments'][] = $item['access arguments'][0];
  453. $callbacks[$path]['load arguments'][1] = (array) $callbacks[$path]['load arguments'][1];
  454. $callbacks[$path]['load arguments'][1][] = $display_id;
  455. }
  456. $our_paths[$path] = TRUE;
  457. }
  458. }
  459. }
  460. // Save memory: Destroy those views.
  461. foreach ($views as $data) {
  462. list($view, $display_id) = $data;
  463. $view->destroy();
  464. }
  465. }
  466. /**
  467. * Load a views argument.
  468. *
  469. * Helper function for menu loading. This will automatically be
  470. * called in order to 'load' a views argument; primarily it
  471. * will be used to perform validation.
  472. *
  473. * @param string $value
  474. * The actual value passed.
  475. * @param string $name
  476. * The name of the view. This needs to be specified in the 'load function'
  477. * of the menu entry.
  478. * @param string $display_id
  479. * The display id that will be loaded for this menu item.
  480. * @param int $index
  481. * The menu argument index. This counts from 1.
  482. */
  483. function views_arg_load($value, $name, $display_id, $index) {
  484. static $views = array();
  485. $display_ids = is_array($display_id) ? $display_id : array($display_id);
  486. $display_id = reset($display_ids);
  487. foreach ($display_ids as $id) {
  488. // Make sure we haven't already loaded this views argument for a similar
  489. // menu item elsewhere. Since access is always checked for the current user,
  490. // we are sure that the static cache contains valid entries.
  491. $key = $name . ':' . $id . ':' . $value . ':' . $index;
  492. if (isset($views[$key])) {
  493. return $views[$key];
  494. }
  495. // Lazy load the view object to avoid unnecessary work.
  496. if (!isset($view)) {
  497. $view = views_get_view($name);
  498. }
  499. // Pick the first display we have access to.
  500. if ($view && count($display_ids) > 1 && $view->access($id)) {
  501. $display_id = $id;
  502. break;
  503. }
  504. }
  505. if ($view) {
  506. $view->set_display($display_id);
  507. $view->init_handlers();
  508. $ids = array_keys($view->argument);
  509. $indexes = array();
  510. $path = explode('/', $view->get_path());
  511. foreach ($path as $id => $piece) {
  512. if ($piece == '%' && !empty($ids)) {
  513. $indexes[$id] = array_shift($ids);
  514. }
  515. }
  516. if (isset($indexes[$index])) {
  517. if (isset($view->argument[$indexes[$index]])) {
  518. $arg = $view->argument[$indexes[$index]]->validate_argument($value) ? $value : FALSE;
  519. $view->destroy();
  520. // Store the output in case we load this same menu item again.
  521. $views[$key] = $arg;
  522. return $arg;
  523. }
  524. }
  525. $view->destroy();
  526. }
  527. }
  528. /**
  529. * Page callback: Displays a page view, given a name and display id.
  530. *
  531. * @param string $name
  532. * The name of a view.
  533. * @param string $display_id
  534. * The display id of a view.
  535. *
  536. * @return string|int
  537. * Either the HTML of a fully-executed view, or MENU_NOT_FOUND.
  538. */
  539. function views_page($name, $display_id) {
  540. $args = func_get_args();
  541. // Remove $name and $display_id from the arguments.
  542. array_shift($args);
  543. array_shift($args);
  544. // Load the view and render it.
  545. if ($view = views_get_view($name)) {
  546. return $view->execute_display($display_id, $args);
  547. }
  548. // Fallback; if we get here no view was found or handler was not valid.
  549. return MENU_NOT_FOUND;
  550. }
  551. /**
  552. * Implements hook_page_alter().
  553. */
  554. function views_page_alter(&$page) {
  555. // If the main content of this page contains a view, attach its contextual
  556. // links to the overall page array. This allows them to be rendered directly
  557. // next to the page title.
  558. $view = views_get_page_view();
  559. if (!empty($view)) {
  560. // If a module is still putting in the display like we used to, catch that.
  561. if (is_subclass_of($view, 'views_plugin_display')) {
  562. $view = $view->view;
  563. }
  564. views_add_contextual_links($page, 'page', $view, $view->current_display);
  565. }
  566. }
  567. /**
  568. * Implements MODULE_preprocess_HOOK() for html.tpl.php.
  569. */
  570. function views_preprocess_html(&$variables) {
  571. // If the page contains a view as its main content, contextual links may have
  572. // been attached to the page as a whole; for example, by views_page_alter().
  573. // This allows them to be associated with the page and rendered by default
  574. // next to the page title (which we want). However, it also causes the
  575. // Contextual Links module to treat the wrapper for the entire page (i.e.,
  576. // the <body> tag) as the HTML element that these contextual links are
  577. // associated with. This we don't want; for better visual highlighting, we
  578. // prefer a smaller region to be chosen. The region we prefer differs from
  579. // theme to theme and depends on the details of the theme's markup in
  580. // page.tpl.php, so we can only find it using JavaScript. We therefore remove
  581. // the "contextual-links-region" class from the <body> tag here and add
  582. // JavaScript that will insert it back in the correct place.
  583. if (!empty($variables['page']['#views_contextual_links_info'])) {
  584. $key = array_search('contextual-links-region', $variables['classes_array']);
  585. if ($key !== FALSE) {
  586. $variables['classes_array'] = array_diff($variables['classes_array'], array('contextual-links-region'));
  587. // Add the JavaScript, with a group and weight such that it will run
  588. // before modules/contextual/contextual.js.
  589. drupal_add_js(drupal_get_path('module', 'views') . '/js/views-contextual.js', array('group' => JS_LIBRARY, 'weight' => -1));
  590. }
  591. }
  592. }
  593. /**
  594. * Implements hook_preprocess_HOOK() for page.tpl.php.
  595. */
  596. function views_preprocess_page(&$variables) {
  597. // If the page contains a view as its main content, contextual links may have
  598. // been attached to the page as a whole; for example, by views_page_alter().
  599. // This allows them to be associated with the page and rendered by default
  600. // next to the page title (which we want). However, it also causes the
  601. // Contextual Links module to treat the wrapper for the entire page (i.e.,
  602. // the <body> tag) as the HTML element that these contextual links are
  603. // associated with. This we don't want; for better visual highlighting, we
  604. // prefer a smaller region to be chosen. The region we prefer differs from
  605. // theme to theme and depends on the details of the theme's markup in
  606. // page.tpl.php, so we can only find it using JavaScript. We therefore remove
  607. // the "contextual-links-region" class from the <body> tag here and add
  608. // JavaScript that will insert it back in the correct place.
  609. if (!empty($variables['page']['#views_contextual_links_info'])) {
  610. $variables['classes_array'] = array_diff($variables['classes_array'], array('contextual-links-region'));
  611. }
  612. }
  613. /**
  614. * Implements hook_contextual_links_view_alter().
  615. */
  616. function views_contextual_links_view_alter(&$element, $items) {
  617. // If we are rendering views-related contextual links attached to the overall
  618. // page array, add a class to the list of contextual links. This will be used
  619. // by the JavaScript added in views_preprocess_html().
  620. if (!empty($element['#element']['#views_contextual_links_info']) && !empty($element['#element']['#type']) && $element['#element']['#type'] == 'page') {
  621. $element['#attributes']['class'][] = 'views-contextual-links-page';
  622. }
  623. }
  624. /**
  625. * Implements hook_block_info().
  626. */
  627. function views_block_info() {
  628. // Try to avoid instantiating all the views just to get the blocks info.
  629. views_include('cache');
  630. $cache = views_cache_get('views_block_items', TRUE);
  631. if ($cache && is_array($cache->data)) {
  632. return $cache->data;
  633. }
  634. $items = array();
  635. $views = views_get_all_views();
  636. foreach ($views as $view) {
  637. // Disabled views get nothing.
  638. if (!empty($view->disabled)) {
  639. continue;
  640. }
  641. $view->init_display();
  642. foreach ($view->display as $display_id => $display) {
  643. if (isset($display->handler) && !empty($display->handler->definition['uses hook block'])) {
  644. $result = $display->handler->execute_hook_block_list();
  645. if (is_array($result)) {
  646. $items = array_merge($items, $result);
  647. }
  648. }
  649. if (isset($display->handler) && $display->handler->get_option('exposed_block')) {
  650. $result = $display->handler->get_special_blocks();
  651. if (is_array($result)) {
  652. $items = array_merge($items, $result);
  653. }
  654. }
  655. }
  656. }
  657. // block.module has a delta length limit of 32, but our deltas can
  658. // unfortunately be longer because view names can be 32 and display IDs
  659. // can also be 32. So for very long deltas, change to md5 hashes.
  660. $hashes = array();
  661. // Get the keys because we're modifying the array and we don't want to
  662. // confuse PHP too much.
  663. $keys = array_keys($items);
  664. foreach ($keys as $delta) {
  665. if (strlen($delta) >= 32) {
  666. $hash = md5($delta);
  667. $hashes[$hash] = $delta;
  668. $items[$hash] = $items[$delta];
  669. unset($items[$delta]);
  670. }
  671. }
  672. // Only save hashes if they have changed.
  673. $old_hashes = variable_get('views_block_hashes', array());
  674. if ($hashes != $old_hashes) {
  675. variable_set('views_block_hashes', $hashes);
  676. }
  677. // Save memory: Destroy those views.
  678. foreach ($views as $view) {
  679. $view->destroy();
  680. }
  681. views_cache_set('views_block_items', $items, TRUE);
  682. return $items;
  683. }
  684. /**
  685. * Implements hook_block_configure().
  686. */
  687. function views_block_configure($delta = '') {
  688. // If there's no Views UI module there's nothing to link to.
  689. if (!module_exists('views_ui')) {
  690. return array();
  691. }
  692. // If the user doesn't have access to edit the view then don't bother with
  693. // anything else.
  694. if (!user_access('administer views')) {
  695. return array();
  696. }
  697. // If this is 32, this should be an md5 hash.
  698. if (strlen($delta) == 32) {
  699. $hashes = variable_get('views_block_hashes', array());
  700. if (!empty($hashes[$delta])) {
  701. $delta = $hashes[$delta];
  702. }
  703. }
  704. // Some added precaution in case the delta is missing values.
  705. list($view_name, $display_id) = explode('-', $delta, 2) + array('', '');
  706. // If the view name or display ID can't be found, there's something wrong.
  707. if ($view_name === '' || $display_id === '') {
  708. return array();
  709. }
  710. $view = views_get_view($view_name);
  711. if (empty($view)) {
  712. return array();
  713. }
  714. if (!isset($view->display[$display_id])) {
  715. return array();
  716. }
  717. /** @var \views_display $display */
  718. $display = $view->display[$display_id];
  719. $view_label = $view->get_human_name();
  720. $display_label = $display->display_title;
  721. $path = "admin/structure/views/view/$view_name/edit/$display_id";
  722. return array(
  723. 'fieldset' => array(
  724. '#type' => 'fieldset',
  725. '#title' => t('Configure this views display'),
  726. 'content' => array(
  727. '#markup' => l($view_label . ' - ' . $display_label, $path),
  728. ),
  729. ),
  730. );
  731. }
  732. /**
  733. * Implements hook_block_view().
  734. */
  735. function views_block_view($delta) {
  736. // If this is 32, this should be an md5 hash.
  737. if (strlen($delta) == 32) {
  738. $hashes = variable_get('views_block_hashes', array());
  739. if (!empty($hashes[$delta])) {
  740. $delta = $hashes[$delta];
  741. }
  742. }
  743. // This indicates it's a special one.
  744. if (substr($delta, 0, 1) == '-') {
  745. list($nothing, $type, $name, $display_id) = explode('-', $delta);
  746. // Put the - back on.
  747. $type = '-' . $type;
  748. if ($view = views_get_view($name)) {
  749. if ($view->access($display_id)) {
  750. $view->set_display($display_id);
  751. if (isset($view->display_handler)) {
  752. $output = $view->display_handler->view_special_blocks($type);
  753. // Before returning the block output, convert it to a renderable
  754. // array with contextual links.
  755. views_add_block_contextual_links($output, $view, $display_id, 'special_block_' . $type);
  756. $view->destroy();
  757. return $output;
  758. }
  759. }
  760. $view->destroy();
  761. }
  762. }
  763. // If the delta doesn't contain valid data return nothing.
  764. $explode = explode('-', $delta);
  765. if (count($explode) != 2) {
  766. return;
  767. }
  768. list($name, $display_id) = $explode;
  769. // Load the view.
  770. if ($view = views_get_view($name)) {
  771. if ($view->access($display_id)) {
  772. $output = $view->execute_display($display_id);
  773. // Before returning the block output, convert it to a renderable array
  774. // with contextual links.
  775. views_add_block_contextual_links($output, $view, $display_id);
  776. $view->destroy();
  777. return $output;
  778. }
  779. $view->destroy();
  780. }
  781. }
  782. /**
  783. * Converts Views block content to a renderable array with contextual links.
  784. *
  785. * @param array $block
  786. * An array representing the block, with the same structure as the return
  787. * value of hook_block_view(). This will be modified so as to force
  788. * $block['content'] to be a renderable array, containing the optional
  789. * '#contextual_links' property (if there are any contextual links associated
  790. * with the block).
  791. * @param object $view
  792. * The view that was used to generate the block content.
  793. * @param string $display_id
  794. * The ID of the display within the view that was used to generate the block
  795. * content.
  796. * @param string $block_type
  797. * The type of the block. If it's block it's a regular views display,
  798. * but 'special_block_-exp' exist as well.
  799. */
  800. function views_add_block_contextual_links(&$block, $view, $display_id, $block_type = 'block') {
  801. // Do not add contextual links to an empty block.
  802. if (!empty($block['content'])) {
  803. // Contextual links only work on blocks whose content is a renderable
  804. // array, so if the block contains a string of already-rendered markup,
  805. // convert it to an array.
  806. if (is_string($block['content'])) {
  807. $block['content'] = array('#markup' => $block['content']);
  808. }
  809. // Add the contextual links.
  810. views_add_contextual_links($block['content'], $block_type, $view, $display_id);
  811. }
  812. }
  813. /**
  814. * Adds contextual links associated with a view display to a renderable array.
  815. *
  816. * This function should be called when a view is being rendered in a particular
  817. * location and you want to attach the appropriate contextual links (e.g.,
  818. * links for editing the view) to it.
  819. *
  820. * The function operates by checking the view's display plugin to see if it has
  821. * defined any contextual links that are intended to be displayed in the
  822. * requested location; if so, it attaches them. The contextual links intended
  823. * for a particular location are defined by the 'contextual links' and
  824. * 'contextual links locations' properties in hook_views_plugins() and
  825. * hook_views_plugins_alter(); as a result, these hook implementations have
  826. * full control over where and how contextual links are rendered for each
  827. * display.
  828. *
  829. * In addition to attaching the contextual links to the passed-in array (via
  830. * the standard #contextual_links property), this function also attaches
  831. * additional information via the #views_contextual_links_info property. This
  832. * stores an array whose keys are the names of each module that provided
  833. * views-related contextual links (same as the keys of the #contextual_links
  834. * array itself) and whose values are themselves arrays whose keys ('location',
  835. * 'view_name', and 'view_display_id') store the location, name of the view,
  836. * and display ID that were passed in to this function. This allows you to
  837. * access information about the contextual links and how they were generated in
  838. * a variety of contexts where you might be manipulating the renderable array
  839. * later on (for example, alter hooks which run later during the same page
  840. * request).
  841. *
  842. * @param array $render_element
  843. * The renderable array to which contextual links will be added. This array
  844. * should be suitable for passing in to drupal_render() and will normally
  845. * contain a representation of the view display whose contextual links are
  846. * being requested.
  847. * @param string $location
  848. * The location in which the calling function intends to render the view and
  849. * its contextual links. The core system supports three options for this
  850. * parameter:
  851. * - 'block': Used when rendering a block which contains a view. This
  852. * retrieves any contextual links intended to be attached to the block
  853. * itself.
  854. * - 'page': Used when rendering the main content of a page which contains a
  855. * view. This retrieves any contextual links intended to be attached to the
  856. * page itself (for example, links which are displayed directly next to the
  857. * page title).
  858. * - 'view': Used when rendering the view itself, in any context. This
  859. * retrieves any contextual links intended to be attached directly to the
  860. * view.
  861. * If you are rendering a view and its contextual links in another location,
  862. * you can pass in a different value for this parameter. However, you will
  863. * also need to use hook_views_plugins() or hook_views_plugins_alter() to
  864. * declare, via the 'contextual links locations' array key, which view
  865. * displays support having their contextual links rendered in the location
  866. * you have defined.
  867. * @param object $view
  868. * The view whose contextual links will be added.
  869. * @param string $display_id
  870. * The ID of the display within $view whose contextual links will be added.
  871. *
  872. * @see hook_views_plugins()
  873. * @see views_block_view()
  874. * @see views_page_alter()
  875. * @see template_preprocess_views_view()
  876. */
  877. function views_add_contextual_links(&$render_element, $location, $view, $display_id) {
  878. // Do not do anything if the view is configured to hide its administrative
  879. // links.
  880. if (empty($view->hide_admin_links)) {
  881. // Also do not do anything if the display plugin has not defined any
  882. // contextual links that are intended to be displayed in the requested
  883. // location.
  884. $plugin = views_fetch_plugin_data('display', $view->display[$display_id]->display_plugin);
  885. // If contextual links locations are not set, provide a sane default. (To
  886. // avoid displaying any contextual links at all, a display plugin can still
  887. // set 'contextual links locations' to, e.g., an empty array).
  888. $plugin += array('contextual links locations' => array('view'));
  889. // On exposed_forms blocks contextual links should always be visible.
  890. $plugin['contextual links locations'][] = 'special_block_-exp';
  891. $has_links = !empty($plugin['contextual links']) && !empty($plugin['contextual links locations']);
  892. if ($has_links && in_array($location, $plugin['contextual links locations'])) {
  893. foreach ($plugin['contextual links'] as $module => $link) {
  894. $args = array();
  895. $valid = TRUE;
  896. if (!empty($link['argument properties'])) {
  897. foreach ($link['argument properties'] as $property) {
  898. // If the plugin is trying to create an invalid contextual link
  899. // (for example, "path/to/{$view->property}", where $view->property
  900. // does not exist), we cannot construct the link, so we skip it.
  901. if (!property_exists($view, $property)) {
  902. $valid = FALSE;
  903. break;
  904. }
  905. else {
  906. $args[] = $view->{$property};
  907. }
  908. }
  909. }
  910. // If the link was valid, attach information about it to the renderable
  911. // array.
  912. if ($valid) {
  913. $render_element['#contextual_links'][$module] = array($link['parent path'], $args);
  914. $render_element['#views_contextual_links_info'][$module] = array(
  915. 'location' => $location,
  916. 'view' => $view,
  917. 'view_name' => $view->name,
  918. 'view_display_id' => $display_id,
  919. );
  920. }
  921. }
  922. }
  923. }
  924. }
  925. /**
  926. * Returns an array of language names.
  927. *
  928. * This is a one to one copy of locale_language_list because we can't rely on
  929. * enabled locale module.
  930. *
  931. * @param string $field
  932. * Either 'name' for localized names in current language or 'native' for
  933. * native names.
  934. * @param bool $all
  935. * Boolean to return all languages or only enabled ones.
  936. *
  937. * @see locale_language_list()
  938. */
  939. function views_language_list($field = 'name', $all = FALSE) {
  940. if ($all) {
  941. $languages = language_list();
  942. }
  943. else {
  944. $languages = language_list('enabled');
  945. $languages = $languages[1];
  946. }
  947. $list = array();
  948. foreach ($languages as $language) {
  949. $list[$language->language] = ($field == 'name') ? t($language->name) : $language->$field;
  950. }
  951. return $list;
  952. }
  953. /**
  954. * Implements hook_flush_caches().
  955. */
  956. function views_flush_caches() {
  957. return array('cache_views', 'cache_views_data');
  958. }
  959. /**
  960. * Implements hook_field_create_instance().
  961. */
  962. function views_field_create_instance($instance) {
  963. cache_clear_all('*', 'cache_views', TRUE);
  964. cache_clear_all('*', 'cache_views_data', TRUE);
  965. }
  966. /**
  967. * Implements hook_field_update_instance().
  968. */
  969. function views_field_update_instance($instance, $prior_instance) {
  970. cache_clear_all('*', 'cache_views', TRUE);
  971. cache_clear_all('*', 'cache_views_data', TRUE);
  972. }
  973. /**
  974. * Implements hook_field_delete_instance().
  975. */
  976. function views_field_delete_instance($instance) {
  977. cache_clear_all('*', 'cache_views', TRUE);
  978. cache_clear_all('*', 'cache_views_data', TRUE);
  979. }
  980. /**
  981. * Invalidate the views cache, forcing a rebuild on the next grab of table data.
  982. *
  983. * @param string $cid
  984. * The cache identifier we want to clear. If no given, it will default to '*'
  985. * which will clear the entire cache_views bin.
  986. */
  987. function views_invalidate_cache($cid = '*') {
  988. // Clear the views cache.
  989. cache_clear_all($cid, 'cache_views', TRUE);
  990. // Clear the page and block cache.
  991. cache_clear_all();
  992. // Set the menu as needed to be rebuilt.
  993. variable_set('menu_rebuild_needed', TRUE);
  994. // Allow modules to respond to the Views cache being cleared.
  995. module_invoke_all('views_invalidate_cache', $cid);
  996. }
  997. /**
  998. * Access callback to determine if the user can import Views.
  999. *
  1000. * View imports require an additional access check because they are PHP
  1001. * code and PHP is more locked down than administer views.
  1002. */
  1003. function views_import_access() {
  1004. return user_access('administer views') && user_access('use PHP for settings');
  1005. }
  1006. /**
  1007. * Determine if the logged in user has access to a view.
  1008. *
  1009. * This function should only be called from a menu hook or some other
  1010. * embedded source. Each argument is the result of a call to
  1011. * views_plugin_access::get_access_callback() which is then used
  1012. * to determine if that display is accessible. If *any* argument
  1013. * is accessible, then the view is accessible.
  1014. */
  1015. function views_access() {
  1016. $args = func_get_args();
  1017. foreach ($args as $arg) {
  1018. if ($arg === TRUE) {
  1019. return TRUE;
  1020. }
  1021. if (!is_array($arg)) {
  1022. continue;
  1023. }
  1024. list($callback, $arguments) = $arg;
  1025. $arguments = $arguments ? $arguments : array();
  1026. // Bring dynamic arguments to the access callback.
  1027. foreach ($arguments as $key => $value) {
  1028. if (is_int($value) && isset($args[$value])) {
  1029. $arguments[$key] = $args[$value];
  1030. }
  1031. }
  1032. if (function_exists($callback) && call_user_func_array($callback, $arguments)) {
  1033. return TRUE;
  1034. }
  1035. }
  1036. return FALSE;
  1037. }
  1038. /**
  1039. * Access callback for the views_plugin_access_perm access plugin.
  1040. *
  1041. * Determine if the specified user has access to a view on the basis of
  1042. * permissions. If the $account argument is omitted, the current user
  1043. * is used.
  1044. */
  1045. function views_check_perm($perms, $account = NULL) {
  1046. // Backward compatibility to ensure also a single permission string is
  1047. // properly processed.
  1048. $perms = is_array($perms) ? $perms : array($perms);
  1049. if (user_access('access all views', $account)) {
  1050. return TRUE;
  1051. }
  1052. // Perms are handled as OR, as soon one permission allows access TRUE is
  1053. // returned.
  1054. foreach ($perms as $perm) {
  1055. if (user_access($perm, $account)) {
  1056. return TRUE;
  1057. }
  1058. }
  1059. return FALSE;
  1060. }
  1061. /**
  1062. * Access callback for the views_plugin_access_role access plugin.
  1063. *
  1064. * Determine if the specified user has access to a view on the basis of any of
  1065. * the requested roles. If the $account argument is omitted, the current user
  1066. * is used.
  1067. */
  1068. function views_check_roles($rids, $account = NULL) {
  1069. global $user;
  1070. $account = isset($account) ? $account : $user;
  1071. $roles = array_keys($account->roles);
  1072. $roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
  1073. return user_access('access all views', $account) || array_intersect(array_filter($rids), $roles);
  1074. }
  1075. /**
  1076. * Set page view.
  1077. *
  1078. * Set the current 'page view' that is being displayed so that it is easy
  1079. * for other modules or the theme to identify.
  1080. */
  1081. function &views_set_page_view($view = NULL) {
  1082. static $cache = NULL;
  1083. if (isset($view)) {
  1084. $cache = $view;
  1085. }
  1086. return $cache;
  1087. }
  1088. /**
  1089. * Get page view.
  1090. *
  1091. * Find out what, if any, page view is currently in use. Please note that
  1092. * this returns a reference, so be careful! You can unintentionally modify the
  1093. * $view object.
  1094. *
  1095. * @return view
  1096. * A fully formed, empty $view object.
  1097. */
  1098. function &views_get_page_view() {
  1099. return views_set_page_view();
  1100. }
  1101. /**
  1102. * Set current view.
  1103. *
  1104. * Set the current 'current view' that is being built/rendered so that it is
  1105. * easy for other modules or items in drupal_eval to identify.
  1106. *
  1107. * @return view
  1108. * The current view.
  1109. */
  1110. function &views_set_current_view($view = NULL) {
  1111. static $cache = NULL;
  1112. if (isset($view)) {
  1113. $cache = $view;
  1114. }
  1115. return $cache;
  1116. }
  1117. /**
  1118. * Get current view.
  1119. *
  1120. * Find out what, if any, current view is currently in use. Please note that
  1121. * this returns a reference, so be careful! You can unintentionally modify the
  1122. * $view object.
  1123. *
  1124. * @return view
  1125. * The current view.
  1126. */
  1127. function &views_get_current_view() {
  1128. return views_set_current_view();
  1129. }
  1130. /**
  1131. * Include views .inc files as necessary.
  1132. */
  1133. function views_include($file) {
  1134. static $views_path;
  1135. if (!isset($views_path)) {
  1136. $views_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'views');
  1137. }
  1138. include_once $views_path . '/includes/' . $file . '.inc';
  1139. }
  1140. /**
  1141. * Load views files on behalf of modules.
  1142. */
  1143. function views_module_include($api, $reset = FALSE) {
  1144. if ($reset) {
  1145. $cache = &drupal_static('ctools_plugin_api_info');
  1146. if (isset($cache['views']['views'])) {
  1147. unset($cache['views']['views']);
  1148. }
  1149. }
  1150. ctools_include('plugins');
  1151. return ctools_plugin_api_include('views', $api, views_api_minimum_version(), views_api_version());
  1152. }
  1153. /**
  1154. * Get a list of modules that support the current views API.
  1155. */
  1156. function views_get_module_apis($api = 'views', $reset = FALSE) {
  1157. if ($reset) {
  1158. $cache = &drupal_static('ctools_plugin_api_info');
  1159. if (isset($cache['views']['views'])) {
  1160. unset($cache['views']['views']);
  1161. }
  1162. }
  1163. ctools_include('plugins');
  1164. return ctools_plugin_api_info('views', $api, views_api_minimum_version(), views_api_version());
  1165. }
  1166. /**
  1167. * Include views .css files.
  1168. */
  1169. function views_add_css($file) {
  1170. // We set preprocess to FALSE because we are adding the files conditionally,
  1171. // and we don't want to generate duplicate cache files.
  1172. // @todo at some point investigate adding some files unconditionally and
  1173. // allowing preprocess.
  1174. drupal_add_css(drupal_get_path('module', 'views') . "/css/$file.css", array('preprocess' => FALSE));
  1175. }
  1176. /**
  1177. * Include views .js files.
  1178. */
  1179. function views_add_js($file) {
  1180. // If JavaScript has been disabled by the user, never add js files.
  1181. if (variable_get('views_no_javascript', FALSE)) {
  1182. return;
  1183. }
  1184. static $base = TRUE, $ajax = TRUE;
  1185. if ($base) {
  1186. drupal_add_js(drupal_get_path('module', 'views') . "/js/base.js");
  1187. $base = FALSE;
  1188. }
  1189. if ($ajax && in_array($file, array('ajax', 'ajax_view'))) {
  1190. drupal_add_library('system', 'drupal.ajax');
  1191. drupal_add_library('system', 'jquery.form');
  1192. $ajax = FALSE;
  1193. }
  1194. ctools_add_js($file, 'views');
  1195. }
  1196. /**
  1197. * Load views files on behalf of modules.
  1198. */
  1199. function views_include_handlers($reset = FALSE) {
  1200. static $finished = FALSE;
  1201. // Ensure this only gets run once.
  1202. if ($finished && !$reset) {
  1203. return;
  1204. }
  1205. views_include('base');
  1206. views_include('handlers');
  1207. views_include('cache');
  1208. views_include('plugins');
  1209. views_module_include('views', $reset);
  1210. $finished = TRUE;
  1211. }
  1212. /**
  1213. * Fetch a handler from the data cache.
  1214. *
  1215. * @param string $table
  1216. * The name of the table this handler is from.
  1217. * @param string $field
  1218. * The name of the field this handler is from.
  1219. * @param string $key
  1220. * The type of handler. i.e, sort, field, argument, filter, relationship.
  1221. * @param mixed $override
  1222. * Override the actual handler object with this class. Used for aggregation
  1223. * when the handler is redirected to the aggregation handler.
  1224. *
  1225. * @return views_handler
  1226. * An instance of a handler object. May be views_handler_broken.
  1227. */
  1228. function views_get_handler($table, $field, $key, $override = NULL) {
  1229. static $recursion_protection = array();
  1230. $data = views_fetch_data($table, FALSE);
  1231. $handler = NULL;
  1232. views_include('handlers');
  1233. // Support conversion on table level.
  1234. if (isset($data['moved to'])) {
  1235. $moved = array($data['moved to'], $field);
  1236. }
  1237. // Support conversion on datafield level.
  1238. if (isset($data[$field]['moved to'])) {
  1239. $moved = $data[$field]['moved to'];
  1240. }
  1241. // Support conversion on handler level.
  1242. if (isset($data[$field][$key]['moved to'])) {
  1243. $moved = $data[$field][$key]['moved to'];
  1244. }
  1245. if (isset($data[$field][$key]) || !empty($moved)) {
  1246. if (!empty($moved)) {
  1247. list($moved_table, $moved_field) = $moved;
  1248. if (!empty($recursion_protection[$moved_table][$moved_field])) {
  1249. // Recursion detected!
  1250. return NULL;
  1251. }
  1252. $recursion_protection[$moved_table][$moved_field] = TRUE;
  1253. $handler = views_get_handler($moved_table, $moved_field, $key, $override);
  1254. $recursion_protection = array();
  1255. if ($handler) {
  1256. // Store these values so we know what we were originally called.
  1257. $handler->original_table = $table;
  1258. $handler->original_field = $field;
  1259. if (empty($handler->actual_table)) {
  1260. $handler->actual_table = $moved_table;
  1261. $handler->actual_field = $moved_field;
  1262. }
  1263. }
  1264. return $handler;
  1265. }
  1266. // Set up a default handler.
  1267. if (empty($data[$field][$key]['handler'])) {
  1268. $data[$field][$key]['handler'] = 'views_handler_' . $key;
  1269. }
  1270. if ($override) {
  1271. $data[$field][$key]['override handler'] = $override;
  1272. }
  1273. $handler = _views_prepare_handler($data[$field][$key], $data, $field, $key);
  1274. }
  1275. if ($handler) {
  1276. return $handler;
  1277. }
  1278. // DEBUG -- identify missing handlers.
  1279. $placeholders = array('@table' => $table, '@field' => $field, '@key' => $key);
  1280. vpr("Missing handler: @table @field @key", $placeholders);
  1281. $broken = array(
  1282. 'title' => t('Broken handler @table.@field', array('@table' => $table, '@field' => $field)),
  1283. 'handler' => 'views_handler_' . $key . '_broken',
  1284. 'table' => $table,
  1285. 'field' => $field,
  1286. );
  1287. return _views_create_handler($broken, 'handler', $key);
  1288. }
  1289. /**
  1290. * Fetch Views' data from the cache.
  1291. */
  1292. function views_fetch_data($table = NULL, $move = TRUE, $reset = FALSE) {
  1293. views_include('cache');
  1294. return _views_fetch_data($table, $move, $reset);
  1295. }
  1296. /**
  1297. * Fetch the plugin data from cache.
  1298. */
  1299. function views_fetch_plugin_data($type = NULL, $plugin = NULL, $reset = FALSE) {
  1300. views_include('cache');
  1301. return _views_fetch_plugin_data($type, $plugin, $reset);
  1302. }
  1303. /**
  1304. * Fetch a list of all base tables available.
  1305. *
  1306. * @param string $type
  1307. * Either 'display', 'style' or 'row'.
  1308. * @param string $key
  1309. * For style plugins, this is an optional type to restrict to. May be
  1310. * 'normal', 'summary', 'feed' or others based on the needs of the display.
  1311. * @param array $base
  1312. * An array of possible base tables.
  1313. *
  1314. * @return array
  1315. * A keyed array of in the form of 'base_table' => 'Description'.
  1316. */
  1317. function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
  1318. $data = views_fetch_plugin_data();
  1319. $plugins[$type] = array();
  1320. foreach ($data[$type] as $id => $plugin) {
  1321. // Skip plugins that don't conform to our key.
  1322. if ($key && (empty($plugin['type']) || $plugin['type'] != $key)) {
  1323. continue;
  1324. }
  1325. if (empty($plugin['no ui']) && (empty($base) || empty($plugin['base']) || array_intersect($base, $plugin['base']))) {
  1326. $plugins[$type][$id] = $plugin['title'];
  1327. }
  1328. }
  1329. if (!empty($plugins[$type])) {
  1330. asort($plugins[$type]);
  1331. return $plugins[$type];
  1332. }
  1333. // Fall-through.
  1334. return array();
  1335. }
  1336. /**
  1337. * Get a handler for a plugin.
  1338. *
  1339. * @return views_plugin
  1340. * The created plugin object.
  1341. */
  1342. function views_get_plugin($type, $plugin, $reset = FALSE) {
  1343. views_include('handlers');
  1344. $definition = views_fetch_plugin_data($type, $plugin, $reset);
  1345. if (!empty($definition)) {
  1346. return _views_create_handler($definition, $type);
  1347. }
  1348. }
  1349. /**
  1350. * Load the current enabled localization plugin.
  1351. *
  1352. * @return string
  1353. * The name of the localization plugin.
  1354. */
  1355. function views_get_localization_plugin() {
  1356. $plugin = variable_get('views_localization_plugin', '');
  1357. // Provide sane default values for the localization plugin.
  1358. if (empty($plugin)) {
  1359. if (module_exists('locale')) {
  1360. $plugin = 'core';
  1361. }
  1362. else {
  1363. $plugin = 'none';
  1364. }
  1365. }
  1366. return $plugin;
  1367. }
  1368. /**
  1369. * Get all view templates.
  1370. *
  1371. * Templates are special in-code views that are never active, but exist only
  1372. * to be cloned into real views as though they were templates.
  1373. */
  1374. function views_get_all_templates() {
  1375. $templates = array();
  1376. $modules = views_module_include('views_template');
  1377. foreach ($modules as $module => $info) {
  1378. $function = $module . '_views_templates';
  1379. if (function_exists($function)) {
  1380. $new = $function();
  1381. if ($new && is_array($new)) {
  1382. $templates = array_merge($new, $templates);
  1383. }
  1384. }
  1385. }
  1386. return $templates;
  1387. }
  1388. /**
  1389. * Create an empty view to work with.
  1390. *
  1391. * @return view
  1392. * A fully formed, empty $view object. This object must be populated before
  1393. * it can be successfully saved.
  1394. */
  1395. function views_new_view() {
  1396. views_include('view');
  1397. $view = new view();
  1398. $view->vid = 'new';
  1399. $view->add_display('default');
  1400. return $view;
  1401. }
  1402. /**
  1403. * Get applicable views.
  1404. *
  1405. * Return a list of all views and display IDs that have a particular setting in
  1406. * their display's plugin settings.
  1407. *
  1408. * @return array
  1409. * An array with the following structure.
  1410. * array(
  1411. * array($view, $display_id),
  1412. * array($view, $display_id),
  1413. * );
  1414. */
  1415. function views_get_applicable_views($type) {
  1416. // @todo Use a smarter flagging system so that we don't have to
  1417. // load every view for this.
  1418. $result = array();
  1419. $views = views_get_all_views();
  1420. foreach ($views as $view) {
  1421. // Skip disabled views.
  1422. if (!empty($view->disabled)) {
  1423. continue;
  1424. }
  1425. if (empty($view->display)) {
  1426. // Skip this view as it is broken.
  1427. vsm(t("Skipping broken view @view", array('@view' => $view->name)));
  1428. continue;
  1429. }
  1430. // Loop on array keys because something seems to muck with $view->display
  1431. // a bit in PHP4.
  1432. foreach (array_keys($view->display) as $id) {
  1433. $plugin = views_fetch_plugin_data('display', $view->display[$id]->display_plugin);
  1434. if (!empty($plugin[$type])) {
  1435. // This view uses hook menu. Clone it so that different handlers
  1436. // don't trip over each other, and add it to the list.
  1437. $v = $view->clone_view();
  1438. if ($v->set_display($id) && $v->display_handler->get_option('enabled')) {
  1439. $result[] = array($v, $id);
  1440. }
  1441. // In PHP 4.4.7 and presumably earlier, if we do not unset $v
  1442. // here, we will find that it actually overwrites references
  1443. // possibly due to shallow copying issues.
  1444. unset($v);
  1445. }
  1446. }
  1447. }
  1448. return $result;
  1449. }
  1450. /**
  1451. * Return an array of all views as fully loaded $view objects.
  1452. *
  1453. * @param bool $reset
  1454. * If TRUE, reset the static cache forcing views to be reloaded.
  1455. */
  1456. function views_get_all_views($reset = FALSE) {
  1457. ctools_include('export');
  1458. return ctools_export_crud_load_all('views_view', $reset);
  1459. }
  1460. /**
  1461. * Returns an array of all enabled views, as fully loaded $view objects.
  1462. */
  1463. function views_get_enabled_views() {
  1464. $views = views_get_all_views();
  1465. return array_filter($views, 'views_view_is_enabled');
  1466. }
  1467. /**
  1468. * Returns an array of all disabled views, as fully loaded $view objects.
  1469. */
  1470. function views_get_disabled_views() {
  1471. $views = views_get_all_views();
  1472. return array_filter($views, 'views_view_is_disabled');
  1473. }
  1474. /**
  1475. * Get options array.
  1476. *
  1477. * Return an array of view as options array, that can be used by select,
  1478. * checkboxes and radios as #options.
  1479. *
  1480. * @param bool $views_only
  1481. * If TRUE, only return views, not displays.
  1482. * @param string $filter
  1483. * Filters the views on status. Can either be 'all' (default), 'enabled' or
  1484. * 'disabled'.
  1485. * @param mixed $exclude_view
  1486. * View or current display to exclude
  1487. * either a
  1488. * - views object (containing name and current_display)
  1489. * - views name as string: e.g. my_view
  1490. * - views name and display id (separated by ':'): e.g. my_view:default.
  1491. * @param bool $optgroup
  1492. * If TRUE, returns an array with optgroups for each view (will be ignored for
  1493. * $views_only = TRUE). Can be used by select.
  1494. * @param bool $sort
  1495. * If TRUE, the list of views is sorted ascending.
  1496. *
  1497. * @return array
  1498. * An associative array for use in select.
  1499. * - key: view name and display id separated by ':', or the view name only
  1500. */
  1501. function views_get_views_as_options($views_only = FALSE, $filter = 'all', $exclude_view = NULL, $optgroup = FALSE, $sort = FALSE) {
  1502. // Filter the big views array.
  1503. switch ($filter) {
  1504. case 'all':
  1505. case 'disabled':
  1506. case 'enabled':
  1507. $func = "views_get_{$filter}_views";
  1508. $views = $func();
  1509. break;
  1510. default:
  1511. return array();
  1512. }
  1513. // Prepare exclude view strings for comparison.
  1514. if (empty($exclude_view)) {
  1515. $exclude_view_name = '';
  1516. $exclude_view_display = '';
  1517. }
  1518. elseif (is_object($exclude_view)) {
  1519. $exclude_view_name = $exclude_view->name;
  1520. $exclude_view_display = $exclude_view->current_display;
  1521. }
  1522. else {
  1523. list($exclude_view_name, $exclude_view_display) = explode(':', $exclude_view);
  1524. }
  1525. $options = array();
  1526. foreach ($views as $view) {
  1527. // Return only views.
  1528. if ($views_only && $view->name != $exclude_view_name) {
  1529. $options[$view->name] = $view->get_human_name();
  1530. }
  1531. // Return views with display ids.
  1532. else {
  1533. foreach ($view->display as $display_id => $display) {
  1534. if (!($view->name == $exclude_view_name && $display_id == $exclude_view_display)) {
  1535. if ($optgroup) {
  1536. $options[$view->name][$view->name . ':' . $display->id] = t('@view : @display', array('@view' => $view->name, '@display' => $display->id));
  1537. }
  1538. else {
  1539. $options[$view->name . ':' . $display->id] = t('View: @view - Display: @display', array('@view' => $view->name, '@display' => $display->id));
  1540. }
  1541. }
  1542. }
  1543. }
  1544. }
  1545. if ($sort) {
  1546. ksort($options);
  1547. }
  1548. return $options;
  1549. }
  1550. /**
  1551. * Returns TRUE if a view is enabled, FALSE otherwise.
  1552. */
  1553. function views_view_is_enabled($view) {
  1554. return empty($view->disabled);
  1555. }
  1556. /**
  1557. * Returns TRUE if a view is disabled, FALSE otherwise.
  1558. */
  1559. function views_view_is_disabled($view) {
  1560. return !empty($view->disabled);
  1561. }
  1562. /**
  1563. * Get a view from the database or from default views.
  1564. *
  1565. * This function is just a static wrapper around views::load(). This function
  1566. * isn't called 'views_load()' primarily because it might get a view
  1567. * from the default views which aren't technically loaded from the database.
  1568. *
  1569. * @param string $name
  1570. * The name of the view.
  1571. * @param bool $reset
  1572. * If TRUE, reset this entry in the load cache.
  1573. *
  1574. * @return view
  1575. * A reference to the $view object. Use $reset if you're sure you want
  1576. * a fresh one.
  1577. */
  1578. function views_get_view($name, $reset = FALSE) {
  1579. if ($reset) {
  1580. $cache = &drupal_static('ctools_export_load_object');
  1581. if (isset($cache['views_view'][$name])) {
  1582. unset($cache['views_view'][$name]);
  1583. }
  1584. }
  1585. ctools_include('export');
  1586. $view = ctools_export_crud_load('views_view', $name);
  1587. if ($view) {
  1588. $view->update();
  1589. return $view->clone_view();
  1590. }
  1591. }
  1592. /**
  1593. * Find the real location of a table.
  1594. *
  1595. * If a table has moved, find the new name of the table so that we can
  1596. * change its name directly in options where necessary.
  1597. */
  1598. function views_move_table($table) {
  1599. $data = views_fetch_data($table, FALSE);
  1600. if (isset($data['moved to'])) {
  1601. $table = $data['moved to'];
  1602. }
  1603. return $table;
  1604. }
  1605. /**
  1606. * Export callback to load the view subrecords, which are the displays.
  1607. */
  1608. function views_load_display_records(&$views) {
  1609. // Get vids from the views.
  1610. $names = array();
  1611. foreach ($views as $view) {
  1612. if (empty($view->display)) {
  1613. $names[$view->vid] = $view->name;
  1614. }
  1615. }
  1616. if (empty($names)) {
  1617. return;
  1618. }
  1619. foreach (view::db_objects() as $key) {
  1620. $object_name = "views_$key";
  1621. $result = db_query("SELECT * FROM {{$object_name}} WHERE vid IN (:vids) ORDER BY vid, position",
  1622. array(':vids' => array_keys($names)));
  1623. foreach ($result as $data) {
  1624. $object = new $object_name(FALSE);
  1625. $object->load_row($data);
  1626. // Because it can get complicated with this much indirection,
  1627. // make a shortcut reference.
  1628. $location = &$views[$names[$object->vid]]->$key;
  1629. // If we have a basic id field, load the item onto the view based on
  1630. // this ID, otherwise push it on.
  1631. if (!empty($object->id)) {
  1632. $location[$object->id] = $object;
  1633. }
  1634. else {
  1635. $location[] = $object;
  1636. }
  1637. }
  1638. }
  1639. }
  1640. /**
  1641. * Export CRUD callback to save a view.
  1642. */
  1643. function views_save_view(&$view) {
  1644. return $view->save();
  1645. }
  1646. /**
  1647. * Export CRUD callback to delete a view.
  1648. */
  1649. function views_delete_view(&$view) {
  1650. return $view->delete(TRUE);
  1651. }
  1652. /**
  1653. * Export CRUD callback to export a view.
  1654. */
  1655. function views_export_view(&$view, $indent = '') {
  1656. return $view->export($indent);
  1657. }
  1658. /**
  1659. * Export callback to change view status.
  1660. */
  1661. function views_export_status($view, $status) {
  1662. ctools_export_set_object_status($view, $status);
  1663. views_invalidate_cache();
  1664. }
  1665. /**
  1666. * Provide debug output for Views.
  1667. *
  1668. * This relies on devel.module
  1669. * or on the debug() function if you use a simpletest.
  1670. *
  1671. * @param mixed $message
  1672. * The message/variable which should be debugged.
  1673. * This either could be
  1674. * * an array/object which is converted to pretty output
  1675. * * a translation source string which is used together with the parameter
  1676. * placeholders.
  1677. * @param array $placeholders
  1678. * The placeholders which are used for the translation source string.
  1679. */
  1680. function views_debug($message, $placeholders = array()) {
  1681. if (!is_string($message)) {
  1682. $output = '<pre>' . var_export($message, TRUE) . '</pre>';
  1683. }
  1684. if (module_exists('devel') && variable_get('views_devel_output', FALSE) && user_access('access devel information')) {
  1685. $devel_region = variable_get('views_devel_region', 'footer');
  1686. if ($devel_region == 'watchdog') {
  1687. $output = $message;
  1688. watchdog('views_logging', $output, $placeholders);
  1689. }
  1690. elseif ($devel_region == 'drupal_debug') {
  1691. $output = empty($output) ? t($message, $placeholders) : $output;
  1692. dd($output);
  1693. }
  1694. else {
  1695. $output = empty($output) ? t($message, $placeholders) : $output;
  1696. dpm($output);
  1697. }
  1698. }
  1699. elseif (isset($GLOBALS['drupal_test_info'])) {
  1700. $output = empty($output) ? t($message, $placeholders) : $output;
  1701. debug($output);
  1702. }
  1703. }
  1704. /**
  1705. * Shortcut to views_debug().
  1706. */
  1707. function vpr($message, $placeholders = array()) {
  1708. views_debug($message, $placeholders);
  1709. }
  1710. /**
  1711. * Debug messages.
  1712. */
  1713. function vsm($message) {
  1714. if (module_exists('devel')) {
  1715. dpm($message);
  1716. }
  1717. }
  1718. function views_trace() {
  1719. $message = '';
  1720. foreach (debug_backtrace() as $item) {
  1721. $traces = array('vsm_trace', 'vpr_trace', 'views_trace');
  1722. if (!empty($item['file']) && !in_array($item['function'], $traces)) {
  1723. $message .= basename($item['file']) . ": " . (empty($item['class']) ? '' : ($item['class'] . '->')) . "$item[function] line $item[line]" . "\n";
  1724. }
  1725. }
  1726. return $message;
  1727. }
  1728. function vsm_trace() {
  1729. vsm(views_trace());
  1730. }
  1731. function vpr_trace() {
  1732. dpr(views_trace());
  1733. }
  1734. /**
  1735. * Determine whether the view has form elements.
  1736. *
  1737. * Returns TRUE if the passed-in view contains handlers with views form
  1738. * implementations, FALSE otherwise.
  1739. */
  1740. function views_view_has_form_elements($view) {
  1741. foreach ($view->field as $field) {
  1742. if (property_exists($field, 'views_form_callback') || method_exists($field, 'views_form')) {
  1743. return TRUE;
  1744. }
  1745. }
  1746. $area_handlers = array_merge(array_values($view->header), array_values($view->footer));
  1747. $empty = empty($view->result);
  1748. foreach ($area_handlers as $area) {
  1749. if (method_exists($area, 'views_form') && !$area->views_form_empty($empty)) {
  1750. return TRUE;
  1751. }
  1752. }
  1753. return FALSE;
  1754. }
  1755. /**
  1756. * This is the entry function. Just gets the form for the current step.
  1757. *
  1758. * The form is always assumed to be multistep, even if it has only one
  1759. * step (the default 'views_form_views_form' step). That way it is actually
  1760. * possible for modules to have a multistep form if they need to.
  1761. */
  1762. function views_form($form, &$form_state, $view, $output) {
  1763. $form_state['step'] = isset($form_state['step']) ? $form_state['step'] : 'views_form_views_form';
  1764. // Cache the built form to prevent it from being rebuilt prior to validation
  1765. // and submission, which could lead to data being processed incorrectly,
  1766. // because the views rows (and thus, the form elements as well) have changed
  1767. // in the meantime.
  1768. $form_state['cache'] = TRUE;
  1769. $form = array();
  1770. $query = drupal_get_query_parameters($_GET, array('q'));
  1771. $form['#action'] = url($view->get_url(), array('query' => $query));
  1772. // Tell the preprocessor whether it should hide the header, footer, pager...
  1773. $form['show_view_elements'] = array(
  1774. '#type' => 'value',
  1775. '#value' => ($form_state['step'] == 'views_form_views_form') ? TRUE : FALSE,
  1776. );
  1777. $form = $form_state['step']($form, $form_state, $view, $output);
  1778. return $form;
  1779. }
  1780. /**
  1781. * Callback for the main step of a Views form.
  1782. *
  1783. * Invoked by views_form().
  1784. */
  1785. function views_form_views_form($form, &$form_state, $view, $output) {
  1786. $form['#prefix'] = '<div class="views-form">';
  1787. $form['#suffix'] = '</div>';
  1788. $form['#theme'] = 'views_form_views_form';
  1789. $form['#validate'][] = 'views_form_views_form_validate';
  1790. $form['#submit'][] = 'views_form_views_form_submit';
  1791. // Add the output markup to the form array so that it's included when the form
  1792. // array is passed to the theme function.
  1793. $form['output'] = array(
  1794. '#type' => 'markup',
  1795. '#markup' => $output,
  1796. // This way any additional form elements will go before the view
  1797. // (below the exposed widgets).
  1798. '#weight' => 50,
  1799. );
  1800. $substitutions = array();
  1801. foreach ($view->field as $field_name => $field) {
  1802. $form_element_name = $field_name;
  1803. if (method_exists($field, 'form_element_name')) {
  1804. $form_element_name = $field->form_element_name();
  1805. }
  1806. $method_form_element_row_id_exists = FALSE;
  1807. if (method_exists($field, 'form_element_row_id')) {
  1808. $method_form_element_row_id_exists = TRUE;
  1809. }
  1810. // If the field provides a views form, allow it to modify the $form array.
  1811. $has_form = FALSE;
  1812. if (property_exists($field, 'views_form_callback')) {
  1813. $callback = $field->views_form_callback;
  1814. $callback($view, $field, $form, $form_state);
  1815. $has_form = TRUE;
  1816. }
  1817. elseif (method_exists($field, 'views_form')) {
  1818. $field->views_form($form, $form_state);
  1819. $has_form = TRUE;
  1820. }
  1821. // Build the substitutions array for use in the theme function.
  1822. if ($has_form) {
  1823. foreach ($view->result as $row_id => $row) {
  1824. if ($method_form_element_row_id_exists) {
  1825. $form_element_row_id = $field->form_element_row_id($row_id);
  1826. }
  1827. else {
  1828. $form_element_row_id = $row_id;
  1829. }
  1830. $substitutions[] = array(
  1831. 'placeholder' => '<!--form-item-' . $form_element_name . '--' . $form_element_row_id . '-->',
  1832. 'field_name' => $form_element_name,
  1833. 'row_id' => $form_element_row_id,
  1834. );
  1835. }
  1836. }
  1837. }
  1838. // Give the area handlers a chance to extend the form.
  1839. $area_handlers = array_merge(array_values($view->header), array_values($view->footer));
  1840. $empty = empty($view->result);
  1841. foreach ($area_handlers as $area) {
  1842. if (method_exists($area, 'views_form') && !$area->views_form_empty($empty)) {
  1843. $area->views_form($form, $form_state);
  1844. }
  1845. }
  1846. $form['#substitutions'] = array(
  1847. '#type' => 'value',
  1848. '#value' => $substitutions,
  1849. );
  1850. $form['actions'] = array(
  1851. '#type' => 'container',
  1852. '#attributes' => array('class' => array('form-actions')),
  1853. '#weight' => 100,
  1854. );
  1855. $form['actions']['submit'] = array(
  1856. '#type' => 'submit',
  1857. '#value' => t('Save'),
  1858. );
  1859. return $form;
  1860. }
  1861. /**
  1862. * Validate handler for the first step of the views form.
  1863. *
  1864. * Calls any existing views_form_validate functions located
  1865. * on the views fields.
  1866. */
  1867. function views_form_views_form_validate($form, &$form_state) {
  1868. $view = $form_state['build_info']['args'][0];
  1869. // Call the validation method on every field handler that has it.
  1870. foreach ($view->field as $field_name => $field) {
  1871. if (method_exists($field, 'views_form_validate')) {
  1872. $field->views_form_validate($form, $form_state);
  1873. }
  1874. }
  1875. // Call the validate method on every area handler that has it.
  1876. foreach (array('header', 'footer') as $area) {
  1877. foreach ($view->{$area} as $area_name => $area_handler) {
  1878. if (method_exists($area_handler, 'views_form_validate')) {
  1879. $area_handler->views_form_validate($form, $form_state);
  1880. }
  1881. }
  1882. }
  1883. }
  1884. /**
  1885. * Submit handler for the first step of the views form.
  1886. *
  1887. * Calls any existing views_form_submit functions located
  1888. * on the views fields.
  1889. */
  1890. function views_form_views_form_submit($form, &$form_state) {
  1891. $view = $form_state['build_info']['args'][0];
  1892. // Call the submit method on every field handler that has it.
  1893. foreach ($view->field as $field_name => $field) {
  1894. if (method_exists($field, 'views_form_submit')) {
  1895. $field->views_form_submit($form, $form_state);
  1896. }
  1897. }
  1898. // Call the submit method on every area handler that has it.
  1899. foreach (array('header', 'footer') as $area) {
  1900. foreach ($view->{$area} as $area_name => $area_handler) {
  1901. if (method_exists($area_handler, 'views_form_submit')) {
  1902. $area_handler->views_form_submit($form, $form_state);
  1903. }
  1904. }
  1905. }
  1906. }
  1907. /**
  1908. * Form builder for the exposed widgets form.
  1909. *
  1910. * Be sure that $view and $display are references.
  1911. */
  1912. function views_exposed_form($form, &$form_state) {
  1913. // Don't show the form when batch operations are in progress.
  1914. if ($batch = batch_get() && isset($batch['current_set'])) {
  1915. return array(
  1916. // Set the theme callback to be nothing to avoid errors in
  1917. // template_preprocess_views_exposed_form().
  1918. '#theme' => '',
  1919. );
  1920. }
  1921. // Make sure that we validate because this form might be submitted
  1922. // multiple times per page.
  1923. $form_state['must_validate'] = TRUE;
  1924. $view = &$form_state['view'];
  1925. $display = &$form_state['display'];
  1926. $form_state['input'] = $view->get_exposed_input();
  1927. // Let form plugins know this is for exposed widgets.
  1928. $form_state['exposed'] = TRUE;
  1929. // Check if the form was already created.
  1930. if ($cache = views_exposed_form_cache($view->name, $view->current_display)) {
  1931. return $cache;
  1932. }
  1933. $form['#info'] = array();
  1934. if (!variable_get('clean_url', FALSE)) {
  1935. $form['q'] = array(
  1936. '#type' => 'hidden',
  1937. '#value' => $view->get_url(),
  1938. );
  1939. }
  1940. // Go through each handler and let it generate its exposed widget.
  1941. foreach ($view->display_handler->handlers as $type => $value) {
  1942. foreach ($view->$type as $id => $handler) {
  1943. if ($handler->can_expose() && $handler->is_exposed()) {
  1944. // Grouped exposed filters have their own forms.
  1945. // Instead of render the standard exposed form, a new Select or
  1946. // Radio form field is rendered with the available groups.
  1947. // When an user choose an option the selected value is split
  1948. // into the operator and value that the item represents.
  1949. if ($handler->is_a_group()) {
  1950. $handler->group_form($form, $form_state);
  1951. $id = $handler->options['group_info']['identifier'];
  1952. }
  1953. else {
  1954. $handler->exposed_form($form, $form_state);
  1955. }
  1956. if ($info = $handler->exposed_info()) {
  1957. $form['#info']["$type-$id"] = $info;
  1958. }
  1959. }
  1960. }
  1961. }
  1962. // Form submit, #name is an empty string to prevent showing up in $_GET.
  1963. $form['submit'] = array(
  1964. '#name' => '',
  1965. '#type' => 'submit',
  1966. '#value' => t('Apply'),
  1967. '#id' => drupal_html_id('edit-submit-' . $view->name),
  1968. );
  1969. $form['#action'] = url($view->display_handler->get_url());
  1970. $form['#theme'] = views_theme_functions('views_exposed_form', $view, $display);
  1971. $form['#id'] = drupal_clean_css_identifier('views_exposed_form-' . check_plain($view->name) . '-' . check_plain($display->id));
  1972. // If using AJAX, we need the form plugin.
  1973. if ($view->use_ajax) {
  1974. drupal_add_library('system', 'jquery.form');
  1975. }
  1976. ctools_include('dependent');
  1977. $exposed_form_plugin = $form_state['exposed_form_plugin'];
  1978. $exposed_form_plugin->exposed_form_alter($form, $form_state);
  1979. // Save the form.
  1980. views_exposed_form_cache($view->name, $view->current_display, $form);
  1981. return $form;
  1982. }
  1983. /**
  1984. * Implements hook_form_alter() for views_exposed_form().
  1985. *
  1986. * Since the exposed form is a GET form, we don't want it to send a wide
  1987. * variety of information.
  1988. */
  1989. function views_form_views_exposed_form_alter(&$form, &$form_state) {
  1990. $form['form_build_id']['#access'] = FALSE;
  1991. $form['form_token']['#access'] = FALSE;
  1992. $form['form_id']['#access'] = FALSE;
  1993. }
  1994. /**
  1995. * Validate handler for exposed filters.
  1996. */
  1997. function views_exposed_form_validate(&$form, &$form_state) {
  1998. foreach (array('field', 'filter') as $type) {
  1999. $handlers = &$form_state['view']->$type;
  2000. foreach ($handlers as $key => $handler) {
  2001. $handlers[$key]->exposed_validate($form, $form_state);
  2002. }
  2003. }
  2004. $exposed_form_plugin = $form_state['exposed_form_plugin'];
  2005. $exposed_form_plugin->exposed_form_validate($form, $form_state);
  2006. }
  2007. /**
  2008. * Submit handler for exposed filters.
  2009. */
  2010. function views_exposed_form_submit(&$form, &$form_state) {
  2011. foreach (array('field', 'filter') as $type) {
  2012. $handlers = &$form_state['view']->$type;
  2013. foreach ($handlers as $key => $info) {
  2014. $handlers[$key]->exposed_submit($form, $form_state);
  2015. }
  2016. }
  2017. $form_state['view']->exposed_data = $form_state['values'];
  2018. $form_state['view']->exposed_raw_input = array();
  2019. $exclude = array(
  2020. 'q',
  2021. 'submit',
  2022. 'form_build_id',
  2023. 'form_id',
  2024. 'form_token',
  2025. 'exposed_form_plugin',
  2026. '',
  2027. 'reset',
  2028. );
  2029. $exposed_form_plugin = $form_state['exposed_form_plugin'];
  2030. $exposed_form_plugin->exposed_form_submit($form, $form_state, $exclude);
  2031. foreach ($form_state['values'] as $key => $value) {
  2032. if (!in_array($key, $exclude)) {
  2033. $form_state['view']->exposed_raw_input[$key] = $value;
  2034. }
  2035. }
  2036. }
  2037. /**
  2038. * Save the Views exposed form for later use.
  2039. *
  2040. * @param string $views_name
  2041. * The views name.
  2042. * @param string $display_name
  2043. * The current view display name.
  2044. * @param array $form_output
  2045. * An optional form structure. Only needed when inserting the value.
  2046. *
  2047. * @return array|bool
  2048. * Array. The form structure, if any. Otherwise, return FALSE.
  2049. */
  2050. function views_exposed_form_cache($views_name, $display_name, $form_output = NULL) {
  2051. // When running tests for exposed filters, this cache should
  2052. // be cleared between each test.
  2053. $views_exposed = &drupal_static(__FUNCTION__);
  2054. // Save the form output.
  2055. if (!empty($form_output)) {
  2056. $views_exposed[$views_name][$display_name] = $form_output;
  2057. }
  2058. // Return the form output, if any.
  2059. return empty($views_exposed[$views_name][$display_name]) ? FALSE : $views_exposed[$views_name][$display_name];
  2060. }
  2061. /**
  2062. * Build a list of theme function names for use most everywhere.
  2063. */
  2064. function views_theme_functions($hook, $view, $display = NULL) {
  2065. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'views') . "/theme/theme.inc";
  2066. return _views_theme_functions($hook, $view, $display);
  2067. }
  2068. /**
  2069. * Substitute current time; this works with cached queries.
  2070. */
  2071. function views_views_query_substitutions($view) {
  2072. global $language_content;
  2073. return array(
  2074. '***CURRENT_VERSION***' => VERSION,
  2075. '***CURRENT_TIME***' => REQUEST_TIME,
  2076. '***CURRENT_LANGUAGE***' => $language_content->language,
  2077. '***DEFAULT_LANGUAGE***' => language_default('language'),
  2078. );
  2079. }
  2080. /**
  2081. * Implements hook_query_TAG_alter().
  2082. *
  2083. * This is the hook_query_alter() for queries tagged by Views and is used to
  2084. * add in substitutions from hook_views_query_substitutions().
  2085. */
  2086. function views_query_views_alter(QueryAlterableInterface $query) {
  2087. $substitutions = $query->getMetaData('views_substitutions');
  2088. $tables =& $query->getTables();
  2089. $where =& $query->conditions();
  2090. // Replaces substitions in tables.
  2091. foreach ($tables as $table_name => $table_metadata) {
  2092. foreach ($table_metadata['arguments'] as $replacement_key => $value) {
  2093. if (isset($substitutions[$value])) {
  2094. $tables[$table_name]['arguments'][$replacement_key] = $substitutions[$value];
  2095. }
  2096. }
  2097. }
  2098. // Replaces substitions in filter criterias.
  2099. _views_query_tag_alter_condition($query, $where, $substitutions);
  2100. }
  2101. /**
  2102. * Replaces the substitutions recursive foreach condition.
  2103. */
  2104. function _views_query_tag_alter_condition(QueryAlterableInterface $query, &$conditions, $substitutions) {
  2105. foreach ($conditions as $condition_id => &$condition) {
  2106. if (is_numeric($condition_id)) {
  2107. if (is_string($condition['field'])) {
  2108. $condition['field'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['field']);
  2109. }
  2110. elseif (is_object($condition['field'])) {
  2111. $sub_conditions =& $condition['field']->conditions();
  2112. _views_query_tag_alter_condition($query, $sub_conditions, $substitutions);
  2113. }
  2114. // $condition['value'] is a subquery so alter the subquery recursive.
  2115. // Therefore take sure to get the metadata of the main query.
  2116. if (is_object($condition['value'])) {
  2117. $subquery = $condition['value'];
  2118. $subquery->addMetaData('views_substitutions', $query->getMetaData('views_substitutions'));
  2119. views_query_views_alter($condition['value']);
  2120. }
  2121. elseif (isset($condition['value'])) {
  2122. $condition['value'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['value']);
  2123. }
  2124. }
  2125. }
  2126. }
  2127. /**
  2128. * Embed a view using a PHP snippet.
  2129. *
  2130. * This function is meant to be called from PHP snippets, should one wish to
  2131. * embed a view in a node or something. It's meant to provide the simplest
  2132. * solution and doesn't really offer a lot of options, but breaking the function
  2133. * apart is pretty easy, and this provides a worthwhile guide to doing so.
  2134. *
  2135. * Note that this function does NOT display the title of the view. If you want
  2136. * to do that, you will need to do what this function does manually, by
  2137. * loading the view, getting the preview and then getting $view->get_title().
  2138. *
  2139. * @param string $name
  2140. * The name of the view to embed.
  2141. * @param string $display_id
  2142. * The display id to embed. If unsure, use 'default', as it will always be
  2143. * valid. But things like 'page' or 'block' should work here.
  2144. * @param ...
  2145. * Any additional parameters will be passed as arguments.
  2146. */
  2147. function views_embed_view($name, $display_id = 'default') {
  2148. $args = func_get_args();
  2149. // Remove $name.
  2150. array_shift($args);
  2151. if (count($args)) {
  2152. // Remove $display_id.
  2153. array_shift($args);
  2154. }
  2155. $view = views_get_view($name);
  2156. if (!$view || !$view->access($display_id)) {
  2157. return;
  2158. }
  2159. return $view->preview($display_id, $args);
  2160. }
  2161. /**
  2162. * Get the result of a view.
  2163. *
  2164. * @param string $name
  2165. * The name of the view to retrieve the data from.
  2166. * @param string $display_id
  2167. * The display id. On the edit page for the view in question, you'll find
  2168. * a list of displays at the left side of the control area. "Master"
  2169. * will be at the top of that list. Hover your cursor over the name of the
  2170. * display you want to use. An URL will appear in the status bar of your
  2171. * browser. This is usually at the bottom of the window, in the chrome.
  2172. * Everything after #views-tab- is the display ID, e.g. page_1.
  2173. * @param ...
  2174. * Any additional parameters will be passed as arguments.
  2175. *
  2176. * @return array
  2177. * An array containing an object for each view item.
  2178. */
  2179. function views_get_view_result($name, $display_id = NULL) {
  2180. $args = func_get_args();
  2181. // Remove $name.
  2182. array_shift($args);
  2183. if (count($args)) {
  2184. // Remove $display_id.
  2185. array_shift($args);
  2186. }
  2187. $view = views_get_view($name);
  2188. if (is_object($view)) {
  2189. if (is_array($args)) {
  2190. $view->set_arguments($args);
  2191. }
  2192. if (is_string($display_id)) {
  2193. $view->set_display($display_id);
  2194. }
  2195. else {
  2196. $view->init_display();
  2197. }
  2198. $view->pre_execute();
  2199. $view->execute();
  2200. return $view->result;
  2201. }
  2202. else {
  2203. return array();
  2204. }
  2205. }
  2206. /**
  2207. * Export a field.
  2208. */
  2209. function views_var_export($var, $prefix = '', $init = TRUE) {
  2210. if (is_array($var)) {
  2211. if (empty($var)) {
  2212. $output = 'array()';
  2213. }
  2214. else {
  2215. $output = "array(\n";
  2216. foreach ($var as $key => $value) {
  2217. $output .= " " . views_var_export($key, '', FALSE) . " => " . views_var_export($value, ' ', FALSE) . ",\n";
  2218. }
  2219. $output .= ')';
  2220. }
  2221. }
  2222. elseif (is_bool($var)) {
  2223. $output = $var ? 'TRUE' : 'FALSE';
  2224. }
  2225. elseif (is_string($var) && strpos($var, "\n") !== FALSE) {
  2226. // Replace line breaks in strings with a token for replacement
  2227. // at the very end. This protects multi-line strings from
  2228. // unintentional indentation.
  2229. $var = str_replace("\n", "***BREAK***", $var);
  2230. $output = var_export($var, TRUE);
  2231. }
  2232. else {
  2233. $output = var_export($var, TRUE);
  2234. }
  2235. if ($prefix) {
  2236. $output = str_replace("\n", "\n$prefix", $output);
  2237. }
  2238. if ($init) {
  2239. $output = str_replace("***BREAK***", "\n", $output);
  2240. }
  2241. return $output;
  2242. }
  2243. /**
  2244. * Prepare a string for use as a valid CSS identifier.
  2245. *
  2246. * This function is similar to a core version but with more sane filter values.
  2247. * http://www.w3.org/TR/CSS21/syndata.html#characters shows the syntax for valid
  2248. * CSS identifiers (including element names, classes, and IDs in selectors).
  2249. *
  2250. * @param string $identifier
  2251. * The identifier to clean.
  2252. * @param array $filter
  2253. * An array of string replacements to use on the identifier.
  2254. *
  2255. * @return string
  2256. * The cleaned identifier.
  2257. *
  2258. * @see drupal_clean_css_identifier()
  2259. */
  2260. function views_clean_css_identifier($identifier, $filter = array(
  2261. ' ' => '-',
  2262. '/' => '-',
  2263. '[' => '-',
  2264. ']' => '',
  2265. )) {
  2266. // By default, we filter using Drupal's coding standards.
  2267. $identifier = strtr($identifier, $filter);
  2268. // Valid characters in a CSS identifier are:
  2269. // - the hyphen (U+002D)
  2270. // - a-z (U+0030 - U+0039)
  2271. // - A-Z (U+0041 - U+005A)
  2272. // - the underscore (U+005F)
  2273. // - 0-9 (U+0061 - U+007A)
  2274. // - ISO 10646 characters U+00A1 and higher
  2275. // We strip out any character not in the above list.
  2276. $identifier = preg_replace('/[^\x{002D}\x{0030}-\x{0039}\x{0041}-\x{005A}\x{005F}\x{0061}-\x{007A}\x{00A1}-\x{FFFF}]/u', '', $identifier);
  2277. return $identifier;
  2278. }
  2279. /**
  2280. * Implements hook_views_exportables().
  2281. */
  2282. function views_views_exportables($op = 'list', $views = NULL, $name = 'foo') {
  2283. $all_views = views_get_all_views();
  2284. if ($op == 'list') {
  2285. foreach ($all_views as $name => $view) {
  2286. // In list, $views is a list of tags.
  2287. if (empty($views) || in_array($view->tag, $views)) {
  2288. $return[$name] = array(
  2289. 'name' => check_plain($name),
  2290. 'desc' => check_plain($view->description),
  2291. 'tag' => check_plain($view->tag),
  2292. );
  2293. }
  2294. }
  2295. return $return;
  2296. }
  2297. if ($op == 'export') {
  2298. $code = "/**\n";
  2299. $code .= " * Implement hook_views_default_views().\n";
  2300. $code .= " */\n";
  2301. $code .= "function " . $name . "_views_default_views() {\n";
  2302. foreach ($views as $view => $truth) {
  2303. $code .= " /*\n";
  2304. $code .= " * View " . var_export($all_views[$view]->name, TRUE) . "\n";
  2305. $code .= " */\n";
  2306. $code .= $all_views[$view]->export(' ');
  2307. $code .= ' $views[$view->name] = $view;' . "\n\n";
  2308. }
  2309. $code .= " return \$views;\n";
  2310. $code .= "}\n";
  2311. return $code;
  2312. }
  2313. }
  2314. /**
  2315. * Process callback to see if we need to check_plain() the options.
  2316. *
  2317. * Since FAPI is inconsistent, the #options are sanitized for you in all cases
  2318. * _except_ checkboxes. We have form elements that are sometimes 'select' and
  2319. * sometimes 'checkboxes', so we need decide late in the form rendering cycle
  2320. * if the options need to be sanitized before they're rendered. This callback
  2321. * inspects the type, and if it's still 'checkboxes', does the sanitation.
  2322. */
  2323. function views_process_check_options($element, &$form_state) {
  2324. if ($element['#type'] == 'checkboxes' || $element['#type'] == 'checkbox') {
  2325. $element['#options'] = array_map('check_plain', $element['#options']);
  2326. }
  2327. return $element;
  2328. }
  2329. /**
  2330. * Trim the field down to the specified length.
  2331. *
  2332. * @param array $alter
  2333. * - max_length: Maximum length of the string, the rest gets truncated.
  2334. * - word_boundary: Trim only on a word boundary.
  2335. * - ellipsis: Show an ellipsis (...) at the end of the trimmed string.
  2336. * - html: Take sure that the html is correct.
  2337. * @param string $value
  2338. * The string which should be trimmed.
  2339. */
  2340. function views_trim_text($alter, $value) {
  2341. if (drupal_strlen($value) > $alter['max_length']) {
  2342. $value = drupal_substr($value, 0, $alter['max_length']);
  2343. // @todo Replace this with cleanstring of CTools.
  2344. if (!empty($alter['word_boundary'])) {
  2345. $regex = "(.*)\b.+";
  2346. if (function_exists('mb_ereg')) {
  2347. mb_regex_encoding('UTF-8');
  2348. $found = mb_ereg($regex, $value, $matches);
  2349. }
  2350. else {
  2351. $found = preg_match("/$regex/us", $value, $matches);
  2352. }
  2353. if ($found) {
  2354. $value = $matches[1];
  2355. }
  2356. }
  2357. // Remove scraps of HTML entities from the end of a strings.
  2358. $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value));
  2359. if (!empty($alter['ellipsis'])) {
  2360. $value .= t('...');
  2361. }
  2362. }
  2363. if (!empty($alter['html'])) {
  2364. $value = _filter_htmlcorrector($value);
  2365. }
  2366. return $value;
  2367. }
  2368. /**
  2369. * Adds one to each key of the array.
  2370. *
  2371. * For example array(0 => 'foo') would be array(1 => 'foo').
  2372. */
  2373. function views_array_key_plus($array) {
  2374. $keys = array_keys($array);
  2375. rsort($keys);
  2376. foreach ($keys as $key) {
  2377. $array[$key + 1] = $array[$key];
  2378. unset($array[$key]);
  2379. }
  2380. asort($array);
  2381. return $array;
  2382. }
  2383. /**
  2384. * Implements hook_ctools_plugin_api_hook_name().
  2385. *
  2386. * Report to CTools that we use hook_views_api instead of
  2387. * hook_ctools_plugin_api().
  2388. */
  2389. function views_ctools_plugin_api_hook_name() {
  2390. return 'views_api';
  2391. }
  2392. /**
  2393. * Implements hook_views_api().
  2394. *
  2395. * This one is used as the base to reduce errors when updating.
  2396. */
  2397. function views_views_api() {
  2398. return array(
  2399. // In your modules do *not* use views_api_version()!!!
  2400. 'api' => views_api_version(),
  2401. 'path' => drupal_get_path('module', 'views') . '/modules',
  2402. );
  2403. }
  2404. /**
  2405. * Implements hook_admin_menu_cache_info().
  2406. */
  2407. function views_admin_menu_cache_info() {
  2408. $caches['views'] = array(
  2409. 'title' => t('Views'),
  2410. 'callback' => 'views_invalidate_cache',
  2411. );
  2412. return $caches;
  2413. }
  2414. if (!function_exists('aggregator_views_api')) {
  2415. /**
  2416. * Provide Views integration for the Aggregator module.
  2417. */
  2418. function aggregator_views_api() {
  2419. return views_views_api();
  2420. }
  2421. }
  2422. if (!function_exists('book_views_api')) {
  2423. /**
  2424. * Provide Views integration for the Book module.
  2425. */
  2426. function book_views_api() {
  2427. return views_views_api();
  2428. }
  2429. }
  2430. if (!function_exists('comment_views_api')) {
  2431. /**
  2432. * Provide Views integration for the Comment module.
  2433. */
  2434. function comment_views_api() {
  2435. return views_views_api();
  2436. }
  2437. }
  2438. if (!function_exists('field_views_api')) {
  2439. /**
  2440. * Provide Views integration for the Field module.
  2441. */
  2442. function field_views_api() {
  2443. return views_views_api();
  2444. }
  2445. }
  2446. if (!function_exists('file_views_api')) {
  2447. /**
  2448. * Provide Views integration for the File module.
  2449. */
  2450. function file_views_api() {
  2451. return views_views_api();
  2452. }
  2453. }
  2454. if (!function_exists('filter_views_api')) {
  2455. /**
  2456. * Provide Views integration for the Filter module.
  2457. */
  2458. function filter_views_api() {
  2459. return views_views_api();
  2460. }
  2461. }
  2462. if (!function_exists('image_views_api')) {
  2463. /**
  2464. * Provide Views integration for the Image module.
  2465. */
  2466. function image_views_api() {
  2467. return views_views_api();
  2468. }
  2469. }
  2470. if (!function_exists('locale_views_api')) {
  2471. /**
  2472. * Provide Views integration for the Locale module.
  2473. */
  2474. function locale_views_api() {
  2475. return views_views_api();
  2476. }
  2477. }
  2478. if (!function_exists('node_views_api')) {
  2479. /**
  2480. * Provide Views integration for the Node module.
  2481. */
  2482. function node_views_api() {
  2483. return views_views_api();
  2484. }
  2485. }
  2486. if (!function_exists('poll_views_api')) {
  2487. /**
  2488. * Provide Views integration for the Poll module.
  2489. */
  2490. function poll_views_api() {
  2491. return views_views_api();
  2492. }
  2493. }
  2494. if (!function_exists('profile_views_api')) {
  2495. /**
  2496. * Provide Views integration for the Profile module.
  2497. */
  2498. function profile_views_api() {
  2499. return views_views_api();
  2500. }
  2501. }
  2502. if (!function_exists('search_views_api')) {
  2503. /**
  2504. * Provide Views integration for the Search module.
  2505. */
  2506. function search_views_api() {
  2507. return views_views_api();
  2508. }
  2509. }
  2510. if (!function_exists('statistics_views_api')) {
  2511. /**
  2512. * Provide Views integration for the Statistics module.
  2513. */
  2514. function statistics_views_api() {
  2515. return views_views_api();
  2516. }
  2517. }
  2518. if (!function_exists('system_views_api')) {
  2519. /**
  2520. * Provide Views integration for the System module.
  2521. */
  2522. function system_views_api() {
  2523. return views_views_api();
  2524. }
  2525. }
  2526. if (!function_exists('tracker_views_api')) {
  2527. /**
  2528. * Provide Views integration for the Tracker module.
  2529. */
  2530. function tracker_views_api() {
  2531. return views_views_api();
  2532. }
  2533. }
  2534. if (!function_exists('taxonomy_views_api')) {
  2535. /**
  2536. * Provide Views integration for the Taxonomy module.
  2537. */
  2538. function taxonomy_views_api() {
  2539. return views_views_api();
  2540. }
  2541. }
  2542. if (!function_exists('translation_views_api')) {
  2543. /**
  2544. * Provide Views integration for the Translation module.
  2545. */
  2546. function translation_views_api() {
  2547. return views_views_api();
  2548. }
  2549. }
  2550. if (!function_exists('user_views_api')) {
  2551. /**
  2552. * Provide Views integration for the User module.
  2553. */
  2554. function user_views_api() {
  2555. return views_views_api();
  2556. }
  2557. }
  2558. if (!function_exists('contact_views_api')) {
  2559. /**
  2560. * Provide Views integration for the Contact module.
  2561. */
  2562. function contact_views_api() {
  2563. return views_views_api();
  2564. }
  2565. }