'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)) { $type = $item['#type']; if ($type == 'fieldset') { // Loop through all the subitems. foreach ($form[$key] as $subkey => $subitem) { popups_reference_alter_item($form[$key], $subkey, $subitem, $fields); } } else { popups_reference_alter_item($form, $key, $item, $fields); } } } } } } /** * 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_'); // 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, $type); $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 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++; }