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