diff --git a/.project b/.project new file mode 100644 index 0000000000000000000000000000000000000000..3418a995911501f5fa2f340f9671402fc874b24b --- /dev/null +++ b/.project @@ -0,0 +1,22 @@ + + + popups_reference62 + + + + + + org.eclipse.php.core.PhpIncrementalProjectBuilder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.php.core.PHPNature + + diff --git a/.settings/org.eclipse.php.core.projectOptions.prefs b/.settings/org.eclipse.php.core.projectOptions.prefs new file mode 100644 index 0000000000000000000000000000000000000000..8a30f77cf3c97eba83fc77590a1f8319d23e45c0 --- /dev/null +++ b/.settings/org.eclipse.php.core.projectOptions.prefs @@ -0,0 +1,5 @@ +#Thu Mar 19 18:19:44 PDT 2009 +eclipse.preferences.version=1 +org.eclipse.php.core.contextRoot= +org.eclipse.php.core.defaultEncoding= +org.eclipse.php.core.includePath=\r\n\r\n\r\n diff --git a/CHANGELOG.txt b/CHANGELOG.txt index b3b597b6f405beff5dd2413742c3d3b0d7c46547..03091bc854a2659d82c97d3b561f2e7ce635a042 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1 +1,28 @@ -Working w/Popups Api RC2 \ No newline at end of file +Drupal-6-1-0-RC3 + * Recommended for use with Popups API 6.x-1.2 +Bug Fixes + * http://drupal.org/node/390146 (cYu) - Allow users with "admin nodes" permission to see links. + * http://drupal.org/node/388406 - Only autofill a single autocomplete field if there are mutliples. + +Drupal-6-1-0-RC2 +Bug Fixes + * http://drupal.org/node/361964 (DeFr) - Bad unicode in autocomplete + * http://drupal.org/node/379576 - RC1 checkbox showing up on all fields. +Other + * http://drupal.org/node/380932 (KarenS) - Setting min php to 5.0 in .info file. + +Drupal-6-1-0-RC1 +New Features + * http://drupal.org/node/310019 - Checkbox to toggle the "Add New:" links per field. Funded by Agaric Design. + * http://drupal.org/node/308589 - Select the newly created node (requres Popups API 1.2-ALPHA1 or newer). Funded by Agaric Design + * Added support for radio buttons widget. + * http://drupal.org/node/359524 by Cloudy - use human type names in link text. +Other + * Better comments and function documentation. + +Drupal-6--1-0-BETA2 + * http://drupal.org/node/322600 - test if user has permission to do any adds before showing "Add new" text. + * http://drupal.org/node/325720 (jhodgdon ) - fix for content types with '_'s in them. + +Drupal-6--1-0-BETA1 +* Working w/Popups Api RC2 \ No newline at end of file diff --git a/README.txt b/README.txt index db76f82fcdbfc662135196c5db3acd1e6f22f791..0076e5cff25c0c5424f21241c0809707a20a4ccb 100644 --- a/README.txt +++ b/README.txt @@ -3,5 +3,8 @@ Initial inspiration: Add and Reference by tema (http://drupal.org/project/add_n_ TODO * Fix losing highlighted selections on node add with multi-select box. - * Auto choose newly created node. + +LIMITATIONS + * http://drupal.org/node/378988 - conflicts with Hierarchical Select. + \ No newline at end of file diff --git a/popups_reference.info b/popups_reference.info index ebb7b7ce13d8a2efc5f2a64d53e21f6d8f621d72..b1e503030d1032043d0eba19510c5fbc1d3e7369 100644 --- a/popups_reference.info +++ b/popups_reference.info @@ -3,4 +3,6 @@ name = Popups: Add & Reference description = In-place popup to add a new node to Node Reference widget. package = User interface core = 6.x -dependencies[] = popups +php = 5.0 +dependencies[] = popups +dependencies[] = content diff --git a/popups_reference.js b/popups_reference.js new file mode 100644 index 0000000000000000000000000000000000000000..56018e5a7244ca5d9b79c122dc54c2c3f1276e99 --- /dev/null +++ b/popups_reference.js @@ -0,0 +1,63 @@ +// $Id$ + +/** + * Popups: Add and Reference behavior + * + * Adds the behavior of selecting the newly created node. + */ + +/** + * Parse the cookies to find a value. + * + * @param name of cookie value. + */ +function popups_reference_get_cookie_value(name) { + name += '='; + var cookies = document.cookie.split(';'); + for (var i=0; i < cookies.length; i++) { + var cookie = jQuery.trim(cookies[i]); + if (cookie.indexOf(name) === 0) { + return cookie.substring(name.length, cookie.length); + } + } +} + +/** + * Attach the behavior. + */ +Drupal.behaviors.popups_reference = function(context) { + $('.popups-reference', context).not('.popups-reference-processed').each(function() { + $(this).addClass('popups-reference-processed'); // Don't re-add to processed links. + $(this).click(function() { + var rel = $(this).attr('rel'); // Rel attribute of the clicked link is the wrapper id. +// var $wrapper = $('#' + rel); + // Unbind namespaced event, so bindings don't pile up every click. + $(document).unbind('popups_form_success.popups_reference'); + + // Bind to the popups API custom form_success event. +// $(document).bind('popups_form_success.popups_reference', function(obj, element, options, targetLayerSelector) { + $(document).bind('popups_form_success.popups_reference', function(obj, popup) { + var $wrapper = $(popup.targetLayerSelector() + ' div.' + rel); + console.log("popups_form_success.popups_reference"); + console.log($wrapper); + // Info about the new node was placed in a cookie when it was created. + var nid = popups_reference_get_cookie_value('PopupRefNid'); + var title = decodeURIComponent(popups_reference_get_cookie_value('PopupRefTitle')); + $wrapper.find('select').val(nid); // Select + $wrapper.find(':radio[value=' + nid + ']').select(); // Radio buttons + + // Get the first empty autocomplete field to fill (http://drupal.org/node/388406). + $emptyAutos = $wrapper.find('input.form-autocomplete').filter(function(i) { + return !$(this).val(); + }); + if ($emptyAutos.length) { + $emptyAutos.eq(0).val(title); + } + else { // There are no empty fields, use the first one. + $wrapper.find('input.form-autocomplete:first').val(title); + } + }); + }); + }); +}; + diff --git a/popups_reference.module b/popups_reference.module index 6ef6e1cc67ac10d8a7ee74906e9d6b80f48e8074..9513fc5d05b0390dae3666e0bb2ec984e032333e 100644 --- a/popups_reference.module +++ b/popups_reference.module @@ -6,55 +6,33 @@ * Modify the Node Reference widget to use a popup to add a new node. */ -function _popups_reference_counter() { - static $count = 0; - return $count++; -} - -/** - * Generates 'Add new...' link - * for each allowed content type - */ -function _popups_reference_links($field, $src_type, $id=null) { - if ($id) { // Creating link to select box. - $pclass = $id; - // Target just the select box for replacing. - popups_add_popups(array('.'.$pclass=>array('targetSelectors'=>array('#'.$id=>'#'.$id)))); - } - else { // Creating link to autofill box. - $pclass = 'popups'; - popups_add_popups(); - } - $options = array( - 'attributes' => array( - 'class' => $pclass, - ), - 'query' => array('destination' => "node/add/$src_type"), - ); - $links = array(); - $all_types = node_get_types(); - foreach ($field['referenceable_types'] as $type => $value) { - if (!empty($value) && user_access("create $type content")) { - $path = "node/add/$type"; - $links[] = l("Add $type", $path, $options); - } - } - return $links; -} /** * Implementation of hook_form_alter(). + * + * Modifies the nodereference setting form and the basic node form. */ function popups_reference_form_alter(&$form, $form_state, $form_id) { - if (isset($form['type'])) { + if ($form_id == 'content_field_edit_form' && $form['#field']['type'] == 'nodereference') { + // Add a checkbox to the nodereference settings page. + $field_name = $form['#field']['field_name']; + $form['field']['show_add_link'] = array( + '#type' => 'checkbox', + '#default_value' => variable_get('popups_reference_show_add_link_'. $field_name, TRUE), + '#title' => t('Show the "Add New: Node Type" Popup links'), + '#description' => t("Activate Popups:Add and Reference behavior for this reference.") + ); + $form['#submit'][] = '_popups_reference_manage_fields_submit'; + } + elseif (isset($form['type'])) { + // Add the "Add New: Node Type" links. $node = $form['#node']; if ($form['type']['#value'] .'_node_form' == $form_id) { $fields = content_fields(); foreach ($form as $key => $item) { if (is_array($item)) { - // firep($item['#type'], $key); $type = $item['#type']; - if ($type == 'fieldset') { // loop through all the subitems. + if ($type == 'fieldset') { // Loop through all the subitems. foreach ($form[$key] as $subkey => $subitem) { popups_reference_alter_item($form[$key], $subkey, $subitem, $fields); } @@ -69,26 +47,107 @@ function popups_reference_form_alter(&$form, $form_state, $form_id) { } } +/** + * Implementation of hook_nodeapi(). + * Add cookies with node info when a new node is created. + * These cookies will be found by the popups_reference behavior and used + * to select the newly created node in the reference widget. + */ +function popups_reference_nodeapi($node, $op) { + if ($op == 'insert') { + $five = time()+300; // 5 minutes in the future. + setcookie("PopupRefNid", $node->nid, $five, '/'); + setrawcookie("PopupRefTitle", rawurlencode($node->title), $five, '/'); + } +} + +/** + * Submit added to the the nodereference settings form. + * Set a variable for each nodereference field. + */ +function _popups_reference_manage_fields_submit($form, &$form_state) { + $field_name = $form['#field']['field_name']; + variable_set('popups_reference_show_add_link_'. $field_name, $form_state['values']['show_add_link']); +} + +/** + * Run on every element in the basic node form. + * Wrap the enabled nodereference fields, and add the popup links. + * + * @param $form - the form (or fieldgroup). + * @param $key - form element name. + * @param $item - the form element array. + * @param $fields - all fields info. + */ function popups_reference_alter_item(&$form, $key, $item, $fields) { - $field_name = strstr($key, 'field_'); - if (isset($fields[$field_name]) && $fields[$field_name]['type'] == 'nodereference') { + $field_name = strstr($key, 'field_'); // Check if $key starts with 'field_'; + if (isset($fields[$field_name]) && + $fields[$field_name]['type'] == 'nodereference' && + variable_get('popups_reference_show_add_link_'. $field_name, TRUE)) { $type = $form['type']['#value']; - $field = content_fields($field_name, $form['type']['#value']); + $field = content_fields($field_name, $type); - if ($field['widget']['type'] == 'nodereference_select') { - $id = 'popups-reference-' . _popups_reference_counter(); - $links = _popups_reference_links($field, $type, $id); - // Need wrapped with id for ahah replacement. - $form[$key]['#prefix'] = '
'; + $wrapper_id = 'popups-reference-' . _popups_reference_counter(); + $links = _popups_reference_links($field, $type, $wrapper_id, $field['widget']['type']); + if ($links) { + // Put the nodereference widget and links in an wrapper. + // Makes it easy to find for Ahah targeting, and popups_reference behavior selecting. + $form[$key]['#prefix'] = '
'; $form[$key]['#suffix'] = '
Add New: ' . implode(', ', $links) .'
'; } + } +} - else if ($field['widget']['type'] == 'nodereference_autocomplete') { - $links = _popups_reference_links($field, $type, $id); - $form[$key]['#suffix'] = '
Add New: ' . implode(', ', $links) .'
'; +/** + * Generates 'Add new...' link + * for each allowed content type + * + * @param $field + * @param $src_type - the type of base node. + * @param $wrapper_id - id for the wrapper around the node reference. + * @param $type - the type of widget. + * @return Array of html links. + */ +function _popups_reference_links($field, $src_type, $wrapper_id, $widget_type) { + if ($widget_type == 'nodereference_select' || $widget_type == 'nodereference_buttons') { + // Target the wrapper for replacing. +// dsm('$wrapper_id: ' . $wrapper_id); + popups_add_popups(array('a.'. $wrapper_id => array('targetSelectors' => array('div.'. $wrapper_id)))); + } + else if ($widget_type == 'nodereference_autocomplete') { + // Don't replace the autocomplete when done. + popups_add_popups(array('a.'. $wrapper_id => array('noUpdate' => TRUE))); + } + else { // Unsupported type. + return; + } + $options = array( + 'attributes' => array( + 'class' => $wrapper_id . ' popups-reference', + 'rel' => $wrapper_id, + ), + 'query' => array('destination' => 'node/add/' . str_replace('_', '-', $src_type)), + ); + $links = array(); + $all_types = node_get_types(); + foreach ($field['referenceable_types'] as $add_type => $value) { + if (!empty($value) && (user_access("create $add_type content") || user_access('administer nodes'))) { + drupal_add_js(drupal_get_path('module', 'popups_reference') .'/popups_reference.js'); + $path = 'node/add/' . str_replace('_', '-', $add_type); + $name = $all_types[$add_type]->name; + $links[] = l("Add $name", $path, $options); } - } + return $links; +} + +/** + * A counter for generating unique element id's. + * + * @return int: next integer. + */ +function _popups_reference_counter() { + static $count = 0; + return $count++; } -