views.api.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. <?php
  2. /**
  3. * @file
  4. * Describe hooks provided by the Views module.
  5. */
  6. /**
  7. * @mainpage Views 3 API Manual
  8. *
  9. * Much of this information is actually stored in the advanced help; please
  10. * check the API topic. This help will primarily be aimed at documenting
  11. * classes and function calls.
  12. *
  13. * Topics:
  14. * - @link views_lifetime The life of a view @endlink
  15. * - @link views_hooks Views hooks @endlink
  16. * - @link views_handlers About Views handlers @endlink
  17. * - @link views_plugins About Views plugins @endlink
  18. * - @link views_templates Views template files @endlink
  19. * - @link views_module_handlers Views module handlers @endlink
  20. */
  21. /**
  22. * @defgroup views_lifetime The life of a view
  23. * @{
  24. * This page explains the basic cycle of a view and what processes happen.
  25. *
  26. * @todo.
  27. * @}
  28. */
  29. /**
  30. * @defgroup views_handlers About Views handlers
  31. * @{
  32. * In Views, a handler is an object that is part of the view and is part of the
  33. * query building flow.
  34. *
  35. * Handlers are objects; much of the time, the base handlers will work, but
  36. * often you'll need to override the handler to achieve something meaningful.
  37. * One typical handler override will be views_handler_filter_operator_in which
  38. * allows you to have a filter select from a list of options; you'll need to
  39. * override this to provide your list.
  40. *
  41. * Handlers have two distinct code flows; the UI flow and the view building
  42. * flow.
  43. *
  44. * For the query flow:
  45. * - handler->construct()
  46. * - Create the initial handler; at this time it is not yet attached to a
  47. * view. It is here that you can set basic defaults if needed, but there
  48. * will be no knowledge of the environment yet.
  49. * - handler->set_definition()
  50. * - Set the data from hook_views_data() relevant to the handler.
  51. * - handler->init()
  52. * - Attach the handler to a view, and usually provides the options from the
  53. * display.
  54. * - handler->pre_query()
  55. * - Run prior to the query() stage to do early processing.
  56. * - handler->query()
  57. * - Do the bulk of the work this handler needs to do to add itself to the
  58. * query.
  59. *
  60. * Fields, being the only handlers concerned with output, also have an extended
  61. * piece of the flow:
  62. *
  63. * - handler->pre_render(&$values)
  64. * - Called prior to the actual rendering, this allows handlers to query for
  65. * extra data; the entire resultset is available here, and this is where
  66. * items that have "multiple values" per record can do their extra query for
  67. * all of the records available. There are several examples of this at work
  68. * in the code, see for example views_handler_field_user_roles.
  69. * - handler->render()
  70. * - This does the actual work of rendering the field.
  71. *
  72. * Most handlers are just extensions of existing classes with a few tweaks that
  73. * are specific to the field in question. For example,
  74. * views_handler_filter_in_operator provides a simple mechanism to set a
  75. * multiple-value list for setting filter values. Below,
  76. * views_handler_filter_node_type overrides the list options, but inherits
  77. * everything else.
  78. *
  79. * @code
  80. * class views_handler_filter_node_type extends views_handler_filter_in_operator {
  81. * function get_value_options() {
  82. * if (!isset($this->value_options)) {
  83. * $this->value_title = t('Node type');
  84. * $types = node_get_types();
  85. * foreach ($types as $type => $info) {
  86. * $options[$type] = $info->name;
  87. * }
  88. * $this->value_options = $options;
  89. * }
  90. * }
  91. * }
  92. * @endcode
  93. *
  94. * Handlers are stored in their own files and loaded on demand. Like all other
  95. * module files, they must first be registered through the module's info file.
  96. * For example:
  97. *
  98. * @code
  99. * name = Example module
  100. * description = "Gives an example of a module."
  101. * core = 7.x
  102. * files[] = example.module
  103. * files[] = example.install
  104. *
  105. * ; Views handlers
  106. * files[] = includes/views/handlers/example_handler_argument_string.inc
  107. * @endcode
  108. *
  109. * The best place to learn more about handlers and how they work is to explore
  110. * @link views_handlers Views' handlers @endlink and use existing handlers as a
  111. * guide and a model. Understanding how views_handler and its child classes work
  112. * is handy but you can do a lot just following these models. You can also
  113. * explore the views module directory, particularly node.views.inc.
  114. *
  115. * Please note that while all handler names in views are prefixed with views_,
  116. * you should use your own module's name to prefix your handler names in order
  117. * to ensure namespace safety. Note that the basic pattern for handler naming
  118. * goes like this:
  119. *
  120. * [module]_handler_[type]_[tablename]_[fieldname].
  121. *
  122. * Sometimes table and fieldname are not appropriate, but something that
  123. * resembles what the table/field would be can be used.
  124. *
  125. * See also:
  126. * - @link views_field_handlers Views field handlers @endlink
  127. * - @link views_sort_handlers Views sort handlers @endlink
  128. * - @link views_filter_handlers Views filter handlers @endlink
  129. * - @link views_argument_handlers Views argument handlers @endlink
  130. * - @link views_relationship_handlers Views relationship handlers @endlink
  131. * - @link views_area_handlers Views area handlers @endlink
  132. * @}
  133. */
  134. /**
  135. * @defgroup views_plugins About Views plugins
  136. *
  137. * In Views, a plugin is a bit like a handler, but plugins are not directly
  138. * responsible for building the query. Instead, they are objects that are used
  139. * to display the view or make other modifications.
  140. *
  141. * There are several types of plugins in Views:
  142. * - Display: Display plugins are responsible for controlling *where* a view
  143. * lives; that is, how they are being exposed to other parts of Drupal. Page
  144. * and block are the most common displays, as well as the ubiquitous 'master'
  145. * (or 'default') display.
  146. * - Style: Style plugins control how a view is displayed. For the most part
  147. * they are object wrappers around theme templates. Styles could for example
  148. * be HTML lists or tables.
  149. * - Row style: Row styles handle each individual record from the main view
  150. * table. The two included by default render the entire entity (nodes only),
  151. * or selected fields.
  152. * - Argument default: Argument default plugins allow pluggable ways of
  153. * providing default values for contextual filters (previously 'arguments').
  154. * This is useful for blocks and other display types lacking a natural
  155. * argument input. Examples are plugins to extract node and user IDs from the
  156. * URL.
  157. * - Argument validator: Validator plugins can ensure arguments are valid, and
  158. * even do transformations on the arguments. They can also provide replacement
  159. * patterns for the view title. For example, the 'content' validator
  160. * verifies verifies that the argument value corresponds to a node, loads
  161. * that node and provides the node title as a replacement pattern.
  162. * - Access: Access plugins are responsible for controlling access to the view.
  163. * Views includes plugins for checking user roles and individual permissions.
  164. * - Query: Query plugins generate and execute a query, so they can be seen as
  165. * a data backend. The default implementation is using SQL. There are
  166. * contributed modules reading data from other sources, see for example the
  167. * Views XML Backend module.
  168. * - Cache: Cache plugins control the storage and loading of caches. Currently
  169. * they can do both result and render caching, but maybe one day cache the
  170. * generated query.
  171. * - Pager plugins: Pager plugins take care of everything regarding pagers.
  172. * From getting and setting the total amount of items to render the pager and
  173. * setting the global pager arrays.
  174. * - Exposed form plugins: Exposed form plugins are responsible for building,
  175. * rendering and controlling exposed forms. They can expose new parts of the
  176. * view to the user and more.
  177. * - Localization plugins: Localization plugins take care how the view options
  178. * are translated. There are example implementations for t(), 'no
  179. * translation' and i18n.
  180. * - Display extenders: Display extender plugins allow scaling of views options
  181. * horizontally. This means that you can add options and do stuff on all
  182. * views displays. One theoretical example is metatags for views.
  183. *
  184. * Plugins are registered by implementing hook_views_plugins() in your
  185. * modulename.views.inc file and returning an array of data.
  186. * For examples please look at views_views_plugins() in
  187. * views/includes/plugins.inc as it has examples for all of them.
  188. *
  189. * Similar to handlers, make sure that you add your plugin files to the
  190. * module.info file.
  191. *
  192. * The array defining plugins will look something like this:
  193. * @code
  194. * return array(
  195. * 'display' => array(
  196. * // ... list of display plugins,
  197. * ),
  198. * 'style' => array(
  199. * // ... list of style plugins,
  200. * ),
  201. * 'row' => array(
  202. * // ... list of row style plugins,
  203. * ),
  204. * 'argument default' => array(
  205. * // ... list of argument default plugins,
  206. * ),
  207. * 'argument validator' => array(
  208. * // ... list of argument validator plugins,
  209. * ),
  210. * 'access' => array(
  211. * // ... list of access plugins,
  212. * ),
  213. * 'query' => array(
  214. * // ... list of query plugins,
  215. * ),,
  216. * 'cache' => array(
  217. * // ... list of cache plugins,
  218. * ),,
  219. * 'pager' => array(
  220. * // ... list of pager plugins,
  221. * ),,
  222. * 'exposed_form' => array(
  223. * // ... list of exposed_form plugins,
  224. * ),,
  225. * 'localization' => array(
  226. * // ... list of localization plugins,
  227. * ),
  228. * 'display_extender' => array(
  229. * // ... list of display extender plugins,
  230. * ),
  231. * );
  232. * @endcode
  233. *
  234. * Each plugin will be registered with an identifier for the plugin, plus a
  235. * fairly lengthy list of items that can define how and where the plugin is
  236. * used. Here is an example of a row style plugin from Views core:
  237. * @code
  238. * 'node' => array(
  239. * 'title' => t('Node'),
  240. * 'help' => t('Display the node with standard node view.'),
  241. * 'handler' => 'views_plugin_row_node_view',
  242. * // Not necessary for most modules.
  243. * 'path' => drupal_get_path('module', 'views') . '/modules/node',
  244. * 'theme' => 'views_view_row_node',
  245. * // Only works with 'node' as base.
  246. * 'base' => array('node'),
  247. * 'uses options' => TRUE,
  248. * 'type' => 'normal',
  249. * ),
  250. * @endcode
  251. *
  252. * Of particular interest is the *path* directive, which works a little
  253. * differently from handler registration; each plugin must define its own path,
  254. * rather than relying on a global info for the paths. For example:
  255. * @code
  256. * 'feed' => array(
  257. * 'title' => t('Feed'),
  258. * 'help' => t('Display the view as a feed, such as an RSS feed.'),
  259. * 'handler' => 'views_plugin_display_feed',
  260. * 'uses hook menu' => TRUE,
  261. * 'use ajax' => FALSE,
  262. * 'use pager' => FALSE,
  263. * 'accept attachments' => FALSE,
  264. * 'admin' => t('Feed'),
  265. * 'help topic' => 'display-feed',
  266. * ),
  267. * @endcode
  268. *
  269. * Please be sure to prefix your plugin identifiers with your module name to
  270. * ensure namespace safety; after all, two different modules could try to
  271. * implement the 'grid2' plugin, and that would cause one plugin to completely
  272. * fail.
  273. *
  274. * @todo Finish this document.
  275. *
  276. * See also:
  277. * - @link views_display_plugins Views display plugins @endlink
  278. * - @link views_style_plugins Views style plugins @endlink
  279. * - @link views_row_plugins Views row plugins @endlink
  280. */
  281. /**
  282. * @defgroup views_hooks Views hooks
  283. * @{
  284. * Hooks that can be implemented by other modules in order to implement the
  285. * Views API.
  286. */
  287. /**
  288. * Describes data tables (or the equivalent) to Views.
  289. *
  290. * This hook should be placed in MODULENAME.views.inc and it will be auto
  291. * loaded. MODULENAME.views.inc must be in the directory specified by the 'path'
  292. * key returned by MODULENAME_views_api(), or the same directory as the .module
  293. * file, if 'path' is unspecified.
  294. *
  295. * @return array
  296. * An associative array describing the data structure. Primary key is the
  297. * name used internally by Views for the table(s) – usually the actual table
  298. * name. The values for the key entries are described in detail below.
  299. */
  300. function hook_views_data() {
  301. // This example describes how to write hook_views_data() for the following
  302. // table:
  303. //
  304. // CREATE TABLE example_table (
  305. // nid INT(11) NOT NULL COMMENT 'Primary key; refers to {node}.nid.',
  306. // plain_text_field VARCHAR(32) COMMENT 'Just a plain text field.',
  307. // numeric_field INT(11) COMMENT 'Just a numeric field.',
  308. // boolean_field INT(1) COMMENT 'Just an on/off field.',
  309. // timestamp_field INT(8) COMMENT 'Just a timestamp field.',
  310. // PRIMARY KEY(nid)
  311. // );
  312. // First, the entry $data['example_table']['table'] describes properties of
  313. // the actual table – not its content.
  314. // The 'group' index will be used as a prefix in the UI for any of this
  315. // table's fields, sort criteria, etc. so it's easy to tell where they came
  316. // from.
  317. $data['example_table']['table']['group'] = t('Example table');
  318. // Define this as a base table – a table that can be described in itself by
  319. // views (and not just being brought in as a relationship). In reality this
  320. // is not very useful for this table, as it isn't really a distinct object of
  321. // its own, but it makes a good example.
  322. $data['example_table']['table']['base'] = array(
  323. // This is the identifier field for the view.
  324. 'field' => 'nid',
  325. 'title' => t('Example table'),
  326. 'help' => t('Example table contains example content and can be related to nodes.'),
  327. 'weight' => -10,
  328. );
  329. // This table references the {node} table. The declaration below creates an
  330. // 'implicit' relationship to the node table, so that when 'node' is the base
  331. // table, the fields are automatically available.
  332. $data['example_table']['table']['join'] = array(
  333. // Index this array by the table name to which this table refers.
  334. 'node' => array(
  335. // The primary key in the referenced table.
  336. 'left_field' => 'nid',
  337. // The foreign key in this table.
  338. 'field' => 'nid',
  339. ),
  340. );
  341. // Next, describe each of the individual fields in this table to Views. This
  342. // is done by describing $data['example_table']['FIELD_NAME']. This part of
  343. // the array may then have further entries:
  344. // - title: The label for the table field, as presented in Views.
  345. // - help: The description text for the table field.
  346. // - relationship: A description of any relationship handler for the table
  347. // field.
  348. // - field: A description of any field handler for the table field.
  349. // - sort: A description of any sort handler for the table field.
  350. // - filter: A description of any filter handler for the table field.
  351. // - argument: A description of any argument handler for the table field.
  352. // - area: A description of any handler for adding content to header,
  353. // footer or as no result behaviour.
  354. //
  355. // The handler descriptions are described with examples below.
  356. // Node ID table field.
  357. $data['example_table']['nid'] = array(
  358. 'title' => t('Example content'),
  359. 'help' => t('Some example content that references a node.'),
  360. // Define a relationship to the {node} table, so example_table views can
  361. // add a relationship to nodes. If you want to define a relationship the
  362. // other direction, use hook_views_data_alter(), or use the 'implicit' join
  363. // method described above.
  364. 'relationship' => array(
  365. // The name of the table to join with.
  366. 'base' => 'node',
  367. // The name of the field on the joined table.
  368. 'base field' => 'nid',
  369. // 'field' => 'nid' -- see hook_views_data_alter(); not needed here.
  370. 'handler' => 'views_handler_relationship',
  371. 'label' => t('Default label for the relationship'),
  372. 'title' => t('Title shown when adding the relationship'),
  373. 'help' => t('More information on this relationship'),
  374. ),
  375. );
  376. // Example plain text field.
  377. $data['example_table']['plain_text_field'] = array(
  378. 'title' => t('Plain text field'),
  379. 'help' => t('Just a plain text field.'),
  380. 'field' => array(
  381. 'handler' => 'views_handler_field',
  382. // This is use by the table display plugin.
  383. 'click sortable' => TRUE,
  384. ),
  385. 'sort' => array(
  386. 'handler' => 'views_handler_sort',
  387. ),
  388. 'filter' => array(
  389. 'handler' => 'views_handler_filter_string',
  390. ),
  391. 'argument' => array(
  392. 'handler' => 'views_handler_argument_string',
  393. ),
  394. );
  395. // Example numeric text field.
  396. $data['example_table']['numeric_field'] = array(
  397. 'title' => t('Numeric field'),
  398. 'help' => t('Just a numeric field.'),
  399. 'field' => array(
  400. 'handler' => 'views_handler_field_numeric',
  401. 'click sortable' => TRUE,
  402. ),
  403. 'filter' => array(
  404. 'handler' => 'views_handler_filter_numeric',
  405. ),
  406. 'sort' => array(
  407. 'handler' => 'views_handler_sort',
  408. ),
  409. );
  410. // Example boolean field.
  411. $data['example_table']['boolean_field'] = array(
  412. 'title' => t('Boolean field'),
  413. 'help' => t('Just an on/off field.'),
  414. 'field' => array(
  415. 'handler' => 'views_handler_field_boolean',
  416. 'click sortable' => TRUE,
  417. ),
  418. 'filter' => array(
  419. 'handler' => 'views_handler_filter_boolean_operator',
  420. // Note that you can override the field-wide label.
  421. 'label' => t('Published'),
  422. // This setting is used by the boolean filter handler, as possible option.
  423. 'type' => 'yes-no',
  424. // use boolean_field = 1 instead of boolean_field <> 0 in WHERE statement.
  425. 'use equal' => TRUE,
  426. ),
  427. 'sort' => array(
  428. 'handler' => 'views_handler_sort',
  429. ),
  430. );
  431. // Example timestamp field.
  432. $data['example_table']['timestamp_field'] = array(
  433. 'title' => t('Timestamp field'),
  434. 'help' => t('Just a timestamp field.'),
  435. 'field' => array(
  436. 'handler' => 'views_handler_field_date',
  437. 'click sortable' => TRUE,
  438. ),
  439. 'sort' => array(
  440. 'handler' => 'views_handler_sort_date',
  441. ),
  442. 'filter' => array(
  443. 'handler' => 'views_handler_filter_date',
  444. ),
  445. );
  446. return $data;
  447. }
  448. /**
  449. * Alter table structure.
  450. *
  451. * You can add/edit/remove existing tables defined by hook_views_data().
  452. *
  453. * This hook should be placed in MODULENAME.views.inc and it will be
  454. * auto-loaded. MODULENAME.views.inc must be in the directory specified by the
  455. * 'path' key returned by MODULENAME_views_api(), or the same directory as the
  456. * .module file, if 'path' is unspecified.
  457. *
  458. * @param array $data
  459. * An array of all Views data, passed by reference. See hook_views_data() for
  460. * structure.
  461. *
  462. * @see hook_views_data()
  463. */
  464. function hook_views_data_alter(&$data) {
  465. // This example alters the title of the node:nid field in the Views UI.
  466. $data['node']['nid']['title'] = t('Node-Nid');
  467. // This example adds an example field to the users table.
  468. $data['users']['example_field'] = array(
  469. 'title' => t('Example field'),
  470. 'help' => t('Some example content that references a user'),
  471. 'field' => array(
  472. 'handler' => 'modulename_handler_field_example_field',
  473. ),
  474. );
  475. // This example changes the handler of the node title field.
  476. // In this handler you could do stuff, like preview of the node when clicking
  477. // the node title.
  478. $data['node']['title']['field']['handler'] = 'modulename_handler_field_node_title';
  479. // This example adds a relationship to table {foo}, so that 'foo' views can
  480. // add this table using a relationship. Because we don't want to write over
  481. // the primary key field definition for the {foo}.fid field, we use a dummy
  482. // field name as the key.
  483. $data['foo']['dummy_name'] = array(
  484. 'title' => t('Example relationship'),
  485. 'help' => t('Example help'),
  486. 'relationship' => array(
  487. // Table we're joining to.
  488. 'base' => 'example_table',
  489. // Field on the joined table.
  490. 'base field' => 'eid',
  491. // Real field name on the 'foo' table.
  492. 'field' => 'fid',
  493. 'handler' => 'views_handler_relationship',
  494. 'label' => t('Default label for relationship'),
  495. 'title' => t('Title seen when adding relationship'),
  496. 'help' => t('More information about relationship.'),
  497. ),
  498. );
  499. // Note that the $data array is not returned – it is modified by reference.
  500. }
  501. /**
  502. * Override the default data for a Field API field.
  503. *
  504. * Field module's Implements hook_views_data() invokes this for each
  505. * field in the module that defines the field type (as declared in the field
  506. * array). It is not invoked in other modules.
  507. *
  508. * If no hook implementation exists, hook_views_data() falls back to
  509. * field_views_field_default_views_data().
  510. *
  511. * @param array $field
  512. * A field definition array, as returned by field_info_fields().
  513. *
  514. * @return array
  515. * An array of views data, in the same format as the return value of
  516. * hook_views_data().
  517. *
  518. * @see field_views_data()
  519. * @see hook_field_views_data_alter()
  520. * @see hook_field_views_data_views_data_alter()
  521. */
  522. function hook_field_views_data($field) {
  523. return array();
  524. }
  525. /**
  526. * Alter the views data for a single Field API field.
  527. *
  528. * This is called even if there is no hook_field_views_data() implementation for
  529. * the field, and therefore may be used to alter the default data that
  530. * field_views_field_default_views_data() supplies for the field.
  531. *
  532. * @param array $result
  533. * An array of views table data provided for a single field. This has the same
  534. * format as the return value of hook_views_data().
  535. * @param array $field
  536. * A field definition array, as returned by field_info_fields().
  537. * @param string $module
  538. * The module that defines the field type.
  539. *
  540. * @see field_views_data()
  541. * @see hook_field_views_data()
  542. * @see hook_field_views_data_views_data_alter()
  543. */
  544. function hook_field_views_data_alter(&$result, $field, $module) {
  545. }
  546. /**
  547. * Alter the views data on a per field basis.
  548. *
  549. * Field module's Implements hook_views_data_alter() invokes this for
  550. * each field in the module that defines the field type (as declared in the
  551. * field array). It is not invoked in other modules.
  552. *
  553. * Unlike hook_field_views_data_alter(), this operates on the whole of the views
  554. * data. This allows a field module to add data that concerns its fields to
  555. * other tables, which would not yet be defined at the point when
  556. * hook_field_views_data() and hook_field_views_data_alter() are invoked. For
  557. * example, entityreference adds reverse relationships on the tables for the
  558. * entities which are referenced by entityreference fields.
  559. *
  560. * (Note: this is weirdly named so as not to conflict with
  561. * hook_field_views_data_alter().)
  562. *
  563. * @see hook_field_views_data()
  564. * @see hook_field_views_data_alter()
  565. * @see field_views_data_alter()
  566. */
  567. function hook_field_views_data_views_data_alter(&$data, $field) {
  568. $field_name = $field['field_name'];
  569. $data_key = 'field_data_' . $field_name;
  570. // Views data for this field is in $data[$data_key]
  571. }
  572. /**
  573. * Describes plugins defined by the module.
  574. *
  575. * This hook should be placed in MODULENAME.views.inc and it will be
  576. * auto-loaded. MODULENAME.views.inc must be in the directory specified by the
  577. * 'path' key returned by MODULENAME_views_api(), or the same directory as the
  578. * .module file, if 'path' is unspecified. All plugin files need to be
  579. * referenced in MODULENAME.info with the files[] directive.
  580. *
  581. * @return array
  582. * An array on the form $plugins['PLUGIN TYPE']['PLUGIN NAME']. The plugin
  583. * must be one of row, display, display_extender, style, argument default,
  584. * argument validator, access, query, cache, pager, exposed_form or
  585. * localization. The plugin name should be prefixed with your module name.
  586. * The value for each entry is an associative array that may contain the
  587. * following entries:
  588. * - Used by all plugin types:
  589. * - title (required): The name of the plugin, as shown in Views. Wrap in
  590. * t().
  591. * - handler (required): The name of the file containing the class
  592. * describing the handler, which must also be the name of the handler's
  593. * class.
  594. * - path: Path to the handler. Only required if the handler is not placed
  595. * in the same folder as the .module file or in the subfolder 'views'.
  596. * - parent: The name of the plugin this plugin extends. Since Drupal 7 this
  597. * is no longer required, but may still be useful from a code readability
  598. * perspective.
  599. * - no ui: Set to TRUE to denote that the plugin doesn't appear to be
  600. * selectable in the ui, though on the api side they still exists.
  601. * - uses options: Set to TRUE to denote that the plugin has an additional
  602. * options form.
  603. * - help: A short help text, wrapped in t() used as description on the
  604. * plugin settings form.
  605. * - help topic: The name of an entry by advanced help for the plugin.
  606. * - theme: The name of a theme suggestion to use for the display.
  607. * - js: An array with paths to js files that should be included for the
  608. * display. Note that the path should be relative Drupal root, not module
  609. * root.
  610. * - type: Each plugin can specify a type parameter to group certain
  611. * plugins together. For example all row plugins related to feeds are
  612. * grouped together, because a rss style plugin only accepts feed row
  613. * plugins.
  614. *
  615. * - Used by display plugins:
  616. * - admin: The administrative name of the display, as displayed on the
  617. * Views overview and also used as default name for new displays. Wrap in
  618. * t().
  619. * - no remove: Set to TRUE to make the display non-removable. (Basically
  620. * only used for the master/default display.)
  621. * - use ajax: Set to TRUE to allow AJAX loads in the display. If it's
  622. * disabled there will be no ajax option in the ui.
  623. * - use pager: Set to TRUE to allow paging in the display.
  624. * - use more: Set to TRUE to allow the 'use more' setting in the display.
  625. * - accept attachments: Set to TRUE to allow attachment displays to be
  626. * attached to this display type.
  627. * - contextual links locations: An array with places where contextual links
  628. * should be added. Can for example be 'page' or 'block'. If you don't
  629. * specify it there will be contextual links around the rendered view. If
  630. * this is not set or regions have been specified, views will display an
  631. * option to 'hide contextual links'. Use an empty array if you do not
  632. * want this.
  633. * - uses hook menu: Set to TRUE to have the display included by
  634. * views_menu_alter(). views_menu_alter executes then execute_hook_menu
  635. * on the display object.
  636. * - uses hook block: Set to TRUE to have the display included by
  637. * views_block_info().
  638. * - theme: The name of a theme suggestion to use for the display.
  639. * - js: An array with paths to js files that should be included for the
  640. * display. Note that the path should be relative Drupal root, not module
  641. * root.
  642. *
  643. * - Used by style plugins:
  644. * - uses row plugin: Set to TRUE to allow row plugins for this style.
  645. * - uses row class: Set to TRUE to allow the CSS class settings for rows.
  646. * - uses fields: Set to TRUE to have the style plugin accept field
  647. * handlers.
  648. * - uses grouping: Set to TRUE to allow the grouping settings for rows.
  649. * - even empty: May have the value 'even empty' to tell Views that the
  650. * style should be rendered even if there are no results.
  651. *
  652. * - Used by row plugins:
  653. * - uses fields: Set to TRUE to have the row plugin accept field handlers.
  654. */
  655. function hook_views_plugins() {
  656. $plugins = array();
  657. $plugins['argument validator'] = array(
  658. 'taxonomy_term' => array(
  659. 'title' => t('Taxonomy term'),
  660. 'handler' => 'views_plugin_argument_validate_taxonomy_term',
  661. // Declaring path explicitly not necessary for most modules.
  662. 'path' => drupal_get_path('module', 'views') . '/modules/taxonomy',
  663. ),
  664. );
  665. return array(
  666. // This just tells our themes are elsewhere.
  667. 'module' => 'views',
  668. 'argument validator' => array(
  669. 'taxonomy_term' => array(
  670. 'title' => t('Taxonomy term'),
  671. 'handler' => 'views_plugin_argument_validate_taxonomy_term',
  672. // Declaring path explicitly not necessary for most modules.
  673. 'path' => drupal_get_path('module', 'views') . '/modules/taxonomy',
  674. ),
  675. ),
  676. 'argument default' => array(
  677. 'taxonomy_tid' => array(
  678. 'title' => t('Taxonomy term ID from URL'),
  679. 'handler' => 'views_plugin_argument_default_taxonomy_tid',
  680. 'path' => drupal_get_path('module', 'views') . '/modules/taxonomy',
  681. 'parent' => 'fixed',
  682. ),
  683. ),
  684. );
  685. }
  686. /**
  687. * Alter existing plugins data, defined by modules.
  688. *
  689. * @see hook_views_plugins()
  690. */
  691. function hook_views_plugins_alter(&$plugins) {
  692. // Add apachesolr to the base of the node row plugin.
  693. $plugins['row']['node']['base'][] = 'apachesolr';
  694. }
  695. /**
  696. * Alter existing plugin option definitions.
  697. *
  698. * This can be used to edit default or add new option definitions to existing
  699. * plugins. The reason for doing this is that only overriding the relevent form
  700. * with hook_form_alter() is insufficent because submitted form values will be
  701. * ignored if they haven't been declared as an available option.
  702. *
  703. * An alternative approach you could also take is to extend each plugin
  704. * individually. However if your goal is to override many, or even all plugins,
  705. * this results in a lot of additional code and files. This makes it a lot more
  706. * troublesome to maintain the codebase, as well as interoperability with other
  707. * modules.
  708. *
  709. * @param array $options
  710. * The option definitions to be altered.
  711. * @param $plugin
  712. * A views object of the plugin where the default options are defined.
  713. *
  714. * @see views_object::option_definition()
  715. * @see hook_views_handler_option_definition_alter()
  716. * @see hook_form_alter()
  717. */
  718. function hook_views_plugin_option_definition_alter(&$options, $plugin) {
  719. // Add a new option definition.
  720. $options['option_name'] = array('default' => '');
  721. }
  722. /**
  723. * Alter existing handler option definitions.
  724. *
  725. * This can be used to edit default or add new option definitions to existing
  726. * handlers. The reason for doing this is that only overriding the relevent form
  727. * with hook_form_alter() is insufficent because submitted form values will be
  728. * ignored if they haven't been declared as an available option.
  729. *
  730. * An alternative approach you could also take is to extend each handler
  731. * individually. However if your goal is to override many, or even all handlers,
  732. * this results in a lot of additional code and files. This makes it a lot more
  733. * troublesome to maintain the codebase, as well as interoperability with other
  734. * modules.
  735. *
  736. * @param array $options
  737. * The option definitions to be altered.
  738. * @param $handler
  739. * A views object of the handler where the default options are defined.
  740. *
  741. * @see views_handler::option_definition()
  742. * @see hook_views_plugin_option_definition_alter()
  743. * @see hook_form_alter()
  744. */
  745. function hook_views_handler_option_definition_alter(&$options, $handler) {
  746. // Add a new option definition.
  747. $options['option_name'] = array('default' => '');
  748. }
  749. /**
  750. * Register View API information.
  751. *
  752. * This is required for your module to have its include files loaded; for
  753. * example, when implementing hook_views_default_views().
  754. *
  755. * @return array
  756. * An array with the following possible keys:
  757. * - api: (required) The version of the Views API the module implements.
  758. * - path: (optional) If includes are stored somewhere other than within the
  759. * root module directory, specify its path here.
  760. * - template path: (optional) A path where the module has stored its views
  761. * template files. When you have specified this key views automatically
  762. * uses the template files for the views. You can use the same naming
  763. * conventions like for normal views template files.
  764. */
  765. function hook_views_api() {
  766. return array(
  767. 'api' => 3,
  768. 'path' => drupal_get_path('module', 'example') . '/includes/views',
  769. 'template path' => drupal_get_path('module', 'example') . '/themes',
  770. );
  771. }
  772. /**
  773. * Allows modules to provide their own views.
  774. *
  775. * These can either be used as-is or as a "starter" for users to build from.
  776. *
  777. * This hook should be placed in MODULENAME.views_default.inc and it will be
  778. * auto-loaded. MODULENAME.views_default.inc must be in the directory specified
  779. * by the 'path' key returned by MODULENAME_views_api(), or the same directory
  780. * as the .module file, if 'path' is unspecified.
  781. *
  782. * The $view->disabled boolean flag indicates whether the View should be
  783. * enabled (FALSE) or disabled (TRUE) by default.
  784. *
  785. * @return array
  786. * An associative array containing the structures of views, as generated from
  787. * the Export tab, keyed by the view name. A best practice is to go through
  788. * and add t() to all title and label strings, with the exception of menu
  789. * strings.
  790. */
  791. function hook_views_default_views() {
  792. // Begin copy and paste of output from the Export tab of a view.
  793. $view = new view();
  794. $view->name = 'frontpage';
  795. $view->description = 'Emulates the default Drupal front page; you may set the default home page path to this view to make it your front page.';
  796. $view->tag = 'default';
  797. $view->base_table = 'node';
  798. $view->human_name = 'Front page';
  799. $view->core = 0;
  800. $view->api_version = '3.0';
  801. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  802. /* Display: Master */
  803. $handler = $view->new_display('default', 'Master', 'default');
  804. $handler->display->display_options['access']['type'] = 'none';
  805. $handler->display->display_options['cache']['type'] = 'none';
  806. $handler->display->display_options['query']['type'] = 'views_query';
  807. $handler->display->display_options['query']['options']['query_comment'] = FALSE;
  808. $handler->display->display_options['exposed_form']['type'] = 'basic';
  809. $handler->display->display_options['pager']['type'] = 'full';
  810. $handler->display->display_options['style_plugin'] = 'default';
  811. $handler->display->display_options['row_plugin'] = 'node';
  812. /* Sort criterion: Content: Sticky */
  813. $handler->display->display_options['sorts']['sticky']['id'] = 'sticky';
  814. $handler->display->display_options['sorts']['sticky']['table'] = 'node';
  815. $handler->display->display_options['sorts']['sticky']['field'] = 'sticky';
  816. $handler->display->display_options['sorts']['sticky']['order'] = 'DESC';
  817. /* Sort criterion: Content: Post date */
  818. $handler->display->display_options['sorts']['created']['id'] = 'created';
  819. $handler->display->display_options['sorts']['created']['table'] = 'node';
  820. $handler->display->display_options['sorts']['created']['field'] = 'created';
  821. $handler->display->display_options['sorts']['created']['order'] = 'DESC';
  822. /* Filter criterion: Content: Promoted to front page */
  823. $handler->display->display_options['filters']['promote']['id'] = 'promote';
  824. $handler->display->display_options['filters']['promote']['table'] = 'node';
  825. $handler->display->display_options['filters']['promote']['field'] = 'promote';
  826. $handler->display->display_options['filters']['promote']['value'] = '1';
  827. $handler->display->display_options['filters']['promote']['group'] = 0;
  828. $handler->display->display_options['filters']['promote']['expose']['operator'] = FALSE;
  829. /* Filter criterion: Content: Published */
  830. $handler->display->display_options['filters']['status']['id'] = 'status';
  831. $handler->display->display_options['filters']['status']['table'] = 'node';
  832. $handler->display->display_options['filters']['status']['field'] = 'status';
  833. $handler->display->display_options['filters']['status']['value'] = '1';
  834. $handler->display->display_options['filters']['status']['group'] = 0;
  835. $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
  836. /* Display: Page */
  837. $handler = $view->new_display('page', 'Page', 'page');
  838. $handler->display->display_options['path'] = 'frontpage';
  839. /* Display: Feed */
  840. $handler = $view->new_display('feed', 'Feed', 'feed');
  841. $handler->display->display_options['defaults']['title'] = FALSE;
  842. $handler->display->display_options['title'] = 'Front page feed';
  843. $handler->display->display_options['pager']['type'] = 'some';
  844. $handler->display->display_options['style_plugin'] = 'rss';
  845. $handler->display->display_options['row_plugin'] = 'node_rss';
  846. $handler->display->display_options['path'] = 'rss.xml';
  847. $handler->display->display_options['displays'] = array(
  848. 'default' => 'default',
  849. 'page' => 'page',
  850. );
  851. $handler->display->display_options['sitename_title'] = '1';
  852. // (Export ends here.)
  853. // Add view to list of views to provide.
  854. $views[$view->name] = $view;
  855. // Repeat all of the above for each view the module should provide. At the
  856. // end, return array of default views.
  857. return $views;
  858. }
  859. /**
  860. * Alter default views defined by other modules.
  861. *
  862. * This hook is called right before all default views are cached to the
  863. * database. It takes a keyed array of views by reference.
  864. *
  865. * Example usage to add a field to a view:
  866. * @code
  867. * $handler =& $view->display['DISPLAY_ID']->handler;
  868. * // Add the user name field to the view.
  869. * $handler->display->display_options['fields']['name']['id'] = 'name';
  870. * $handler->display->display_options['fields']['name']['table'] = 'users';
  871. * $handler->display->display_options['fields']['name']['field'] = 'name';
  872. * $handler->display->display_options['fields']['name']['label'] = 'Author';
  873. * $handler->display->display_options['fields']['name']['link_to_user'] = 1;
  874. * @endcode
  875. */
  876. function hook_views_default_views_alter(&$views) {
  877. if (isset($views['taxonomy_term'])) {
  878. $views['taxonomy_term']->display['default']->display_options['title'] = 'Categories';
  879. }
  880. }
  881. /**
  882. * Performs replacements in the query before being performed.
  883. *
  884. * @param object $view
  885. * The View being executed.
  886. *
  887. * @return array
  888. * An array with keys being the strings to replace, and the values the strings
  889. * to replace them with. The strings to replace are often surrounded with
  890. * '***', as illustrated in the example implementation.
  891. */
  892. function hook_views_query_substitutions($view) {
  893. // Example from views_views_query_substitutions().
  894. global $language_content;
  895. return array(
  896. '***CURRENT_VERSION***' => VERSION,
  897. '***CURRENT_TIME***' => REQUEST_TIME,
  898. '***CURRENT_LANGUAGE***' => $language_content->language,
  899. '***DEFAULT_LANGUAGE***' => language_default('language'),
  900. );
  901. }
  902. /**
  903. * This hook is called to get a list of placeholders and their substitutions.
  904. *
  905. * Used when preprocessing a View with form elements.
  906. *
  907. * @return array
  908. * An array with keys being the strings to replace, and the values the strings
  909. * to replace them with.
  910. */
  911. function hook_views_form_substitutions() {
  912. return array(
  913. '<!--views-form-example-substitutions-->' => 'Example Substitution',
  914. );
  915. }
  916. /**
  917. * Allows altering a view at the very beginning of processing a preview.
  918. *
  919. * Occurs before anything is done.
  920. *
  921. * This hook is only triggered when the one of the following are invoked:
  922. * - $view->execute_display()
  923. * - $view->preview()
  924. *
  925. * As such code placed in this hook will not fire during:
  926. * - $view->build()
  927. * - $view->execute()
  928. * - $view->render()
  929. *
  930. * Likely, hook_views_pre_build() or hook_views_pre_execute() are much better
  931. * choices for most use cases since they are always invoked, not just when
  932. * previewing a display.
  933. *
  934. * Adding output to the view can be accomplished by placing text on
  935. * $view->attachment_before and $view->attachment_after.
  936. *
  937. * @param object $view
  938. * The view object about to be processed.
  939. * @param string $display_id
  940. * The machine name of the active display.
  941. * @param array $args
  942. * An array of arguments passed into the view.
  943. */
  944. function hook_views_pre_view(&$view, &$display_id, &$args) {
  945. // Change the display if the acting user has 'administer site configuration'
  946. // permission, to display something radically different.
  947. // (Note that this is not necessarily the best way to solve that task. Feel
  948. // free to contribute another example!)
  949. if (
  950. $view->name == 'my_special_view'
  951. && user_access('administer site configuration')
  952. && $display_id == 'public_display'
  953. ) {
  954. $view->set_display('private_display');
  955. }
  956. }
  957. /**
  958. * Called after the display's pre_execute phase but before the build process.
  959. *
  960. * Adding output to the view can be accomplished by placing text on
  961. * $view->attachment_before and $view->attachment_after.
  962. *
  963. * @param object $view
  964. * The view object about to be processed.
  965. */
  966. function hook_views_pre_build(&$view) {
  967. // Because of some inexplicable business logic, we should remove all
  968. // attachments from all views on Mondays.
  969. // (This alter could be done later in the execution process as well.)
  970. if (date('D') == 'Mon') {
  971. unset($view->attachment_before);
  972. unset($view->attachment_after);
  973. }
  974. }
  975. /**
  976. * This hook is called right after the build process.
  977. *
  978. * The query is now fully built, but it has not yet been run through
  979. * db_rewrite_sql.
  980. *
  981. * Adding output to the view can be accomplished by placing text on
  982. * $view->attachment_before and $view->attachment_after.
  983. *
  984. * @param object $view
  985. * The view object about to be processed.
  986. */
  987. function hook_views_post_build(&$view) {
  988. // If the exposed field 'type' is set, hide the column containing the content
  989. // type. (Note that this is a solution for a particular view, and makes
  990. // assumptions about both exposed filter settings and the fields in the view.
  991. // Also note that this alter could be done at any point before the view being
  992. // rendered.)
  993. if ($view->name == 'my_view' && isset($view->exposed_raw_input['type']) && $view->exposed_raw_input['type'] != 'All') {
  994. // 'Type' should be interpreted as content type.
  995. if (isset($view->field['type'])) {
  996. $view->field['type']->options['exclude'] = TRUE;
  997. }
  998. }
  999. }
  1000. /**
  1001. * This hook is called right before the execute process.
  1002. *
  1003. * The query is now fully built, but it has not yet been run through
  1004. * db_rewrite_sql.
  1005. *
  1006. * Adding output to the view can be accomplished by placing text on
  1007. * $view->attachment_before and $view->attachment_after.
  1008. *
  1009. * @param object $view
  1010. * The view object about to be processed.
  1011. */
  1012. function hook_views_pre_execute(&$view) {
  1013. // Whenever a view queries more than two tables, show a message that notifies
  1014. // view administrators that the query might be heavy.
  1015. // (This action could be performed later in the execution process, but not
  1016. // earlier.)
  1017. if (count($view->query->tables) > 2 && user_access('administer views')) {
  1018. drupal_set_message(t('The view %view may be heavy to execute.', array('%view' => $view->name)), 'warning');
  1019. }
  1020. }
  1021. /**
  1022. * This hook is called right after the execute process.
  1023. *
  1024. * The query has been executed, but the pre_render() phase has not yet happened
  1025. * for handlers.
  1026. *
  1027. * Adding output to the view can be accomplished by placing text on
  1028. * $view->attachment_before and $view->attachment_after. Altering the content
  1029. * can be achieved by editing the items of $view->result.
  1030. *
  1031. * @param object $view
  1032. * The view object about to be processed.
  1033. */
  1034. function hook_views_post_execute(&$view) {
  1035. // If there are more than 100 results, show a message that encourages the user
  1036. // to change the filter settings.
  1037. // (This action could be performed later in the execution process, but not
  1038. // earlier.)
  1039. if ($view->total_rows > 100) {
  1040. drupal_set_message(t('You have more than 100 hits. Use the filter settings to narrow down your list.'));
  1041. }
  1042. }
  1043. /**
  1044. * This hook is called right before the render process.
  1045. *
  1046. * The query has been executed, and the pre_render() phase has already happened
  1047. * for handlers, so all data should be available.
  1048. *
  1049. * Adding output to the view can be accomplished by placing text on
  1050. * $view->attachment_before and $view->attachment_after. Altering the content
  1051. * can be achieved by editing the items of $view->result.
  1052. *
  1053. * This hook can be utilized by themes.
  1054. *
  1055. * @param object $view
  1056. * The view object about to be processed.
  1057. */
  1058. function hook_views_pre_render(&$view) {
  1059. // Scramble the order of the rows shown on this result page.
  1060. // Note that this could be done earlier, but not later in the view execution
  1061. // process.
  1062. shuffle($view->result);
  1063. }
  1064. /**
  1065. * Post process any rendered data.
  1066. *
  1067. * This can be valuable to be able to cache a view and still have some level of
  1068. * dynamic output. In an ideal world, the actual output will include HTML
  1069. * comment based tokens, and then the post process can replace those tokens.
  1070. *
  1071. * Example usage. If it is known that the view is a node view and that the
  1072. * primary field will be a nid, you can do something like this:
  1073. *
  1074. * <!--post-FIELD-NID-->
  1075. *
  1076. * And then in the post render, create an array with the text that should
  1077. * go there:
  1078. *
  1079. * strtr($output, array('<!--post-FIELD-1-->' => 'output for FIELD of nid 1');
  1080. *
  1081. * All of the cached result data will be available in $view->result, as well,
  1082. * so all ids used in the query should be discoverable.
  1083. *
  1084. * This hook can be utilized by themes.
  1085. *
  1086. * @param object $view
  1087. * The view object about to be processed.
  1088. * @param string $output
  1089. * A flat string with the rendered output of the view.
  1090. * @param array $cache
  1091. * The cache settings.
  1092. */
  1093. function hook_views_post_render(&$view, &$output, &$cache) {
  1094. // When using full pager, disable any time-based caching if there are less
  1095. // then 10 results.
  1096. if ($view->query->pager instanceof views_plugin_pager_full && $cache->options['type'] == 'time' && count($view->result) < 10) {
  1097. $cache['options']['results_lifespan'] = 0;
  1098. $cache['options']['output_lifespan'] = 0;
  1099. }
  1100. }
  1101. /**
  1102. * Alter the query before executing the query.
  1103. *
  1104. * This hook should be placed in MODULENAME.views.inc and it will be
  1105. * auto-loaded. MODULENAME.views.inc must be in the directory specified by the
  1106. * 'path' key returned by MODULENAME_views_api(), or the same directory as the
  1107. * .module file, if 'path' is unspecified.
  1108. *
  1109. * @param object $view
  1110. * The view object about to be processed.
  1111. * @param object $query
  1112. * An object describing the query.
  1113. *
  1114. * @see hook_views_query_substitutions()
  1115. */
  1116. function hook_views_query_alter(&$view, &$query) {
  1117. // (Example assuming a view with an exposed filter on node title.)
  1118. // If the input for the title filter is a positive integer, filter against
  1119. // node ID instead of node title.
  1120. if ($view->name == 'my_view' && is_numeric($view->exposed_raw_input['title']) && $view->exposed_raw_input['title'] > 0) {
  1121. // Traverse through the 'where' part of the query.
  1122. foreach ($query->where as &$condition_group) {
  1123. foreach ($condition_group['conditions'] as &$condition) {
  1124. // If this is the part of the query filtering on title, change the
  1125. // condition to filter on node ID.
  1126. if ($condition['field'] == 'node.title') {
  1127. $condition = array(
  1128. 'field' => 'node.nid',
  1129. 'value' => $view->exposed_raw_input['title'],
  1130. 'operator' => '=',
  1131. );
  1132. }
  1133. }
  1134. }
  1135. }
  1136. }
  1137. /**
  1138. * Alter the information box that (optionally) appears with a view preview.
  1139. *
  1140. * Includes query and performance statistics.
  1141. *
  1142. * This hook should be placed in MODULENAME.views.inc and it will be
  1143. * auto-loaded. MODULENAME.views.inc must be in the directory specified by the
  1144. * 'path' key returned by MODULENAME_views_api(), or the same directory as the
  1145. * .module file, if 'path' is unspecified.
  1146. *
  1147. * Warning: $view is not a reference in PHP4 and cannot be modified here. But it
  1148. * IS a reference in PHP5, and can be modified. Please be careful with it.
  1149. *
  1150. * @param array $rows
  1151. * An associative array with two keys:
  1152. * - query: An array of rows suitable for theme('table'), containing
  1153. * information about the query and the display title and path.
  1154. * - statistics: An array of rows suitable for theme('table'), containing
  1155. * performance statistics.
  1156. * @param object $view
  1157. * The view object.
  1158. *
  1159. * @see theme_table()
  1160. */
  1161. function hook_views_preview_info_alter(&$rows, $view) {
  1162. // Adds information about the tables being queried by the view to the query
  1163. // part of the info box.
  1164. $rows['query'][] = array(
  1165. t('<strong>Table queue</strong>'),
  1166. count($view->query->table_queue) . ': (' . implode(', ', array_keys($view->query->table_queue)) . ')',
  1167. );
  1168. }
  1169. /**
  1170. * This hooks allows to alter the links at the top of the view edit form.
  1171. *
  1172. * Some modules might want to add links there.
  1173. *
  1174. * @param array $links
  1175. * An array of links which will be displayed at the top of the view edit form.
  1176. * Each entry should be on a form suitable for theme('link').
  1177. * @param object $view
  1178. * The full view object which is currently edited.
  1179. * @param string $display_id
  1180. * The current display id which is edited. For example that's 'default' or
  1181. * 'page_1'.
  1182. */
  1183. function hook_views_ui_display_top_links_alter(&$links, $view, $display_id) {
  1184. // Put the export link first in the list.
  1185. if (isset($links['export'])) {
  1186. $links = array('export' => $links['export']) + $links;
  1187. }
  1188. }
  1189. /**
  1190. * Allows altering the commands which are used on a views AJAX request.
  1191. *
  1192. * @param array $commands
  1193. * An array of ajax commands.
  1194. * @param object $view
  1195. * The view which is requested.
  1196. */
  1197. function hook_views_ajax_data_alter(&$commands, $view) {
  1198. // Replace Views' method for scrolling to the top of the element with your
  1199. // custom scrolling method.
  1200. foreach ($commands as &$command) {
  1201. if ($command['command'] == 'viewsScrollTop') {
  1202. $command['command'] .= 'myScrollTop';
  1203. }
  1204. }
  1205. }
  1206. /**
  1207. * Allow modules to respond to the Views cache being invalidated.
  1208. *
  1209. * This hook should fire whenever a view is enabled, disabled, created,
  1210. * updated, or deleted.
  1211. *
  1212. * @param string $cid
  1213. * The cache identifier that is being cleared.
  1214. *
  1215. * @see views_invalidate_cache()
  1216. */
  1217. function hook_views_invalidate_cache($cid) {
  1218. cache_clear_all('views:*', 'cache_mymodule', TRUE);
  1219. }
  1220. /**
  1221. * Allow modules to alter a view prior to being saved.
  1222. */
  1223. function hook_views_view_presave($view) {
  1224. // Do some adjustments to the view. Handle with care.
  1225. if (mymodule_check_view($view)) {
  1226. mymodule_do_some_voodoo($view);
  1227. }
  1228. }
  1229. /**
  1230. * Allow modules to respond to a view being saved.
  1231. */
  1232. function hook_views_view_save($view) {
  1233. // Make a watchdog entry.
  1234. watchdog('views', 'The view @name was deleted by @user at @time', array('@name' => $view->name, '@user' => $GLOBALS['user']->name, '@time' => format_date(time())));
  1235. }
  1236. /**
  1237. * Allow modules to respond to a view being deleted or reverted.
  1238. */
  1239. function hook_views_view_delete($view) {
  1240. // Make a watchdog entry.
  1241. watchdog('views', 'The view @name was deleted by @user at @time', array('@name' => $view->name, '@user' => $GLOBALS['user']->name, '@time' => format_date(time())));
  1242. }
  1243. /**
  1244. * @}
  1245. */
  1246. /**
  1247. * @defgroup views_module_handlers Views module handlers
  1248. * @{
  1249. * Handlers exposed by various modules to Views.
  1250. * @}
  1251. */