3

So, this is a newbie question. I'm using Automatic Nodetitles with a custom content type. I'd like the node's title to be equivalent to one of my fields. After some snooping around I came up with this:

<?php return $node->field_my_field ?>

I put this in the "Pattern for the title" field, but this just returns 'Array'. So, it must be an array. However, I'm not having any luck figuring out how to index it to get the value I'm looking for. Everything I tried just gives me 'Undefined offset' errors.

This can't be too hard.... Thanks!

3
<?php return $node->field_my_field['und'][0]['value']; ?>
| improve this answer | |
  • I knew it wasn't that difficult. Thank you! Actually, 'safe_value' gave me a undefined index error, but 'value' worked. Is 'safe_value' only for certain field types? – stahlous Apr 6 '11 at 5:09
4

I think it's better idea to use token instead of php code for this simple replacement.

When you have the token module installed you'll have a list with available tokens under the Pattern for the title: input box. And if your field is named *field_my_field* then you'll have the tokens

[field_my_field-formatted]
[field_my_field-raw]

or something like that to choose from.

| improve this answer | |
  • I second Martin's response. Token module is the way to go, it makes using modules like Automatic Nodetitles (and anything else where you might need to quickly reference fields, etc.) so much easier and standardized for your site. – bjornmeansbear Apr 7 '11 at 5:09
  • I do have the token module installed, and I spent some time searching through the available token list underneath the "Pattern for the title" text box to find what I wanted, but I didn't see anything like these tokens. I tried your suggestion, but it didn't render. I just got back "[field_my_field-raw]". – stahlous Apr 8 '11 at 21:02
  • Thing is (as hairyfro indicated in his comment) the Token module does not seem to expose these fields - same issue here :-( – DilbertDave Apr 20 '11 at 9:11
1

For tokens what finally worked for me was

[node:field_my_field]

That being said I am using Automatic Entity Labels so it may not.

| improve this answer | |
0

I agree that it may be better to use tokens. To add to martin's post, and in response to the issue that DilbertDave raises, I believe that though most of the Token API has been added into Drupal7, certain aspects of the original token plugin still haven't, including, field support. So to that end, you may find it helpful to install the Token module which will then enable you to use field-based tokens such as others here have suggested:

[field_my_field-formatted]
[field_my_field-raw]
| improve this answer | |
  • You didnt add anything to Martin's answer and your answer does nothing to solve issues the issue that DilbertDave raised. – Craicerjack Feb 19 '14 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.