I have created a drupal 7 module 'List (PHP)' that is sort of combination of List module and Computed Field. You use PHP to provide the values for the options. Additionally it passes in the values of other fields to allow for dynamic lists in combination with #ajax.

CommentFileSizeAuthor
list_php.zip4.01 KBgrom358
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

robcarr’s picture

@grom358 - thanks for posting this.

This *almost* works for me: I want to use PHP to grab allowed values for a content type text list from a specific node (so that the allowed values list is effectively editable by certain end users, rather than requiring intervention of a site builder). So have used the following code to generate my allowed values.

$nid = 1234; //nid where body contains default values on separate lines 
$delta = 0;
$language = 'und';

$entity = entity_load('node', array($nid));
$body_orig = $entity[$nid]->body[$language][$delta]['safe_value'];
$body = str_replace(array("\n"),"|",$body_orig);
$arr = explode("|",$body);
//return drupal_map_assoc($arr);
return $arr;

Originally, I was generating an associative array (as commented out in above code) so I could use the PHP evaluation of the CCK module. However, this resulted in 'An illegal choice has been detected...' error and I could not save the node if any of the check boxes had been selected (it's set to unlimited values, so the node edit form presents check boxes).

So I've enabled the list_php module, set up a field within a content type and selected a few of the computed allowed values in a node. This time the node has been saved without error, but has not recorded any of the selected check box values. However, I'm getting a Warning: array_fill() [function.array-fill]: Number of elements must be positive in list_php_options_list() (line 306 of /list_php/list_php.module). error on the content type field settings page, so I suspect I'm not using your contributed code correctly.

Would welcome any advice, examples or even a tiny bit of guidance

EDIT: Tried the old 'turn it all off, then on again' approach (ie, deleted a field, cleared caches and created a new field): all works fine. Only minor snag is that I'm still seeing the array_fill() error

robcarr’s picture

Quick fix that seems to stop the Warning: array_fill() [function.array-fill]: Number of elements must be positive in list_php_options_list() (line 306 of /list_php/list_php.module) error occurring by checking that $field['cardinality'] is positive:

<?php
function list_php_options_list($field) {
  // Return empty array. The options are set by list_php_field_widget_form_alter
  if($field['cardinality'] >= 0){
    return array_fill(0, $field['cardinality'], '- None -');
  } else {
    return null;
  }
}
?>

Would roll patch but not sure anyone else is that interested...

yang_yi_cn’s picture

this is great! can some one commit this or put it into a separate module?

KarenS’s picture

Status: Active » Closed (won't fix)

No new features are going into CCK for D7. If someone wants something like this is will need to be a new project. And it sounds like it ought to be its own project anyway. There is a process for applying to create a new project, or you could start it out as a sandbox. I'm not sure what the process is, but Google around and you should be able to find it.