For my node titles, I often want to concatenate several fields, using a space as the 'glue' character. If one of the fields is empty, I do not want a leading space, trailing space, or double space in the result. I also want to ensure that a default is used if none of the fields have any content.

Below is an example of some code. Might it be possible to integrate this functionality into the module somehow, so the same result could be achieved using (just) token placeholders rather PHP code?

$default_title = '[name not known]';
$title_pieces = array();
$title_pieces[] = "[node:field_person_name_given]";
$title_pieces[] = "[node:field_person_name_middle]";
$title_pieces[] = "[node:field_person_name_surname]";
$title = implode(' ', array_filter($title_pieces));
return ($title) ? ($title) : ($default_title);

Sorry if this is out of scope, and thanks very much for your work on a great module.

Members fund testing for the Drupal project. Drupal Association Learn more


acrollet’s picture

Status: Active » Needs review
1.67 KB

I had this need too, the attached patch adds a checkbox to clean up double spaces, and does the work.

JonMcL’s picture

I think this would be a great addition. I also wanted to be able to use multiple fields, some of which may be empty, to generate the title. I did come across a problem with Entity API's tokens when field is empty #2016457: Entity Token does not work with empty field, but not with the Token module.

A couple of comments about path at #1:
- Shouldn't you also add in a call to trim() to shave off single spaces that might appear on either end of the title?
- I believe you need to call $node->title = str_replace(' ', ' ', $node->title); twice incase the title has 3 spaces together.

And then maybe change text for the option to be something like ".... Check this box to trim extra spaces."