Warning: Table './jgarbe_jacobgarbe/watchdog' is marked as crashed and should be repaired query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:14:\"strict warning\";s:8:\"%message\";s:62:\"Non-static method view::load() should not be called statically\";s:5:\"%file\";s:61:\"/home/jgarbe/public_html/sites/all/modules/views/views.module\";s:5:\"%line\";i:906;}', 3, '', 'http://jacobgarbe.com/node/9', '', '18.216.78.185', 1740235909) in /home/jgarbe/public_html/includes/database.mysqli.inc on line 134

Warning: Table './jgarbe_jacobgarbe/watchdog' is marked as crashed and should be repaired query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:14:\"strict warning\";s:8:\"%message\";s:134:\"Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state)\";s:5:\"%file\";s:82:\"/home/jgarbe/public_html/sites/all/modules/views/handlers/views_handler_filter.inc\";s:5:\"%line\";i:607;}', 3, '', 'http://jacobgarbe.com/node/9', '', '18.216.78.185', 1740235909) in /home/jgarbe/public_html/includes/database.mysqli.inc on line 134

Warning: Table './jgarbe_jacobgarbe/watchdog' is marked as crashed and should be repaired query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:14:\"strict warning\";s:8:\"%message\";s:130:\"Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state)\";s:5:\"%file\";s:82:\"/home/jgarbe/public_html/sites/all/modules/views/handlers/views_handler_filter.inc\";s:5:\"%line\";i:607;}', 3, '', 'http://jacobgarbe.com/node/9', '', '18.216.78.185', 1740235909) in /home/jgarbe/public_html/includes/database.mysqli.inc on line 134

Warning: Table './jgarbe_jacobgarbe/watchdog' is marked as crashed and should be repaired query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:14:\"strict warning\";s:8:\"%message\";s:154:\"Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state)\";s:5:\"%file\";s:99:\"/home/jgarbe/public_html/sites/all/modules/views/handlers/views_handler_filter_boolean_operator.inc\";s:5:\"%line\";i:159;}', 3, '', 'http://jacobgarbe.com/node/9', '', '18.216.78.185', 1740235909) in /home/jgarbe/public_html/includes/database.mysqli.inc on line 134

Warning: Table './jgarbe_jacobgarbe/watchdog' is marked as crashed and should be repaired query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:14:\"strict warning\";s:8:\"%message\";s:130:\"Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state)\";s:5:\"%file\";s:77:\"/home/jgarbe/public_html/sites/all/modules/views/plugins/views_plugin_row.inc\";s:5:\"%line\";i:134;}', 3, '', 'http://jacobgarbe.com/node/9', '', '18.216.78.185', 1740235909) in /home/jgarbe/public_html/includes/database.mysqli.inc on line 134

Warning: Table './jgarbe_jacobgarbe/watchdog' is marked as crashed and should be repaired query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:14:\"strict warning\";s:8:\"%message\";s:126:\"Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state)\";s:5:\"%file\";s:77:\"/home/jgarbe/public_html/sites/all/modules/views/plugins/views_plugin_row.inc\";s:5:\"%line\";i:134;}', 3, '', 'http://jacobgarbe.com/node/9', '', '18.216.78.185', 1740235909) in /home/jgarbe/public_html/includes/database.mysqli.inc on line 134
Custom CCK values override with PHP! | Jacob Garbe

Custom CCK values override with PHP!

This was with CCK 6.x-2.2 (shouldn't matter, all this functionality's been in CCK since back in 4.x I believe) and I needed to make a CCK value for a content type that had dynamic options to select. Basically I'm trying to mimic some of the functionality stories supply with child and parent nodes, except with regular content types. So I added a field to my custom content type.

Looks like I need to do something crazy with the allowed values list then. So I look down the list and see this disclaimer for it:

"Advanced usage only: PHP code that returns a keyed array of allowed values. Should not include <?php ?> delimiters. If this field is filled out, the array returned by this code will override the allowed values list above."

Keyed array of allowed values? Hoo boy. All right, let's break this down.

Scenario:

  1. A person makes a "hyperfiction", which is composed of "hyperfiction nodes".
  2. "hyperfictions" have "titles".
  3. When they make a "hyperfiction node", they have to select a "hyperfiction" that it belongs to from a drop-down box
  4. The drop-down box is populated by searching drupal's node table for "hyperfiction" content types created by the current user

This is what I ended up using:


PHP CODE
global $user;
$sql = "SELECT title FROM node WHERE uid = ".$user->uid." AND type = 'hyperfiction'";
$res = db_query($sql);
while($row = db_fetch_array($res)){
$rows[] = $row['title'];
}
return $rows;


 

SAME THING, IN ENGLISH
activate the capability to get the user ID of the writer that's using this
create the MySQL query to get all the titles of the hyperfictions the current user made
run that query
if you keep getting results,
keep adding the values to the options
give the total results to the drop-down

 



This PHP snippet returns a list of all the hyperfictions a user has created. I applied this for a content type called "hyperfiction node" so that I could associate all the hyperfiction nodes with their parent hyperfiction (kind of a story content type functionality, I suppose). So when they go to make a hyperfiction node, they now have to select which hyperfiction it goes with, and it provides a dropdown populated with all the hyperfictions they have made.

Awesome!

 

My Google+ profile