Hi,

I'm trying to extract the last name from a full name provided by a CCK node reference field. The field's name is "field_komponist" and contains a string like "Johann Sebastian Bach"; the node reference field is tokenized correctly: In one content type, I activated "Automatic title generation"; by using tokens and with "Evaluate PHP in pattern" unchecked, the following is working properly:

[field_komponist-title] - [field_werk-raw]

This gives me an automatically generated node title like

Johann Sebastian Bach - Goldbergvariationen

For the purpose of sorting nodes properly in a view, I'd like to extract the last name part from [field_komponist-title] by adapting the syntax from readme.txt:

<?php
  $token = '[field_komponist-title]';
  $array = explode(" ", $token);
  $name = $array[count($array) - 1];
  return $name;
?> - [field_werk-raw]

This is supposed to put the data from "field_komponist" via token into an array, which gets split up at the whitespace " "; out of this array, I'm trying to extract the last element (hopefully the last name), and to output it into the node's title.

Now with "Evaluate PHP in pattern" checked, this gives me a node title like

Goldbergvariationen

The PHP code gets eaten completely, and I have no idea why the resulting string is not at least

- Goldbergvariationen

Two whitespaces " " and the "-" are getting swallowed, also.

Further, the following string gives me an empty nodetitle; obviously, something very basic is not working right (most probably in my code):

<?php
  $token = '[field_komponist-title]';
  return $token;
?>

Shouldn't this not at least output the full name from "field_komponist"? Also, the following string does not work and again gives an empty field:

<?php
  $token = '[field_werk-raw]';
  return $token;
?>

Without PHP evaluation, [field_werk-raw] outputs the correct title!

Of course a concatenation of those string does not work, either:

<?php
  $token = '[field_komponist-title]';
  $array = explode(" ", $token);
  $name = $array[count($array) - 1];
  $werk = '[field_werk-raw]';
  return $name; . " - " . return $werk;
?>

Please forgive my absolute ignorance about PHP, I've never written any code in this language and solely adapted examples which seemed to be suitable. However, this problem is driving me crazy since I don't understand what's going wrong.

Thank you for any tipps or pointers!

Greetings, -asb

Comments

asb’s picture

Version: 5.x-1.1 » 6.x-1.2
Status: Active » Closed (duplicate)