I'm currently developing a custom module for my company and have run into quite an issue.

I created a function to render a form, however, the checkbox options need to be dynamic based on the term_data table....so I tried this:

function render_video_options(){
	$output = '';
	$query = mysql_query("SELECT * FROM term_data WHERE vid = '9'");	
	while ($row = mysql_fetch_array($query)){
		$output .= "'".$row['name']."' => t('".$row['name']."'),";
	}
	return $output;
}

The point of this function is to create an output variable that will loop through every entry in the term_data and put it into a
'name' => t('name'), format to fit the drupal form api standard.

Then I implement this, hoping to put in the $output variable in place of the options array to populate the array:

$form['videosowned'] = array(
	'#type' => 'checkbox',
	'#title' => t('Videos Owned'),
	'#options' => array(
    	render_video_options()
    ),
);

However, it only prints a checkbox and then the literal string of " 'name' => t('name'), " as a STRING, not as variable input for the array....

Also, although it was doing that before, I tried it again and now it's just printing out a checkbox and then the title of the form (I.E. Videos Owned).

Can someone help me out here?

Unfortunately, I can't reference to a link as the project is on a private vpn of the company.

Comments

nevets’s picture

#optiions should be an array of value label pairs, so your first function should look something like

function render_video_options(){
$output =array();
$query = mysql_query("SELECT * FROM term_data WHERE vid = '9'");
while ($row = mysql_fetch_array($query)){
$output[$row['tid']] = $row['name'];
}
return $output;
}

which will produce an array indexed by tid with the values being the corresponding name.

Then your next piece of code would look like

$form['videosowned'] = array(
'#type' => 'checkbox',
'#title' => t('Videos Owned'),
'#options' => render_video_options()
);

Note your validate and submit functions will see the choice as a term id (tid). Generally it is better for the code to work with tid's (while showing the user names) since the name can change (while the tid stays the same).

Also you may wish to implement the taxonomy hook so if someone deletes a term you can clean up any stored data.

CWolff’s picture

Thanks nevets!

Worked like a charm, however, one typo in your code:

$form['videosowned'] = array(
'#type' => 'checkboxes',
'#title' => t('Videos Owned'),
'#options' => render_video_options()
);

checkbox -> checkboxes

:)

Thanks again.
------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com

nevets’s picture

And I copied from your orignal post :)

CWolff’s picture

Ha!

Well how about that, I guess it really is hard to find any typos until you get a fresh pair of eyes.

Thanks again. :)

------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com

CWolff’s picture

Any clue as to why this wouldn't return the variable?

