- Home
- Blogs
- Guy Schneerson's blog
- Drupal Node actions
Drupal Node actions
Drupal nodes typically have an Edit and View links (rendered as tabs at the top of the node by most themes).
Ever wanted to add you own links so you can add custom functionality?
This can be used to automatically populate node data.
The below example will show you how to add a link titled “reviewed” to an article node type, this link will prompt the user for a comment and set two cck field “reviewed” and “note”.
This is not intended as a real example but as an illustration of the how the following techniques can be used as the bases for the creation of powerful work flow.
The techniques covered are
• hooking into the Drupal menu routing system
• form creation and validation
• node manipulation (node_load and node_save)
• url redirect
Step 1 hook into the Drupal menu routing using hook_menu()
/** * Implementation of hook_menu(). */ function mymodule_menu() { // Add the artical node the reviewed link $items['node/%/artical_reviewd'] = array( 'title' => 'set article as reviewd', 'page callback' => 'mymodule_artical_reviewd_callback', 'page arguments' => array(1), 'access callback' => 'mymodule_artical_reviewd_access_callback', 'access arguments' => array(1), 'type' => MENU_LOCAL_TASK, 'weight' => 3, ); return $items; }
Step 2 implement Menu an access callback to make sure the user has permissions to edit the node
/** * Menu access callback * make sure user has permissions to edit an article node * and that the function was called with an article nid */ function mymodule_artical_reviewd_access_callback($nid) { $node = node_load($nid); if (user_access('create artical content') && $node->type == 'artical') { return TRUE; } else { return FALSE; }
Step 3 implement the menu callback and call the form
/** * Menu callback * make sure to pass the node id to the form */ function mymodule_artical_reviewd_callback($nid) { $out = sprintf('<H2>%s</H2>', t('Set article as reviewd')); $out .= drupal_get_form(' mymodule_artical_reviewd_form', $nid); return $out; }
Step 4 Build the drupal form
/** * return the form array with the note field and a hidden value containing the node id */ function mymodule_artical_reviewd_form($form_state, $nid) { $form['election_records'] = array( '#title' => t('note'), '#type' => 'textfield', '#description' => t('Please enter a note') ); $form['nid'] = array( '#type' => 'value', '#value' => $nid ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit') ); return $form; }
Step 5 add validation if needed
/** * Basic Validation */ function mymodule_artical_reviewd_form_validate($form, &$form_state) { if (empty($form_state['values']['note'])) form_set_error( t('Please enter a note')); } }
Step 6 on submit set the cck fields and optionally redirect to the edit form for some more editing
/** * Submit */ function mymodule_artical_reviewd_form_submit($form, &$form_state) { $note= $form_state['values']['note']; $nid = $form_state['values']['nid']; //load the node $node = node_load($nid) //set node properies $node->field_article_reviewed[0]['value'] = $note; $node->field_article_note[0]['value'] = 1; // node_submit is not strictly needed here as we have not modified the body but // I left it in to make the point that you should call it if the node body has changed $node=node_submit($node); // Save node_save($node); //set a msg to the user drupal_set_message(sprintf('Article is now reviewed: %s', $note )); // Go back to the node edit drupal_goto("node/$nid/edit"); }
hope the code is self explanatory but if not let me know and I will add more explanations
Contact Details
Blue-Bag Ltd
- info [at] blue-bag.com
- Telephone: 0843 2894522
- Blue-Bag HQ:
The Garage, Manor Farm
Chilcompton, Radstock
Somerset, BA3 4HP, United Kingdom - Telephone: (+44) 01761 411542
- Blue-Bag Brighton:
Unit 35 Level 6 North, New England House
New England Street, Brighton
BN1 4GH United Kingdom - Telephone: (+44) 07944 938204
- VAT GB 748125034
- UK Company Reg: 3932829