Hi all,

Does anyone know if it is possible to use tokens in PHP code to access the value from a cck select list? If this is possible, does anyone have any examples of how to access these values.

I see that this is done in the link module, but was not sure if this could be retro-fitted to CCK fields using PHP code.

Thanks!

Comments

atheneus’s picture

You only need tokens to retrieve dynamic values from within a non-dynamic context (body content, menus, emails text etc.). You can get the value of a selected option in a CCK select field from a populated node.

If you have a field with the machine name 'field_my_select' then you can access the value with:
$selected = $node->field_my_select[0]['value'];
If it's a multiple select then you will have to iterate over the array since ...[0]['value'] will only retrieve the first value.

scott5615’s picture

Thanks Nyk for the response - much appreciated.

I have tried the code in a simple array and when selecting from the new field the expected value from $selected is blank. After then viewing this in the node, it appears as "1" - assuming that this is the index value of the array.

<?php
$selected = $node->field_release_version[0]['value'];
return array("value", "$selected");
?>

What could I be doing wrong here?

Cheers!

atheneus’s picture

I don't use CCK select list very often. You can peek into how the $node data is structured by using the Devel module. This adds a tab next to your 'Edit' tab on node pages called 'Dev Node' that gives a nice object browser of the $node object. That should guide you in how to access the value you need in your code.

[EDIT] - So I just looked at one of my sites using content taxonomy select lists and yes the value is the ID of the taxonomy term, so I think that what you are getting is the id of the select list item. That means you would have to look up the label on that item.

scott5615’s picture

Thanks for the info - can you point me at how to look up the label, I think that this is where I am going wrong! I have looked, both in the database and in the forums but I'm not able to locate.

Many thanks.

atheneus’s picture

If your select list is stored as 'Allowed values' in the CCK select type of text field in the form:

1|value 1
2|value 2
3|value 3
...

Then this is stored as a serialized array in the 'content_node_field' table. It is in the 'global_settings' field.

What you would need to do is to pull this from the database and deserialize the string into a PHP array:

 $settings = unserialize($string_from_db);
$values = $settings['allowed_values'];

Then you are going to have to parse the string "1|value1..." to get the appropriate value. This is quite messy, but that is how things are stored.

Your other option is to make your select list a vocabulary in your taxonomy and use a 'content taxonomy field' in CCK to manage your list of values. If you do that you can easily use the taxonomy API to lookup a term id and get the correct display value. This is the approach I usually take with CCK select lists.

scott5615’s picture

Thanks for the information - I see that what I am trying to achieve is not possible without some module development. I will press on with learning about module development.

Regards.

atheneus’s picture

Actually you should be able to implement this sort of value lookup in the theming layer, rather than as a module. What you would do is override your preprocessor function for rendering nodes in your theme's template.php file to lookup the value and then expose the display value that you looked up as a variable that you can use to display the value in your node.tpl.php template file.

http://drupal.org/node/223430

This Theme handbook page gives a rundown of how to use theme preprocessor functions to process a value before sending it to be displayed in your template.

haebooty’s picture

Did you ever figure out how to use tokens in CCK select lists? I have been trying to figure out for weeks.

Please let me know