My content type has first name and surname. The title should be "[firstname] [surname]". First name is optional. If first name is left out there is whitespace before the surname. It would be awesome if there was a checkbox to trim() the automatically generated node title without having to enter php.

CommentFileSizeAuthor
#3 issue-1166594-3-trim-node-title.patch375 bytesmr.york
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jcarlson34’s picture

Status: Active » Needs review

+1

I have a similar issue with [firstname] [lastname] [suffix]

Since the suffix doesn't often exist, I end up with a lot of Jane Doe{space} in my titles. The trim() command does fix this issue.

I haven't learned how to patch for git yet but here's the simple fix on line 138 (just put trim around the $output variable in auto_nodetitle.module):

/**
  * Helper function to generate the title according to the settings.
  *
  * @return a title string
  */
function _auto_nodetitle_patternprocessor($pattern, $node) {
  // Replace tokens.
  $output = token_replace($pattern, array('node' => $node), array('sanitize' => FALSE));
  // Evalute PHP.
  if (variable_get('ant_php_' . $node->type, 0)) {
    $output = auto_nodetitle_eval($output, $node);
  }
  // Strip tags.
  $output = preg_replace('/[\t\n\r\0\x0B]/', '', strip_tags($output));
  return trim($output);
}
rwilson0429’s picture

Thanks, the trim function worked for me for my current specific case. To the extent that trim () only removes whitespaces and other predefined characters from both sides of a string, whenever the pattern includes empty fields in the middle of the pattern, the problem will persist. It is not practical to make all of the fields used in the replacement pattern a required field. Is there a way to trim all fields in the pattern before the result is returned?

mr.york’s picture

Issue summary: View changes
FileSize
375 bytes

Patch added.

mglaman’s picture

Title: Add option to trim node title » Trim node title after pattern processing
Status: Needs review » Reviewed & tested by the community

Worked as expected. Changed title. I don't think this should be an "option" but something it should just do as pure cleanup once processed.

anmolgoyal74’s picture

Assigned: Unassigned » anmolgoyal74

Patch is working as designed

gaurav.kapoor’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.