output:b_test has a value of false
foo({ b_live: 1, b_test: , cmd: "test" });

foo({ b_live: 1, b_test: , cmd: "test" });

a test module for this bug:

function testjs_help($section = 'admin/help#tangible') {
  switch ($section) {
    case 'admin/modules#description':
      return t('test drupal_call_js function');
  }
}
function testjs_menu($cache){
	if($cache){
		  $items[] = array('path' => 'testjs', 'title' => t('test js'),
      'callback' => 'testjs_page', 'access' => TRUE);

	}
	return $items;
}

function testjs_page(){
	
	$parray = array('b_live'=>true,'b_test'=>false,'cmd'=>'test');
	$output = drupal_call_js('foo',$parray);	
	
	$pobject = new Stdclass();
	$pobject->b_live=true;
	$pobject->b_test=false;
	$pobject->cmd='test';
	$output .= drupal_call_js('foo',$pobject);
		
	print theme('page',$output);
	return ;
	
}

CommentFileSizeAuthor
#2 js-cast.patch585 bytesDries
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dries’s picture

Does this affect any of the existing functionality, or only custom functionality or contributed modules?

What is Javascript's equivalent of PHP's FALSE? Any reference?

Dries’s picture

Status: Active » Needs review
FileSize
585 bytes

The problem is this:

print implode(',', array(true, TRUE, false, FALSE));

It prints:

1,1,,

The following seems to work though:

print implode(',', array(true, TRUE, false, FALSE, (int)false, (int)FALSE));

It prints:

1,1,,,0,0

Patched attached. Might be a more elegant way though.

sime’s picture

Status: Needs review » Reviewed & tested by the community

I replicated the problem, and then fixed it with the patch.

I also googled the problem for other solutions. Only a couple of references to the "behaviour" were found, and no better solution was found.

killes@www.drop.org’s picture

Shouldn't we add the cast for all cases?

Dries’s picture

killes: I don't think so. The problem is with implode() not dealing properly with boolean types. The other types should work fine. If in doubt, write a quick test case like I did.

Steven’s picture

Priority: Critical » Normal
Status: Reviewed & tested by the community » Fixed

Err I don't see how implode is involved. PHP just always converts false to the empty string when you cast it to string (e.g. using print, implode, ...). The proper solution is to output 'true' and 'false' instead to preserve the boolean type.

Committed a different patch to HEAD.

sime’s picture

Committed a different patch to HEAD.

Quick help please.
How do I review what you committed? The closest I've found is http://drupal.org/project/cvs/3060, but I don't see anything relevant, so am I missing something?

Steven’s picture

Was not committed due to sleepy head. Committed for real now.

Anonymous’s picture

Status: Fixed » Closed (fixed)