as i mentioned at http://drupal.org/node/67893#comment-232650 -- there's something broken with FAPI when you try to define #default_values for a multi-select form element. you get the following notice on php4:

notice: Array to string conversion in /Users/wright/drupal/cvs/6/core/includes/form.inc on line 957.

(doesn't show up on php5 for whatever reason).

anyway, attached patch (suggested by chx) solves the problem, and makes sense to me.

CommentFileSizeAuthor
fapi_default_values_multiselect.patch.txt750 bytesdww

Comments

dmitrig01’s picture

Status: Needs review » Reviewed & tested by the community

what can I say?

dmitrig01’s picture

I can say AWESOME!!
lol

dww’s picture

@dmitrig01 thanks for the "review". ;) however, for future reference, here are some examples of (useful) things you could say when marking an issue RTBC:

  1. "i looked at the patch, and..."
    • "it makes sense"
    • "it follows the coding guidelines"
    • "it doesn't introduce any security holes"
    • "it doesn't introduce any performance problems"
    • "it doesn't need any additional comments to clarify what its doing"
    • ...

    (etc, etc)

  2. "i am able to reproduce the original bug before applying the patch"
  3. "after applying the patch, the bug goes away"

... this adds legitimacy to your claim, and saves time for the core committers since they don't necessarily have to confirm all those things themselves (if you do a good enough job on enough patch reviews that they can start to trust you, at least). ;) you don't always need to list all of these, but including at least some evidence to support your claim that the patch is RTBC is definitely a good idea.

thanks,
-derek

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Don't think this needs to go in DRUPAL-5 so I'm marking this fixed.

dww’s picture

Version: 6.x-dev » 5.x-dev
Priority: Normal » Minor
Status: Fixed » Reviewed & tested by the community

well, the bug is still present in 5.x, the patch still applies (with minor offset) and still fixes the bug (i just confirmed all of that, and tested). true, it's only visible on D6 because of E_NOTICE being reported, but the underlying code is still doing the wrong thing -- we really shouldn't be trying to cast arrays to strings like that... it's a logic bug in the if() clause that php is being forgiving about, and we happen to still get the right behavior. but, IMHO, it's a (minor) FAPI bug that should be backported. i've seen drumm say in other issues, basically "D5 is no excuse for bad code" in response to similar E_ALL-related bug fixes, so i'm guessing he'd be willing to commit this.

thanks,
-derek

p.s. the bug is there in 4.7 FAPI, too, but the patch doesn't apply. i'd backport if killes wanted, but i'm not sure he's gonna care. ;)

drumm’s picture

Status: Reviewed & tested by the community » Fixed

Yes, individual verifiable fixes do belong in 5.

Committed to 5.

dww’s picture

Version: 5.x-dev » 4.7.x-dev
Status: Fixed » Patch (to be ported)

thanks, drumm.

so, what do you say, killes? want a 4.7.x version? if not, just bump this back to 5.x or 6.x and "fixed".

dww’s picture

Version: 4.7.x-dev » 6.x-dev
Priority: Minor » Normal
Status: Patch (to be ported) » Fixed

from killes in IRC: "dww: I think your time is better spent elsewhere."... ;)

dries’s picture

Status: Fixed » Closed (fixed)
radj’s picture

i was creating an Annotation module reading Chapter 2 of the April 2007 edition of Apress' Pro Drupal Development. Reading and setting the #default_value of the textarea of in the nodeapi hook (find ### in the code below) did not show anything.

when i used a textfield type, the value was shown. but i needed a textarea. after searching, i found this patch. i reversed the patch and reverted my textfield back to textarea and it worked.

am i doing something wrong?

<?php

// $Id$
/**
* @file
* Lets users add private annotations to nodes.
*
* Adds a text field when a node is displayed
* so that authenticated users may make notes.
*/

/**
* Implementation of hook_menu().
*/
function annotate_menu() {
	$items = array();
	if (1) {
		$items[] = array(
			'path' => 'admin/settings/annotate',
			'title' => t('Annotation settings'),
			'description' => t('Change how annotations behave.'),
			'callback' => 'drupal_get_form',
			'callback arguments' => array('annotate_admin_settings'),
			'access' => user_access('administer site configuration')
		);
	}
	return $items;
}

/**
* Define the settings form.
*/
function annotate_admin_settings() {
	$form['annotate_nodetypes'] = array(
		'#type' => 'checkboxes',
		'#title' => t('Users may annotate these node types'),
		'#options' => node_get_types('names'),
		'#default_value' => variable_get('annotate_nodetypes', array('story')),
		'#description' => t('A text field will be available on these node types to make
		user-specific notes.'),
	);
	$form['array_filter'] = array('#type' => 'hidden');
	return system_settings_form($form);
}

/**
* Implementation of hook_nodeapi().
*/
function annotate_nodeapi(&$node, $op, $teaser, $page) {
	global $db_prefix;
	switch ($op) {
		case 'load':
		case 'view':
			global $user;
			// If only the node summary is being displayed, or if the
			// user is an anonymous user (not logged in), abort.
			if ($teaser || $user->uid == 0) {
				break;
			}
			$types_to_annotate = variable_get('annotate_nodetypes', array('story'));
			if (!in_array($node->type, $types_to_annotate)) {
				break;
			}
			// Add our form as a content item.
			$node->content['annotation_form'] = array(
				'#value' => drupal_get_form('annotate_entry_form', $node),
				'#weight' => 10
			);
			// Get previously saved note, if any.
			$result = db_query("SELECT note FROM {$db_prefix}annotations WHERE uid = %d AND nid = %d", $user->uid, $node->nid);
			$node->annotation = db_result($result);
	}
	#echo "<!-- nodeapi $op -->".$node->annotation;
}

/**
* Define the form for entering an annotation.
*/
function annotate_entry_form($node) {
	$form['annotate'] = array(
		'#type' => 'fieldset',
		'#title' => t('Annotations')
	);
	$form['annotate']['nid'] = array(
		'#type' => 'value',
		'#value' => $node->nid
	);
	$form['annotate']['note'] = array(
		'#type' => 'textarea',
		'#rows' => 5,
		'#title' => t('Notes'),
		'#default_value' => $node->annotation, ### <--- not working
		'#description' => t('Make your personal annotations about this content
		here. Only you (and the site administrator) will be able to see them.')
	);
	$form['annotate']['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Update')
	);
	#echo "<!-- entry_form -->".$node->annotation;
	return $form;
}

/*
* Save the annotation to the database.
*/
function annotate_entry_form_submit($form_id, $form_values) {
	global $user, $db_prefix;
	$nid = $form_values['nid'];
	$note = $form_values['note'];
	db_query("DELETE FROM {$db_prefix}annotations WHERE uid = %d and nid = %d", $user->uid, $nid);
	db_query("INSERT INTO {$db_prefix}annotations (uid, nid, note, timestamp) VALUES (%d, %d, '%s', %d)", $user->uid, $nid, $note, time());
	drupal_set_message(t('Your annotation was saved!'));
}