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