- <?php
- * @file
- * Hooks implementations for the Contextual Links Example module.
- */
-
- * @defgroup contextual_links_example Example: Contextual Links
- * @ingroup examples
- * @{
- * This example demonstrates how to implement contextual links.
- */
-
- * Implements hook_menu().
- *
- * Backdrop's menu system allows you to indicate that particular menu items
- * should be displayed as contextual links. If you hover over a block or node
- * while logged in as an administrator (and with the Contextual Links module
- * enabled) you'll see a small gear icon appear. Click on this icon, and the
- * list of items that appears in the exposed menu are what Backdrop calls
- * "contextual links".
- *
- * Contextual links allow site administrators to quickly perform actions
- * related to elements on a page, without having to hunt through the
- * administrative interface. As such, you should usually attach them to objects
- * that appear on the main part of a Backdrop site and limit them to a few common
- * tasks that are frequently performed (for example, "edit" or "configure").
- * Do not rely on contextual links being present for your module to work
- * correctly, since they are a convenience feature only. Within Backdrop core,
- * the Contextual Links module must be enabled (and the user viewing the page
- * must have the "access contextual links" permission) in order for the
- * contextual links corresponding to actions that the user can perform to
- * actually be injected into the page's HTML.
- *
- * Three examples of contextual links are provided here. Although none are
- * difficult to implement, they are presented in order of increasing
- * complexity:
- * - Attaching contextual links to a node.
- * - Attaching contextual links to a block.
- * - Attaching contextual links to an arbitrary piece of content defined by
- * your module.
- *
- * @see contextual_links_example_block_info()
- * @see contextual_links_example_block_view()
- * @see contextual_links_overview_page()
- */
- function contextual_links_example_menu() {
-
-
-
-
-
-
-
-
-
-
-
-
- $items['node/%node/example-action'] = array(
- 'title' => 'Example action',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array('contextual_links_example_node_action_form', 1),
- 'access callback' => TRUE,
-
-
- 'type' => MENU_LOCAL_TASK,
-
-
-
-
-
-
-
-
- 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
-
-
-
- 'weight' => 80,
- );
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $items['examples/contextual-links/%contextual_links_example_object'] = array(
- 'title' => 'Contextual links example object',
- 'page callback' => 'contextual_links_example_object_page',
- 'page arguments' => array(2),
- 'access callback' => TRUE,
- );
- $items['examples/contextual-links/%contextual_links_example_object/view'] = array(
- 'title' => 'View',
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'weight' => -10,
- );
- $items['examples/contextual-links/%contextual_links_example_object/edit'] = array(
- 'title' => 'Edit object',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array('contextual_links_example_object_edit_form', 2),
- 'access callback' => TRUE,
- 'type' => MENU_LOCAL_TASK,
-
-
- 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
- );
-
-
-
-
-
-
-
-
-
-
-
- $items['examples/contextual-links'] = array(
- 'title' => 'Contextual Links Example',
- 'page callback' => 'contextual_links_overview_page',
- 'access callback' => TRUE,
- );
-
- return $items;
- }
-
- * Menu loader callback for the object defined by this module.
- *
- * @param int $id
- * The ID of the object to load.
- *
- * @return object|FALSE
- * A fully loaded object, or FALSE if the object does not exist.
- */
- function contextual_links_example_object_load($id) {
-
-
-
- if (is_numeric($id)) {
- $object = new stdClass();
- $object->id = $id;
- $object->title = t('Title for example object @id', array('@id' => $id));
- $object->content = t('This is the content of example object @id.', array('@id' => $id));
- return $object;
- }
- else {
- return FALSE;
- }
- }
-
- * Implements hook_block_info().
- */
- function contextual_links_example_block_info() {
-
- $blocks['example']['info'] = t('Contextual links example block');
- return $blocks;
- }
-
- * Implements hook_block_view().
- */
- function contextual_links_example_block_view($delta = '') {
- if ($delta == 'example') {
-
-
-
-
- $id = 1;
- $object = contextual_links_example_object_load($id);
- $block['subject'] = t('Contextual links example block for object @id', array('@id' => $id));
- $block['content'] = array(
-
-
-
- '#type' => 'markup',
- '#markup' => filter_xss($object->content),
-
-
-
-
- '#contextual_links' => array(
- 'contextual_links_example' => array(
-
-
-
-
-
-
-
-
-
-
-
- 'examples/contextual-links',
- array($id),
- ),
- ),
- );
-
-
-
-
-
-
- return $block;
- }
- }
-
- * Menu callback; displays a listing of objects defined by this module.
- *
- * @see contextual_links_example_theme()
- * @see contextual-links-example-object.tpl.php
- * @see contextual_links_example_block_view()
- */
- function contextual_links_overview_page() {
- $build = array();
-
-
-
- for ($id = 1; $id <= 5; $id++) {
- $object = contextual_links_example_object_load($id);
- $build[$id] = array(
-
-
-
-
- '#theme' => 'contextual_links_example_object',
- '#object' => $object,
-
-
-
- '#contextual_links' => array(
- 'contextual_links_example' => array(
- 'examples/contextual-links',
- array($id),
- ),
- ),
- );
- }
-
- return $build;
- }
-
- * Implements hook_theme().
- *
- * @see template_preprocess_contextual_links_example_object()
- */
- function contextual_links_example_theme() {
-
-
-
-
-
-
-
-
-
- return array(
- 'contextual_links_example_object' => array(
- 'template' => 'contextual-links-example-object',
- 'render element' => 'element',
- ),
- );
- }
-
- * Process variables for contextual-links-example-object.tpl.php.
- *
- * @see contextual_links_overview_page()
- */
- function template_preprocess_contextual_links_example_object(&$variables) {
-
-
- $variables['title'] = filter_xss($variables['element']['#object']->title);
- $variables['content'] = filter_xss($variables['element']['#object']->content);
- }
-
- * Menu callback; displays an object defined by this module on its own page.
- *
- * @see contextual_links_overview_page()
- */
- function contextual_links_example_object_page($object) {
-
-
-
- $build = array(
- '#theme' => 'contextual_links_example_object',
- '#object' => $object,
- );
-
- return $build;
- }
-
- * Form callback; display the form for editing our module's content.
- *
- * @ingroup forms
- * @see contextual_links_example_object_edit_form_submit()
- */
- function contextual_links_example_object_edit_form($form, &$form_state, $object) {
- $form['text'] = array(
- '#markup' => t('This is the page that would allow you to edit object @id.', array('@id' => $object->id)),
- '#prefix' => '<p>',
- '#suffix' => '</p>',
- );
- $form['object_id'] = array(
- '#type' => 'value',
- '#value' => $object->id,
- );
-
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit'),
- );
-
- return $form;
- }
-
- * Submit handler for contextual_links_example_object_edit_form().
- */
- function contextual_links_example_object_edit_form_submit($form, &$form_state) {
- backdrop_set_message(t('Object @id was edited.', array('@id' => $form_state['values']['object_id'])));
- }
-
- * Form callback; display the form for performing an example action on a node.
- *
- * @ingroup forms
- * @see contextual_links_example_node_action_form_submit()
- */
- function contextual_links_example_node_action_form($form, &$form_state, $node) {
- $form['text'] = array(
- '#markup' => t('This is the page that would allow you to perform an example action on node @nid.', array('@nid' => $node->nid)),
- '#prefix' => '<p>',
- '#suffix' => '</p>',
- );
- $form['nid'] = array(
- '#type' => 'value',
- '#value' => $node->nid,
- );
-
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit'),
- );
-
- return $form;
- }
-
- * Submit handler for contextual_links_example_node_action_form().
- */
- function contextual_links_example_node_action_form_submit($form, &$form_state) {
- backdrop_set_message(t('The example action was performed on node @nid.', array('@nid' => $form_state['values']['nid'])));
- }
- * @} End of "defgroup contextual_links_example".
- */