annotate sites/all/modules/views/views.api.php @ 0:ff03f76ab3fe

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