| 1 |
<?php
|
| 2 |
// $Id: add_n_reference.module,v 1.1 2006/07/27 14:06:20 tema Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Adds a buttons to create and reference new node with nodereference field
|
| 7 |
* Note: Requires content.module, nodereference.module
|
| 8 |
* and form_restore.module to fill in parent field and for ability of
|
| 9 |
* multi-step chained nodes creation.
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_help().
|
| 14 |
*/
|
| 15 |
function add_n_reference_help($section) {
|
| 16 |
switch ($section) {
|
| 17 |
case 'admin/modules#description':
|
| 18 |
return t('Creates and references new nodes with nodereference field.
|
| 19 |
<em>Note: Requires content.module nodereference.module and form_restore.module</em>');
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implementation of hook_form_alter().
|
| 25 |
*/
|
| 26 |
function add_n_reference_form_alter($form_id, &$form) {
|
| 27 |
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
|
| 28 |
theme_add_style(drupal_get_path('module', 'add_n_reference') .'/add-n-reference.css');
|
| 29 |
$fields = content_fields();
|
| 30 |
foreach ($form as $key => $item) {
|
| 31 |
$field_name = strstr($key, 'field_');
|
| 32 |
if (isset($fields[$field_name]) && $fields[$field_name]['type'] == 'nodereference') {
|
| 33 |
$field = content_fields($field_name, $form['type']['#value']);
|
| 34 |
$form[$key]['#prefix'] = '<div class="add-n-reference">';
|
| 35 |
$form[$key]['#suffix'] = "</div>\n";
|
| 36 |
|
| 37 |
if ($field['widget']['type'] == 'nodereference_select') {
|
| 38 |
$buttons = _add_buttons($form['type']['#value'], $field_name, count($item['nids']['#default_value']));
|
| 39 |
if (!empty($buttons)) {
|
| 40 |
$form[$key]['add_n_reference_buttons'] = $buttons + array(
|
| 41 |
'#prefix' => '<div class="form-item field-select add-n-reference-buttons">',
|
| 42 |
'#suffix' => "</div>\n",
|
| 43 |
);
|
| 44 |
}
|
| 45 |
}
|
| 46 |
else if ($field['widget']['type'] == 'nodereference_autocomplete') {
|
| 47 |
foreach ($item as $delta => $field_item) {
|
| 48 |
$buttons = _add_buttons($form['type']['#value'], $field_name, $delta);
|
| 49 |
if (is_numeric($delta) && !empty($buttons)) {
|
| 50 |
$form[$key][$delta]['add_n_reference_buttons'] = $buttons + array(
|
| 51 |
'#prefix' => '<div class="form-item field-'
|
| 52 |
. ($field['multiple'] ? 'multiple' : 'single')
|
| 53 |
.' add-n-reference-buttons">',
|
| 54 |
'#suffix' => "</div>\n",
|
| 55 |
);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
}
|
| 59 |
$form['#pre_render'] = array('_add_n_reference_pre_render');
|
| 60 |
}
|
| 61 |
}
|
| 62 |
}
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Generates 'Add new...' button
|
| 67 |
* for each allowed content type
|
| 68 |
*/
|
| 69 |
function _add_buttons($parent_type, $field_name, $delta) {
|
| 70 |
$buttons = array();
|
| 71 |
$all_types = node_get_types();
|
| 72 |
$field = content_fields($field_name, $parent_type);
|
| 73 |
foreach ($field['referenceable_types'] as $child_type => $value) {
|
| 74 |
if (!empty($value) && user_access("create $child_type content")) {
|
| 75 |
$buttons["add_$child_type"] = array(
|
| 76 |
'#type' => 'button',
|
| 77 |
'#name' => "op[add_n_reference][$parent_type][$field_name][$delta][$child_type]",
|
| 78 |
'#value' => t('Add new') .' '. $all_types[$type],
|
| 79 |
);
|
| 80 |
}
|
| 81 |
}
|
| 82 |
return $buttons;
|
| 83 |
}
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Determines witch button is pressed
|
| 87 |
*/
|
| 88 |
function _disgress_params($op) {
|
| 89 |
if (is_array($op) && isset($op['add_n_reference'])) {
|
| 90 |
$op = $op['add_n_reference'];
|
| 91 |
$parent_type = key($op);
|
| 92 |
$field_name = key($op[$parent_type]);
|
| 93 |
$delta = key($op[$parent_type][$field_name]);
|
| 94 |
$child_type = key($op[$parent_type][$field_name][$delta]);
|
| 95 |
$path = "node/add/$child_type";
|
| 96 |
|
| 97 |
return compact('parent_type', 'field_name', 'delta', 'child_type', 'path');
|
| 98 |
}
|
| 99 |
else {
|
| 100 |
return array();
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
/**
|
| 105 |
* Pre-render function
|
| 106 |
*/
|
| 107 |
function _add_n_reference_pre_render($form_id, &$form) {
|
| 108 |
if (extract(_disgress_params($_POST['op'])) == 5) {
|
| 109 |
if (isset($_SESSION['post_stack'])) {
|
| 110 |
while (count($_SESSION['post_stack']) > 10) { // Stack limit
|
| 111 |
array_shift($_SESSION['post_stack']);
|
| 112 |
}
|
| 113 |
}
|
| 114 |
$_SESSION['post_stack'][$_REQUEST['q']] = $_POST;
|
| 115 |
$_POST = ''; // Prevent form values validation that happened in some cases
|
| 116 |
|
| 117 |
if (isset($_REQUEST['destination'])) {
|
| 118 |
$_SESSION['post_stack'][$_REQUEST['q']]['destination'] = $_REQUEST['destination'];
|
| 119 |
unset($_REQUEST['destination']); // Prevent destination inheritance
|
| 120 |
}
|
| 121 |
drupal_goto($path, 'destination='. $_REQUEST['q']);
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Implementation of hook_nodeapi().
|
| 127 |
*/
|
| 128 |
function add_n_reference_nodeapi(&$node, $op) {
|
| 129 |
if ($op == 'insert' && isset($_REQUEST['destination'])
|
| 130 |
&& isset($_SESSION['post_stack']) && isset($_SESSION['post_stack'][$_REQUEST['destination']])
|
| 131 |
&& extract(_disgress_params($_SESSION['post_stack'][$_REQUEST['destination']]['op'])) == 5) {
|
| 132 |
$field = content_fields($field_name, $parent_type);
|
| 133 |
if ($field['widget']['type'] == 'nodereference_select') {
|
| 134 |
if ($field['multiple']) {
|
| 135 |
$_SESSION['post_stack'][$_REQUEST['destination']]['edit'][$field_name]['nids'][] = $node->nid;
|
| 136 |
}
|
| 137 |
else {
|
| 138 |
$_SESSION['post_stack'][$_REQUEST['destination']]['edit'][$field_name]['nids'] = $node->nid;
|
| 139 |
}
|
| 140 |
}
|
| 141 |
else if ($field['widget']['type'] == 'nodereference_autocomplete') {
|
| 142 |
$_SESSION['post_stack'][$_REQUEST['destination']]['edit'][$field_name][$delta]['node_name'] = $node->title;
|
| 143 |
}
|
| 144 |
}
|
| 145 |
}
|