function render_usercalendar(){
	$output = "";
	drupal_add_js(drupal_get_path('module', 'usercalendar') .'/calendar.js');
	drupal_add_css(drupal_get_path('module', 'usercalendar') .'/calendar_style.css');
	$output = "<div id=\"calback\"><div id=\"calendar\"></div></div>";
	return $output;
}
    '#required' => TRUE,
    '#description' => t("Create a unique name for your new calendar")
    );
    render_usercalendar();
    $form['options'] = array(
    '#type' => 'checkboxes', 
    '#title' => t('Days'), 

I'm just trying to get it to output the divs in the $output variable, but for some odd reason, it won't. Forgive me if this is relatively simple, I've been coding all night and everything's starting to blur together.

Also, would anyone perchance know if there's a hook to add a javascript body onload? I.E.

<body onload = 'blah()'>

------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com

nevets’s picture

Your code (second snippet) only calls render_usercalendar(), it never does anything with the return value. I am guessing you want something like

$form['user_calendar'] = array(
  '#type' => 'markup',
  '#value' => render_usercalendar(),
);

As for running javascript the preferred way is to use the jQuery ready event, something like this

$js = '$(document).ready(blah);'
drupal_add_js('inline', $js);
CWolff’s picture

As for the output, duh. Again, I apologize, it's been a long night.

------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com

CWolff’s picture

as for the js: would it look something like:

$js = '$(document).ready( function here );';
drupal_add_js('inline', $js);

or am I completely off-base, and jQuery, do you mean the built in js or the module?

------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com

nevets’s picture

And by jQuery I mean the library (it gets included as a side effect of calling drupal_add_js)

CWolff’s picture

 else if ($op == 'view') {
		//calendar info for block goes here
		drupal_add_js(drupal_get_path('module', 'usercalendar') .'/calendar.js');
		drupal_add_css(drupal_get_path('module', 'usercalendar') .'/calendar_style.css');
		$js = '$(document).ready(navigate("","");';
		drupal_add_js('inline', $js);
		$content = "
		<div id=\"calback\">
		<div id=\"calendar\"></div>
		</div>

from the calendar.js file:

function navigate(month,year) {
        var url = "calendar.php?month="+month+"&year="+year;
        if(window.XMLHttpRequest) {
                req = new XMLHttpRequest();
        } else if(window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.open("GET", url, true);
        req.onreadystatechange = callback;
        req.send(null);
}

and it's not doing anything except outputting a blank div, I know the function called should be navigate("","") ...it works fine on an html with that as the body onload

------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com

nevets’s picture

Given the function as arguments you need to set up the js like

 $js =<<<JS
$(document).ready(
  function() {
    navigate("", "");
  }
);
JS;

(It does not really need to be multiline, I just find it easier to read and edit that way).

And in your calendar.js the url is probably wrong as it will be relative to the path used to construct the page. So for example iif the path you are using was www.yoursight.com/calendar, the url would translate to '/calendar/calander.php' which is probably not where the script is.
(The net tab of firebug is useful for tracking this kind of problem)

And as a side note your navigate function using jQuery could be coded something like (does not fix path issue though)

function navigate(month,year) {
 $.ajax({
   url: "calendar.php?month="+month+"&year="+year,
   success: callback
 });
}

(You will probably want to set dataType to reflect the information returned by calander.php, you can also set 'error' to a callback to handle errors)

CWolff’s picture

Nothing but a "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/kenaiw/drupal/modules/usercalendar/usercalendar.module on line 137" error...

line 137: $block['content'] = $content;

$js =<<<JS
		$(document).ready(
		function() {
		navigate("", "");
		}
		);
		JS;
var req;

function navigate(month,year) {
        var url = "modules/usercalendar/calendar.php?month="+month+"&year="+year;
        if(window.XMLHttpRequest) {
                req = new XMLHttpRequest();
        } else if(window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.open("GET", url, true);
        req.onreadystatechange = callback;
        req.send(null);
}

*headache*

------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com

nevets’s picture

This part

$js =<<<JS
$(document).ready(
function() {
navigate("", "");
}
);
JS;

is PHP and replaces the orignal setting of $js (so that arguments to navigate are handled).

CWolff’s picture

My bad, it was in the php script, but I copied and pasted AFTER I moved it, so it originally was in the .module.

------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com

nevets’s picture

If you are still getting the error what do the lines right before the line with error look like?

CWolff’s picture

It seems to switch off between the parse error and nothing (i.e. no errors whatsoever, but also, no output).

else if ($op == 'view') {
//calendar info for block goes here
drupal_add_js(drupal_get_path('module', 'usercalendar') .'/calendar.js');
drupal_add_css(drupal_get_path('module', 'usercalendar') .'/calendar_style.css');
$js =<<<JS
$(document).ready(
function() {
navigate("", "");
}
);
JS;
drupal_add_js('inline', $js);
$content = "
<div id=\"calback\">
<div id=\"calendar\"></div>
</div>

------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com

nevets’s picture

This part

$content = "
<div id=\"calback\">
<div id=\"calendar\"></div>
</div>

has no closing quote. If that is not the problem send me the source file and I will look at it the morning.

CWolff’s picture

Hey Nevets,

Sorry I didn't get back to you yesterday, full with meetings. I got it fixed though thanks to your direction. This is what it looks like when working:

// Display Calendar

function render_calendar_create(){
		drupal_add_js(drupal_get_path('module', 'usercalendar') .'/calendar.js');
		drupal_add_css(drupal_get_path('module', 'usercalendar') .'/calendar_style.css');
		$js =<<<JS
		$(document).ready(
		function() {
		navigate("", "");
		}
		);
JS;
		drupal_add_js('inline', $js);
		$output = "<script>navigate(\"\",\"\");</script>
		<div id=\"calback\">
		<div id=\"calendar\"></div>
		</div>";
		return $output;
}

Now I just need to figure out how to pass a variable from the module file to the external .php file. Hopefully it'll be easier than what I'm anticipating. Thanks! :)
------------------------
Cassandra Wolff
PHP Developer
Gaiam Inc.
Direct: 303-222-3676
cassandra.wolff@gaiam.com