diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 33bd4da0ae005d6b872140bdd4ea113bea30f034..4f542639718fae7df9fc56cae6633ce104770af6 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,10 +1,14 @@ Drupal-6-1-0-RC1 New Features -* http://drupal.org/node/310019 - Checkbox to toggle the "Add New: " links. Funded by Agaric Design Collective. - + * 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.2RC1 or newer). Funded by Agaric Design + * Added support for radio buttons widget. +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. + * 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..810a4cc9c7e40458ee72d89fb20b68e58a65e77e 100644 --- a/README.txt +++ b/README.txt @@ -3,5 +3,4 @@ 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. \ No newline at end of file diff --git a/popups_reference.js b/popups_reference.js new file mode 100644 index 0000000000000000000000000000000000000000..e8260a335b329a5550bca17e1bc7aaddf6471e6c --- /dev/null +++ b/popups_reference.js @@ -0,0 +1,49 @@ +// $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() { + // Info about the new node was placed in a cookie when it was created. + var nid = popups_reference_get_cookie_value('PopupRefNid'); + var title = popups_reference_get_cookie_value('PopupRefTitle'); + $wrapper.find('select').val(nid); // Select + $wrapper.find('input.form-autocomplete').val(title); // Autocomplete + $wrapper.find(':radio[value=' + nid + ']').select(); // Radio buttons + }); + }); + }); +}; + diff --git a/popups_reference.module b/popups_reference.module index f2af6dbbea34936492c253b32be89ccbd38849ec..708cb269024c89b82c5b4a2be7e4f888019483b3 100644 --- a/popups_reference.module +++ b/popups_reference.module @@ -9,14 +9,15 @@ /** * 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 ($form_id == 'content_field_edit_form') { - // Add a checkbox to the manage nodereference page. -// dsm($form); + // Add a checkbox to the nodereference settings page. $field_name = $form['#field']['field_name']; - $form['widget']['show_add_link'] = array( - '#type' => 'checkbox', + $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.") @@ -31,7 +32,7 @@ function popups_reference_form_alter(&$form, $form_state, $form_id) { foreach ($form as $key => $item) { if (is_array($item)) { $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); } @@ -46,76 +47,100 @@ 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, '/'); + setcookie("PopupRefTitle", $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']; -// dsm($form_state); -// dsm("Submit $field_name = ". $form_state['values']['show_add_link']); 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_'); -// dsm($field_name .' = '. variable_get('popups_reference_show_add_link_'. $field_name, TRUE)); + $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']); - - if ($field['widget']['type'] == 'nodereference_select') { - $id = 'popups-reference-' . _popups_reference_counter(); - $links = _popups_reference_links($field, $type, $id); - if ($links) { - // Need wrapped with id for ahah replacement. - $form[$key]['#prefix'] = '
'; - $form[$key]['#suffix'] = '
Add New: ' . implode(', ', $links) .'
'; - } - } - else if ($field['widget']['type'] == 'nodereference_autocomplete') { - $links = _popups_reference_links($field, $type, $id); - if ($links) { - $form[$key]['#suffix'] = '
Add New: ' . implode(', ', $links) .'
'; - } + $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) .'
'; } - } } - /** * 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 the node being referenced. + * @return Array of html links. */ -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)))); +function _popups_reference_links($field, $src_type, $wrapper_id, $type) { + if ($type == 'nodereference_select' || $type == 'nodereference_buttons') { + // Target the wrapper for replacing. + popups_add_popups(array('a.'.$wrapper_id=>array('targetSelectors'=>array('#'.$wrapper_id)))); + } + else if ($type == 'nodereference_autocomplete') { + // Don't replace the autocomplete when done. + popups_add_popups(array('a.'.$wrapper_id=>array('noUpdate'=>TRUE))); } - else { // Creating link to autofill box. - $pclass = 'popups'; - popups_add_popups(); + else { // Unsupported type. + return; } $options = array( 'attributes' => array( - 'class' => $pclass, - ), + '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 $type => $value) { - if (!empty($value) && user_access("create $type content")) { - $path = 'node/add/' . str_replace('_', '-', $type); - $links[] = l("Add $type", $path, $options); + foreach ($field['referenceable_types'] as $add_type => $value) { + if (!empty($value) && user_access("create $add_type content")) { + drupal_add_js(drupal_get_path('module', 'popups_reference') .'/popups_reference.js'); + $path = 'node/add/' . str_replace('_', '-', $add_type); + $links[] = l("Add $add_type", $path, $options); } } return $links; } /** - * Generate a counter + * A counter for generating unique element id's. * * @return int: next integer. */