--- README.txt 2006-07-27 10:06:20.000000000 -0400
+++ README.txt 2007-12-13 15:23:41.000000000 -0500
@@ -1,15 +1,9 @@
// $Id: README.txt,v 1.1 2006/07/27 14:06:20 tema Exp $
-Note1: This module requires content.module, nodereference.module
- and form_restore.module
+Note1: This module requires content.module, nodereference.module
Note2: Please read CCK documentation (http://drupal.org/node/62459)
if You are not familiar with CCK and nodereference fields.
-
-Note3: This is my first project so please feel free to point me
- to my mistakes, I'll fix it as soon as possible.
- And sorry for my bad English, I'm not sure that You'r Russian
- is better ;-P
This module adds the ability to reference non-existing (new) nodes
right from nodereference field when You add or edit the node.
--- add_n_reference.module 2006-07-31 07:05:41.000000000 -0400
+++ add_n_reference.module 2007-12-19 02:56:04.000000000 -0500
@@ -1,31 +1,51 @@
Note: Requires content.module nodereference.module and form_restore.module');
+function add_n_reference_form_alter($form_id, &$form) {
+ if (isset($form['type'])) {
+ $node = $form['#node'];
+ if ($form['type']['#value'] .'_node_form' == $form_id) {
+ $type_name = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type);
+ $node_type = content_types($type_name);
+ if (count($node_type['fields'])) {
+ foreach ($node_type['fields'] as $k => $v) {
+ if ($v['type'] == 'nodereference') {
+ if ($_POST['op'] == t('Submit') && isset($_SESSION['add_n_ref'][$_REQUEST['q']]['add_n_ref_restored'])) {
+ /*
+ if (isset($_SESSION['add_n_ref'][$_REQUEST['q']]['destination'])) {
+ $_REQUEST['destination'] = $_SESSION['add_n_ref'][$_REQUEST['q']]['destination'];
+ }
+ */
+ unset($_SESSION['add_n_ref'][$_REQUEST['q']]);
+ }
+ else {
+ array_push($form['#after_build'], 'add_n_reference_after_build');
+ }
+ break;
+ }
+ }
+ }
+ }
}
}
/**
- * Implementation of hook_form_alter().
- */
-function add_n_reference_form_alter($form_id, &$form) {
- if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
- theme_add_style(drupal_get_path('module', 'add_n_reference') .'/add-n-reference.css');
+ * Only node forms have nodereference fields will call this function.
+ *
+ * @param array $form
+ * @return $form
+ */
+function add_n_reference_after_build($form, $form_values) {
+ if (!isset($_REQUEST['destination'])) {
+ drupal_add_css(drupal_get_path('module', 'add_n_reference') .'/add-n-reference.css');
$fields = content_fields();
foreach ($form as $key => $item) {
$field_name = strstr($key, 'field_');
@@ -33,9 +53,9 @@
$field = content_fields($field_name, $form['type']['#value']);
$form[$key]['#prefix'] = '
';
$form[$key]['#suffix'] = "
\n";
-
+
if ($field['widget']['type'] == 'nodereference_select') {
- $buttons = _add_buttons($form['type']['#value'], $field_name, count($item['nids']['#default_value']));
+ $buttons = _add_n_reference_buttons($field, count($item['nids']['#value']));
if (!empty($buttons)) {
$form[$key]['add_n_reference_buttons'] = $buttons + array(
'#prefix' => '',
@@ -43,9 +63,9 @@
);
}
}
- else if ($field['widget']['type'] == 'nodereference_autocomplete') {
+ elseif ($field['widget']['type'] == 'nodereference_autocomplete') {
foreach ($item as $delta => $field_item) {
- $buttons = _add_buttons($form['type']['#value'], $field_name, $delta);
+ $buttons = _add_n_reference_buttons($field, $delta);
if (is_numeric($delta) && !empty($buttons)) {
$form[$key][$delta]['add_n_reference_buttons'] = $buttons + array(
'#prefix' => '
$value) {
+ if (!empty($value) && user_access("create $node_type content")) {
+ $buttons["add_$node_type"] = array(
'#type' => 'button',
- '#name' => "op[add_n_reference][$parent_type][$field_name][$delta][$child_type]",
- '#value' => t('Add new') .' '. $all_types[$type],
+ '#name' => 'op[add_n_reference]['. $field['field_name'] .']['
+ . $field['widget']['type'] .']['. $delta .']['. $node_type .']',
+ '#value' => t('Add new') .' '. $all_types[$node_type]->name,
);
}
}
@@ -83,18 +112,50 @@
}
/**
+ * Enter description here...
+ *
+ * @param unknown_type $form_id
+ * @param unknown_type $form_values
+ */
+function add_n_reference_validate($form_id, $form_values) {
+ if (extract(_disgress_params($_POST['op'])) == 6) {
+ if (is_array($_SESSION['messages']) && array_key_exists('error', $_SESSION['messages'])) {
+ // TODO: Should I check the error? How?
+ unset($_SESSION['messages']['error']);
+ }
+ if (isset($_SESSION['add_n_ref'])) {
+ // Stack limit for different content types
+ while (count($_SESSION['add_n_ref']) > 9) {
+ array_shift($_SESSION['add_n_ref']);
+ }
+ }
+ $_SESSION['add_n_ref'][$_REQUEST['q']] = $_POST;
+ unset($_SESSION['add_n_ref'][$_REQUEST['q']]['form_token']);
+ /*
+ if (isset($_REQUEST['destination'])) {
+ $_SESSION['add_n_ref'][$_REQUEST['q']]['destination'] = $_REQUEST['destination'];
+ unset($_REQUEST['destination']);
+ }
+ */
+ drupal_goto($path, 'destination='. $_REQUEST['q']);
+ }
+}
+
+/**
* Determines witch button is pressed
*/
function _disgress_params($op) {
- if (is_array($op) && isset($op['add_n_reference'])) {
+ if (is_array($op) && is_array($op['add_n_reference'])) {
$op = $op['add_n_reference'];
- $parent_type = key($op);
- $field_name = key($op[$parent_type]);
- $delta = key($op[$parent_type][$field_name]);
- $child_type = key($op[$parent_type][$field_name][$delta]);
- $path = "node/add/$child_type";
-
- return compact('parent_type', 'field_name', 'delta', 'child_type', 'path');
+ $field_name = key($op);
+ $widget_type = key($op[$field_name]);
+ $delta = key($op[$field_name][$widget_type]);
+ $node_type = key($op[$field_name][$widget_type][$delta]);
+ $path = "node/add/$node_type";
+ // multiple or not
+ $fields = content_fields();
+ $multiple = $fields[$field_name]['multiple'];
+ return compact('field_name', 'widget_type', 'delta', 'node_type', 'path', 'multiple');
}
else {
return array();
@@ -102,44 +163,48 @@
}
/**
- * Pre-render function
+ * Implementation of hook_nodeapi().
+ *
+ * Save the newly created node's nid or name
*/
-function _add_n_reference_pre_render($form_id, &$form) {
- if (extract(_disgress_params($_POST['op'])) == 5) {
- if (isset($_SESSION['post_stack'])) {
- while (count($_SESSION['post_stack']) > 10) { // Stack limit
- array_shift($_SESSION['post_stack']);
+function add_n_reference_nodeapi(&$node, $op) {
+ if ($op == 'insert' && isset($_REQUEST['destination'])
+ && isset($_SESSION['add_n_ref']) && isset($_SESSION['add_n_ref'][$_REQUEST['destination']])
+ && extract(_disgress_params($_SESSION['add_n_ref'][$_REQUEST['destination']]['op'])) == 6) {
+ if ($widget_type == 'nodereference_select') {
+ if ($multiple == 1) {
+ $_SESSION['add_n_ref'][$_REQUEST['destination']][$field_name]['nids'][] = $node->nid;
+ }
+ else {
+ $_SESSION['add_n_ref'][$_REQUEST['destination']][$field_name]['nids'] = $node->nid;
}
}
- $_SESSION['post_stack'][$_REQUEST['q']] = $_POST;
- $_POST = ''; // Prevent form values validation that happened in some cases
-
- if (isset($_REQUEST['destination'])) {
- $_SESSION['post_stack'][$_REQUEST['q']]['destination'] = $_REQUEST['destination'];
- unset($_REQUEST['destination']); // Prevent destination inheritance
+ elseif ($widget_type == 'nodereference_autocomplete') {
+ $_SESSION['add_n_ref'][$_REQUEST['destination']][$field_name][$delta]['node_name'] = $node->title .' [nid:'. $node->nid .']';
}
- drupal_goto($path, 'destination='. $_REQUEST['q']);
}
}
/**
- * Implementation of hook_nodeapi().
- */
-function add_n_reference_nodeapi(&$node, $op) {
- if ($op == 'insert' && isset($_REQUEST['destination'])
- && isset($_SESSION['post_stack']) && isset($_SESSION['post_stack'][$_REQUEST['destination']])
- && extract(_disgress_params($_SESSION['post_stack'][$_REQUEST['destination']]['op'])) == 5) {
- $field = content_fields($field_name, $parent_type);
- if ($field['widget']['type'] == 'nodereference_select') {
- if ($field['multiple']) {
- $_SESSION['post_stack'][$_REQUEST['destination']]['edit'][$field_name]['nids'][] = $node->nid;
+ * Enter description here...
+ *
+ * @param unknown_type $form_item
+ * @param unknown_type $stored_item
+ */
+function _add_n_reference_restore_values(&$form_item, $stored_item) {
+ foreach ($form_item as $key => $item) {
+ if (is_array($item) && is_array($stored_item)) {
+ if (array_key_exists($key, $stored_item)) {
+ if (array_key_exists('#default_value', $item)) {
+ $form_item[$key]['#value'] = $stored_item[$key];
+ }
+ else {
+ _add_n_reference_restore_values(&$form_item[$key], $stored_item[$key]);
+ }
}
else {
- $_SESSION['post_stack'][$_REQUEST['destination']]['edit'][$field_name]['nids'] = $node->nid;
+ _add_n_reference_restore_values(&$form_item[$key], $stored_item);
}
}
- else if ($field['widget']['type'] == 'nodereference_autocomplete') {
- $_SESSION['post_stack'][$_REQUEST['destination']]['edit'][$field_name][$delta]['node_name'] = $node->title;
- }
}
}
\ No newline at end of file