How do I use PHP code to create my Allowed Values List for a particular Field? I'm super new to Drupal, probably missing something obvious, but when I type code into the Allowed Values List box it just spits out my code as the available values. I've looked around and people talk as if it's doable, so, how d'ya do it??

Thanks!
Rachel

Comments

rajeevk’s picture

You mean select list ?
If you are using CCK field then you can add as many numbers of values per line in the text area given for select list. Like -
AF|Afghanistan
AL|Albania
DZ|Algeria
AS|American Samoa
AD|Andorra

If you are using webform then use option_element.

If you have some thing other in mind then please elaborate.

Thanks,
RajeevK

Rajeev Kumar,
@drupler@bihar.social (ActivityPub)

Rac7hel’s picture

Thanks for your reply. I do mean select list. But I don't want to manually add all those
AF|Afghanistan
AL|Albania
things. I want a dynamic list that pulls things from my database. Is that possible? Or is there another way to do it?

chunty’s picture

I've not specifically done this myself but here's my theory....

I think if you get your php to "print" the same thing then you should able do it, e.g:

<?php
  // You'd obvious load this from the db rather than this fixed array
  $data = array (
    'A' => 'Afganistan',
    'B' => 'Bolivia'
  );

  foreach ($data as $key=>$val) {
    // It's possible you might need to build this as a string then use return rather than print
    print $key.'|'.$val.'\n';
  }
?>
Rac7hel’s picture

It still doesn't evaluate the code. It just treats each line like text, and changes it to this:

|<?php
// You'd obvious load this from the db rather than this fixed array|// You'd obvious load this from the db rather than this fixed array
$data = array (|$data = array (
'A' => 'Afganistan',|'A' => 'Afganistan',
'B' => 'Bolivia'|'B' => 'Bolivia'
);|);
foreach ($data as $key=>$val) {|foreach ($data as $key=>$val) {
// It's possible you might need to build this as a string then use return rather than print|// It's possible you might need to build this as a string then use return rather than print
print $key.'|'.$val.'\n';
}|}

|?>

It did the same thing with the return statement scenario. :S

chunty’s picture

I've had another look at this for you I see we need to use the PHP code field rather than the allowed values list text box. To do PHP code you need to click the "PHP code" link then enter stuff in the "Code:" field.

Note you don't need the tags and it should return a value not print it like this:

return array (
'A' => 'Afganistan',
'B' => 'Bolivia'
);

Hope that helps

Rac7hel’s picture

Where exactly is this elusive "PHP code" link and field? The only box I have is the Allowed Values List.

edwardcuf’s picture

Hi Rac7hel, I am trying to do the same thing, I figured out that you need the CCK module for the PHP code field to come up. This adds stuff to fields that are not in core.

Rac7hel’s picture

That's what I needed to know. Thanks :D

fejn’s picture

This may not be what you need for you particular app, but I noticed that you are building a countries list. Drupal 7 core has lists for countries, states, and several other fields that you can access by telling Views (or where ever you have defined the field) to use one of the predefined lists.

Just hoping that this might get you what you need without so much effort.

Rac7hel’s picture

The countries list was just an example, but thanks anyway :)

clivesj’s picture

Just to wrap it up (Drupal 7):
If you need to create the Allowed Values List dynamically (form a PHP funtion)
So i.s.o.

0|first option
1|second option
2|third option

you want this:

  $options = get_my_options();
  //(...);
  return $options;
  1. Enable Core module PHP Filter
  2. Set/Check user permissions: admin/people/permissions#module-php
  3. Set/Check Text Formats: admin/config/content/formats
  4. Enable CCK Module. This step is often overlooked. However without CCK Module enabled you wont be able to enter (and thus execute) PHP-code in the Allowed-Values-List window
gasspole’s picture

thanks, i need this code too

v8powerage’s picture

How to make list with NULL, I need to let users select empty field and I don't want anything inserted instead, I tried &nbsp; but it shows in views, I want there to be nothing at all, how to do that?