I am looking for sample PHP code to populate the Node Title of the CCK Custom Content Type with the data in one of the node text fields.
I am using the CCK, auto_nodetitle and token modules, and it works when I select "Automatically generate the title and hide the title field" option but it inserts the name of the Content Type. I am assuming that I need to tell it which cck field to get the data from but don't know how.

I have gone through the forums and I found code to pull data from other sources, but they are too complicated for me to understand.

Thanks
Sal

Comments

I figured it out, these are the steps if anyone else needs them.

If you load the "token" module then no PHP code is required.

1. Down load and install the token module http://drupal.org/project/token
2. Create the new content type
3. Add the custom field's
4. Go back and edit the custom content type, this time you will see a section called "Automatic title generation"
5. Expand the "Automatic title generation" section
6. Select "Automatically generate the title and hide the title field"
7. Check off the "Evaluate PHP in pattern" box
8. Expand the section called "Replacement patterns"
9. Locate the field you want to use for the Node Title and copy it ( example [field_comany_name-raw] )
10. Paste the token name in the text box named "Pattern for the title"
** note if you are using a GUI text editor module such a FCKeditor, make sure you change the setting to "plain text editor" other wise it will save the HTML code and it wont work.
11. Dont forget to save.

Now when you create content the Node Title field is no longer visible, to check that it worked go to the Administer > Content Manager > Content and look for the node you just created it should have the data of the field you selected.

z

Thanks, this helped me, this should go into the documentation somewhere.

Hi,

I've installed token module and try to follow your instruction. I'm stuck at step no. 4 because I can't find the Automatic title generation section you mentioned there.

Could you help me please?

Its easy to miss the "Automatic title generation"
It is the first collapsible link at /admin/content/types/add page when adding a new content type.
When editing an already created content type, it is still at the top, right before name and type.

Of corse do not forget to enable the auto_nodetitle and token modules after installing. I did.

Thank you for this great explanation. Best I've seen for what I would guess is a common problem.

Many thanks, this was very helpful.

In Drupal 7, folks are still working out a solution for field tokens.

You can read the auto_nodetitle modules README.txt for some ideas, but there isn't a really practical example there on how to create values from fields for D7 using just PHP.

Here is a little snippet we're using to create an auto nodetitle for a "Vehicle" node type, based on three fields: Year, Make Model. The Year field is an integer, and thus doesn't need a 'safe_value', however the other two fields are text fields, and 'safe_value' should always be preferred if it exists.

<?php
// Uncomment for testing.
// dpm($node);
print $node->field_vehicle_year['und'][0]['value'];
print
' '. $node->field_vehicle_make['und'][0]['safe_value'];
print
' '. $node->field_vehicle_model['und'][0]['safe_value'];
?>

The "best practices" way to pull a value out of the node array in Drupal 7 is by using field_get_items('node', $node, 'field_vehicle_year') instead of print $node->field_vehicle_year['und'][0]['value'];.

That's great. Thanks!
Small correction though. In D7, it would be field_get_items('node', $entity, 'field_vehicle_year'), and/or replace 'node' with whatever the entity-type is.

Michael

Where you say : replace 'node' with whatever the entity-type is.
Can you expand on that?

I am trying to return a php substr from a Text field. So what should I replace for 'node' in : field_get_items('node', $entity, 'field_i_wish')

UPDATE:
well I guess I got what I want though I am sure my php is not pretty:

<?php
$myarray
=field_get_items('node', $entity, 'field_i_wish');
$longtext = $myarray[0]['value'];
$shorttext  = substr($longtext, 0, 60);
print
'I Wish: ' . $shorttext  '…';
?>

My title becomes:
I Wish: Lorem ipsum dolor sit amet, consectetur adipisicing elit, se…

SWEET!

Thank you! This thread was very helpful.

In my case, I needed to use $node instead of $entity for the 2nd parameter for the field_get_items().

--