annotate popups_reference.module @ 1:ece8e4be4d6f

Fix node titles after the creation
author Franck Deroche <franck@defr.org>
date Wed, 21 Jan 2009 11:34:38 +0100
parents 56772e0a00ae
children 98642e828c60
rev   line source
franck@0 1 <?php
franck@0 2 // $Id: popups_reference.module,v 1.1.2.6 2009/01/18 22:40:33 starbow Exp $
franck@0 3
franck@0 4 /**
franck@0 5 * @file
franck@0 6 * Modify the Node Reference widget to use a popup to add a new node.
franck@0 7 */
franck@0 8
franck@0 9
franck@0 10 /**
franck@0 11 * Implementation of hook_form_alter().
franck@0 12 *
franck@0 13 * Modifies the nodereference setting form and the basic node form.
franck@0 14 */
franck@0 15 function popups_reference_form_alter(&$form, $form_state, $form_id) {
franck@0 16 if ($form_id == 'content_field_edit_form') {
franck@0 17 // Add a checkbox to the nodereference settings page.
franck@0 18 $field_name = $form['#field']['field_name'];
franck@0 19 $form['field']['show_add_link'] = array(
franck@0 20 '#type' => 'checkbox',
franck@0 21 '#default_value' => variable_get('popups_reference_show_add_link_'. $field_name, TRUE),
franck@0 22 '#title' => t('Show the "Add New: Node Type" Popup links'),
franck@0 23 '#description' => t("Activate Popups:Add and Reference behavior for this reference.")
franck@0 24 );
franck@0 25 $form['#submit'][] = '_popups_reference_manage_fields_submit';
franck@0 26 }
franck@0 27 elseif (isset($form['type'])) {
franck@0 28 // Add the "Add New: Node Type" links.
franck@0 29 $node = $form['#node'];
franck@0 30 if ($form['type']['#value'] .'_node_form' == $form_id) {
franck@0 31 $fields = content_fields();
franck@0 32 foreach ($form as $key => $item) {
franck@0 33 if (is_array($item)) {
franck@0 34 $type = $item['#type'];
franck@0 35 if ($type == 'fieldset') { // Loop through all the subitems.
franck@0 36 foreach ($form[$key] as $subkey => $subitem) {
franck@0 37 popups_reference_alter_item($form[$key], $subkey, $subitem, $fields);
franck@0 38 }
franck@0 39 }
franck@0 40 else {
franck@0 41 popups_reference_alter_item($form, $key, $item, $fields);
franck@0 42 }
franck@0 43 }
franck@0 44
franck@0 45 }
franck@0 46 }
franck@0 47 }
franck@0 48 }
franck@0 49
franck@0 50 /**
franck@0 51 * Implementation of hook_nodeapi().
franck@0 52 * Add cookies with node info when a new node is created.
franck@0 53 * These cookies will be found by the popups_reference behavior and used
franck@0 54 * to select the newly created node in the reference widget.
franck@0 55 */
franck@0 56 function popups_reference_nodeapi($node, $op) {
franck@0 57 if ($op == 'insert') {
franck@0 58 $five = time()+300; // 5 minutes in the future.
franck@0 59 setcookie("PopupRefNid", $node->nid, $five, '/');
franck@1 60 setrawcookie("PopupRefTitle", rawurlencode($node->title), $five, '/');
franck@0 61 }
franck@0 62 }
franck@0 63
franck@0 64 /**
franck@0 65 * Submit added to the the nodereference settings form.
franck@0 66 * Set a variable for each nodereference field.
franck@0 67 */
franck@0 68 function _popups_reference_manage_fields_submit($form, &$form_state) {
franck@0 69 $field_name = $form['#field']['field_name'];
franck@0 70 variable_set('popups_reference_show_add_link_'. $field_name, $form_state['values']['show_add_link']);
franck@0 71 }
franck@0 72
franck@0 73 /**
franck@0 74 * Run on every element in the basic node form.
franck@0 75 * Wrap the enabled nodereference fields, and add the popup links.
franck@0 76 *
franck@0 77 * @param $form - the form (or fieldgroup).
franck@0 78 * @param $key - form element name.
franck@0 79 * @param $item - the form element array.
franck@0 80 * @param $fields - all fields info.
franck@0 81 */
franck@0 82 function popups_reference_alter_item(&$form, $key, $item, $fields) {
franck@0 83 $field_name = strstr($key, 'field_'); // Check if $key starts with 'field_';
franck@0 84 if (isset($fields[$field_name]) &&
franck@0 85 $fields[$field_name]['type'] == 'nodereference' &&
franck@0 86 variable_get('popups_reference_show_add_link_'. $field_name, TRUE)) {
franck@0 87 $type = $form['type']['#value'];
franck@0 88 $field = content_fields($field_name, $form['type']['#value']);
franck@0 89
franck@0 90 $wrapper_id = 'popups-reference-' . _popups_reference_counter();
franck@0 91 $links = _popups_reference_links($field, $type, $wrapper_id, $field['widget']['type']);
franck@0 92 if ($links) {
franck@0 93 // Put the nodereference widget and links in an wrapper.
franck@0 94 // Makes it easy to find for Ahah targeting, and popups_reference behavior selecting.
franck@0 95 $form[$key]['#prefix'] = '<div id="'. $wrapper_id .'">';
franck@0 96 $form[$key]['#suffix'] = '<div>Add New: ' . implode(', ', $links) .'</div></div>';
franck@0 97 }
franck@0 98 }
franck@0 99 }
franck@0 100
franck@0 101 /**
franck@0 102 * Generates 'Add new...' link
franck@0 103 * for each allowed content type
franck@0 104 *
franck@0 105 * @param $field
franck@0 106 * @param $src_type - the type of base node.
franck@0 107 * @param $wrapper_id - id for the wrapper around the node reference.
franck@0 108 * @param $type - the type of the node being referenced.
franck@0 109 * @return Array of html links.
franck@0 110 */
franck@0 111 function _popups_reference_links($field, $src_type, $wrapper_id, $type) {
franck@0 112 if ($type == 'nodereference_select' || $type == 'nodereference_buttons') {
franck@0 113 // Target the wrapper for replacing.
franck@0 114 popups_add_popups(array('a.'.$wrapper_id=>array('targetSelectors'=>array('#'.$wrapper_id))));
franck@0 115 }
franck@0 116 else if ($type == 'nodereference_autocomplete') {
franck@0 117 // Don't replace the autocomplete when done.
franck@0 118 popups_add_popups(array('a.'.$wrapper_id=>array('noUpdate'=>TRUE)));
franck@0 119 }
franck@0 120 else { // Unsupported type.
franck@0 121 return;
franck@0 122 }
franck@0 123 $options = array(
franck@0 124 'attributes' => array(
franck@0 125 'class' => $wrapper_id . ' popups-reference',
franck@0 126 'rel' => $wrapper_id,
franck@0 127 ),
franck@0 128 'query' => array('destination' => 'node/add/' . str_replace('_', '-', $src_type)),
franck@0 129 );
franck@0 130 $links = array();
franck@0 131 $all_types = node_get_types();
franck@0 132 foreach ($field['referenceable_types'] as $add_type => $value) {
franck@0 133 if (!empty($value) && user_access("create $add_type content")) {
franck@0 134 drupal_add_js(drupal_get_path('module', 'popups_reference') .'/popups_reference.js');
franck@0 135 $path = 'node/add/' . str_replace('_', '-', $add_type);
franck@0 136 $links[] = l("Add $add_type", $path, $options);
franck@0 137 }
franck@0 138 }
franck@0 139 return $links;
franck@0 140 }
franck@0 141
franck@0 142 /**
franck@0 143 * A counter for generating unique element id's.
franck@0 144 *
franck@0 145 * @return int: next integer.
franck@0 146 */
franck@0 147 function _popups_reference_counter() {
franck@0 148 static $count = 0;
franck@0 149 return $count++;
franck@0 150 }
franck@0 151