Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#1 | auto_nodetitle-remove_empty_tokens-1275148-1.patch | 1.67 KB | acrollet |
Comments
Comment #1
acrollet CreditAttribution: acrollet commentedI had this need too, the attached patch adds a checkbox to clean up double spaces, and does the work.
Comment #2
JonMcL CreditAttribution: JonMcL commentedI 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."
Comment #3
manish-31 CreditAttribution: manish-31 at OpenSense Labs for DrupalFit commentedComment #4
JonMcL CreditAttribution: JonMcL commentedI believe “Closed (won’t fix)” is the right status, but the wrong decision.
Any thoughts on why this reasonable feature request is denied?