Mercurial > defr > drupal > popups_reference
comparison popups_reference.module @ 5:e1318a313b1d
Popups: AddnReference 1.3
author | Franck Deroche <franck@defr.org> |
---|---|
date | Thu, 19 Mar 2009 11:34:01 +0100 |
parents | 56772e0a00ae |
children | 990f71344a66 |
comparison
equal
deleted
inserted
replaced
0:56772e0a00ae | 5:e1318a313b1d |
---|---|
1 <?php | 1 <?php |
2 // $Id: popups_reference.module,v 1.1.2.6 2009/01/18 22:40:33 starbow Exp $ | 2 // $Id: popups_reference.module,v 1.1.2.12 2009/03/07 06:54:25 starbow Exp $ |
3 | 3 |
4 /** | 4 /** |
5 * @file | 5 * @file |
6 * Modify the Node Reference widget to use a popup to add a new node. | 6 * Modify the Node Reference widget to use a popup to add a new node. |
7 */ | 7 */ |
11 * Implementation of hook_form_alter(). | 11 * Implementation of hook_form_alter(). |
12 * | 12 * |
13 * Modifies the nodereference setting form and the basic node form. | 13 * Modifies the nodereference setting form and the basic node form. |
14 */ | 14 */ |
15 function popups_reference_form_alter(&$form, $form_state, $form_id) { | 15 function popups_reference_form_alter(&$form, $form_state, $form_id) { |
16 if ($form_id == 'content_field_edit_form') { | 16 if ($form_id == 'content_field_edit_form' && $form['#field']['type'] == 'nodereference') { |
17 // Add a checkbox to the nodereference settings page. | 17 // Add a checkbox to the nodereference settings page. |
18 $field_name = $form['#field']['field_name']; | 18 $field_name = $form['#field']['field_name']; |
19 $form['field']['show_add_link'] = array( | 19 $form['field']['show_add_link'] = array( |
20 '#type' => 'checkbox', | 20 '#type' => 'checkbox', |
21 '#default_value' => variable_get('popups_reference_show_add_link_'. $field_name, TRUE), | 21 '#default_value' => variable_get('popups_reference_show_add_link_'. $field_name, TRUE), |
55 */ | 55 */ |
56 function popups_reference_nodeapi($node, $op) { | 56 function popups_reference_nodeapi($node, $op) { |
57 if ($op == 'insert') { | 57 if ($op == 'insert') { |
58 $five = time()+300; // 5 minutes in the future. | 58 $five = time()+300; // 5 minutes in the future. |
59 setcookie("PopupRefNid", $node->nid, $five, '/'); | 59 setcookie("PopupRefNid", $node->nid, $five, '/'); |
60 setcookie("PopupRefTitle", $node->title, $five, '/'); | 60 // setcookie("PopupRefTitle", $node->title, $five, '/'); |
61 setrawcookie("PopupRefTitle", rawurlencode($node->title), $five, '/'); | |
61 } | 62 } |
62 } | 63 } |
63 | 64 |
64 /** | 65 /** |
65 * Submit added to the the nodereference settings form. | 66 * Submit added to the the nodereference settings form. |
83 $field_name = strstr($key, 'field_'); // Check if $key starts with 'field_'; | 84 $field_name = strstr($key, 'field_'); // Check if $key starts with 'field_'; |
84 if (isset($fields[$field_name]) && | 85 if (isset($fields[$field_name]) && |
85 $fields[$field_name]['type'] == 'nodereference' && | 86 $fields[$field_name]['type'] == 'nodereference' && |
86 variable_get('popups_reference_show_add_link_'. $field_name, TRUE)) { | 87 variable_get('popups_reference_show_add_link_'. $field_name, TRUE)) { |
87 $type = $form['type']['#value']; | 88 $type = $form['type']['#value']; |
88 $field = content_fields($field_name, $form['type']['#value']); | 89 $field = content_fields($field_name, $type); |
89 | 90 |
90 $wrapper_id = 'popups-reference-' . _popups_reference_counter(); | 91 $wrapper_id = 'popups-reference-' . _popups_reference_counter(); |
91 $links = _popups_reference_links($field, $type, $wrapper_id, $field['widget']['type']); | 92 $links = _popups_reference_links($field, $type, $wrapper_id, $field['widget']['type']); |
92 if ($links) { | 93 if ($links) { |
93 // Put the nodereference widget and links in an wrapper. | 94 // Put the nodereference widget and links in an wrapper. |
94 // Makes it easy to find for Ahah targeting, and popups_reference behavior selecting. | 95 // Makes it easy to find for Ahah targeting, and popups_reference behavior selecting. |
103 * for each allowed content type | 104 * for each allowed content type |
104 * | 105 * |
105 * @param $field | 106 * @param $field |
106 * @param $src_type - the type of base node. | 107 * @param $src_type - the type of base node. |
107 * @param $wrapper_id - id for the wrapper around the node reference. | 108 * @param $wrapper_id - id for the wrapper around the node reference. |
108 * @param $type - the type of the node being referenced. | 109 * @param $type - the type of widget. |
109 * @return Array of html links. | 110 * @return Array of html links. |
110 */ | 111 */ |
111 function _popups_reference_links($field, $src_type, $wrapper_id, $type) { | 112 function _popups_reference_links($field, $src_type, $wrapper_id, $widget_type) { |
112 if ($type == 'nodereference_select' || $type == 'nodereference_buttons') { | 113 if ($widget_type == 'nodereference_select' || $widget_type == 'nodereference_buttons') { |
113 // Target the wrapper for replacing. | 114 // Target the wrapper for replacing. |
114 popups_add_popups(array('a.'.$wrapper_id=>array('targetSelectors'=>array('#'.$wrapper_id)))); | 115 popups_add_popups(array('a.'.$wrapper_id=>array('targetSelectors'=>array('#'.$wrapper_id)))); |
115 } | 116 } |
116 else if ($type == 'nodereference_autocomplete') { | 117 else if ($widget_type == 'nodereference_autocomplete') { |
117 // Don't replace the autocomplete when done. | 118 // Don't replace the autocomplete when done. |
118 popups_add_popups(array('a.'.$wrapper_id=>array('noUpdate'=>TRUE))); | 119 popups_add_popups(array('a.'.$wrapper_id=>array('noUpdate'=>TRUE))); |
119 } | 120 } |
120 else { // Unsupported type. | 121 else { // Unsupported type. |
121 return; | 122 return; |
128 'query' => array('destination' => 'node/add/' . str_replace('_', '-', $src_type)), | 129 'query' => array('destination' => 'node/add/' . str_replace('_', '-', $src_type)), |
129 ); | 130 ); |
130 $links = array(); | 131 $links = array(); |
131 $all_types = node_get_types(); | 132 $all_types = node_get_types(); |
132 foreach ($field['referenceable_types'] as $add_type => $value) { | 133 foreach ($field['referenceable_types'] as $add_type => $value) { |
133 if (!empty($value) && user_access("create $add_type content")) { | 134 if (!empty($value) && (user_access("create $add_type content") || user_access('administer nodes'))) { |
135 //if (!empty($value) && user_access("create $add_type content")) { | |
134 drupal_add_js(drupal_get_path('module', 'popups_reference') .'/popups_reference.js'); | 136 drupal_add_js(drupal_get_path('module', 'popups_reference') .'/popups_reference.js'); |
135 $path = 'node/add/' . str_replace('_', '-', $add_type); | 137 $path = 'node/add/' . str_replace('_', '-', $add_type); |
136 $links[] = l("Add $add_type", $path, $options); | 138 $name = $all_types[$add_type]->name; |
139 $links[] = l("Add $name", $path, $options); | |
137 } | 140 } |
138 } | 141 } |
139 return $links; | 142 return $links; |
140 } | 143 } |
141 | 144 |