Motivation

We have an auto-generated label pattern that includes spaces between the tokens. Some of the tokens have empty values for an particular entity. As a result, we get superfluous spaces, and potential for ickiness — extra spaces at the beginning or end of a label can be problematic if there's inline content before or after the auto-generated label string.

Example:

  • A custom profile content type has separate fields for each part of a person's name.
  • Profile node titles are auto-generated based on a pattern comparable to the following:
    [node:field_nameprefix] [node:field_namefirst] [node:field_namemiddle] [node:field_namelast] [node:field_namesuffix]
  • For profiles of senior officials, official titles get added to page titles in pages' h1#page-title elements (but not actual <title> elements), like this:
    [node:title], [node:field_official_title]
  • When a senior official doesn't have a name suffix, the last token in the auto-generated label is empty, resulting in icky output like this:
    Susan Davidson , Executive Director

Proposed resolution

Trim auto-generated labels – i.e. when stripping tags in function _auto_entitylabel_patternprocessor:

// Strip tags.
$output = preg_replace('/[\t\n\r\0\x0B]/', '', strip_tags($output));

...could be replaced with...

// Strip tags and trim leading/trailing whitespace.
$output = preg_replace('/[\t\n\r\0\x0B]/', '', trim(strip_tags($output)));

Attached is a patch I made for version 7.x-1.1 (just because that's what we're using, so that I could get something up for our site ASAP). If there's positive feedback toward this suggestion, I'll create/submit the patch for the latest dev version the way you're supposed to.

User interface changes

(Depending on the reception of this idea, I'd also offer an edit to the description/help text for the auto_entitylabel_pattern textarea to inform users of leading/trailing whitespace trimming.)

Miscellaneous

Any reason to address multiple adjacent spaces between non-empty token values, too?– like, in the above example, when the middle name is blank. The rendered label doesn't look wrong (because adjacent spaces get "collapsed," so to speak), but there are extra spaces between field values in the rendered label markup. Does this "matter"??

Comments

bforchhammer’s picture

Thanks for the detailed description! Your suggestion sounds good to me.

Any reason to address multiple adjacent spaces between non-empty token values, too?– like, in the above example, when the middle name is blank. The rendered label doesn't look wrong (because adjacent spaces get "collapsed," so to speak), but there are extra spaces between field values in the rendered label markup. Does this "matter"??

Good question. I don't know of any cases where people rely on having multiple spaces in an entity label, so I guess we could try to clean up consecutive spaces as well.

(Depending on the reception of this idea, I'd also offer an edit to the description/help text for the auto_entitylabel_pattern textarea to inform users of leading/trailing whitespace trimming.)

I don't think that's necessary.

Attached is a patch I made for version 7.x-1.1 (just because that's what we're using, so that I could get something up for our site ASAP). If there's positive feedback toward this suggestion, I'll create/submit the patch for the latest dev version the way you're supposed to.

That would be awesome! I'm looking forward to the patch! :)

alison’s picture

Hi bforchhammer -- sorry for the absurd delay!

I kept not making time to deal with the multiple adjacent spaces between rendered token values -- and then we started noticing an actual problem caused by these consecutive spaces, so it became a priority (and just two months after that... here I am :) ).

Profile node titles are auto-generated based on a pattern comparable to the following:
[node:field_nameprefix] [node:field_namefirst] [node:field_namemiddle] [node:field_namelast] [node:field_namesuffix]

...

Any reason to address multiple adjacent spaces between non-empty token values, too?– like, in the above example, when the middle name is blank. The rendered label doesn't look wrong (because adjacent spaces get "collapsed," so to speak), but there are extra spaces between field values in the rendered label markup. Does this "matter"??

Turns out, there is a reason -- let's say the peron is Susan Davidson, middle name left blank -- in entity reference autocomplete fields, Susan Davidson's profile will not appear in the autocomplete suggestions list if you type "Susan D" -- because the actual profile title is "Susan Davidson" (two spaces between first and last).

SO, my revised proposed resolution is:

// Strip tags.
$output = preg_replace('/[\t\n\r\0\x0B]/', '', strip_tags($output));

...to be replaced with...

// Strip tags and trim leading/trailing whitespace.
$output = preg_replace('/\s\s+/', ' ', (preg_replace('/[\t\n\r\0\x0B]/', '', trim(strip_tags($output))));

Thoughts? It's a little ugly, but... (see also...)

With your okay, I will wrap this up this week.

bforchhammer’s picture

If it's possible I'd prefer having only one preg_replace, as the function is not particularly fast afaik. Apart from that I'm sitll ok with the change :-) Maybe this site can help with constructing the correct regular expression.

alison’s picture

I think that makes sense -- the only thing is that one replaces matches with '' and the other replaces matches with ' ' -- but let me see what I can do try and improve it. Thanks! (And thanks for the link -- useful site!)

alison’s picture

@bforchhammer -- I'm trying str_replace but, so far, if there are more than two consecutive spaces in the "input," I end up with two spaces in my output.

$output = str_replace(' ', ' ', (preg_replace('/[\t\n\r\0\x0B]/', '', trim(strip_tags($output)))));

(Still digging.)

alison’s picture

@bforchhammer -- I'm stumped, other than using two preg_replaces (but totally open to suggestions, of course). I'm going to try to wrap up what I have tomorrow, so at least it's ready to go, and we can always tweak it again.

bforchhammer’s picture

Okay, thanks. I'll have another look myself before commit. :)

I'm on holiday at the moment so it'll probably be at least another week or two before I get around to it.

bforchhammer’s picture

@alisonjo2786: are you still working on a patch? What's the latest? :)

bforchhammer’s picture

Version: 7.x-1.1 » 7.x-1.x-dev
alison’s picture

Ack no, total failure over here, I let it slide into the pile of "really gotta do this crap as soon as possible" that keeps getting pushed aside for firedrill after firedrill.

But anyway -- I will really, really make this happen by the end of the weekend if that works for you (hopefully sooner). Sorry for the delay!

alison’s picture

.....I'm sorry........... still failing on the time thing. This week -- not going to promise, since that hasn't been working -- but I think this week is the week. Sorry!!!!

Yuri’s picture

Hello, any progress here? Thanks!

alison’s picture

A long time ago in a galaxy far, far away... A d.o user promised a patch...

;-)

Hope this works, @bforchhammer -- I did the double-preg_replace -- totally open to alternative solutions, but this is what I could come up with. Let me know if you have any questions or concerns, or would like me to make any changes (and it won't take two more years hehe). Thanks!

alison’s picture

Hi @bforchhammer, and happy New Year! I just wanted to check in on this patch. Obviously I'm not one to rush :) But I figured I'd bump it once because I posted it right during the holiday season. Thank you!

alison’s picture

Hi! Just checking in. Thank you!

bneil’s picture

Adding a related issue, where you could use the proposed alter hook to trim the spaces off of the title as an alternative approach.

alison’s picture

Ah, nice, thanks for adding! Maybe that's a better approach so that people can do it however they want to?

basvredeling’s picture

Something like this should work, leveraging the new alter hook:

function MYMODULE_auto_entitylabel_title_alter(&$titles, $entity) {
  foreach ($titles as $language => $title) {
    $titles[$language] = trim($titles[$language]);
  }
}
dww’s picture

Version: 7.x-1.x-dev » 8.x-2.x-dev
Assigned: Unassigned » dww
Status: Active » Needs review
StatusFileSize
new1.26 KB

Sadly, #2618042: Allow title alter in auto_entitylabel_set_title never made it into the D8 port, so there's no alternative solution there.

This exact problem keeps hitting me every time I install this module. I end up writing a custom token to do the conditional check if the tokens I'm using are missing a value, but an option to just trim() the label would be way better.

Trying to strip consecutive whitespace inside the label seems like more of an edge case. It's sad that this issue has lingered for so long trying to sort out the most elegant way for that.

That said, entity label generation is an extremely rare operation in the life of a website. If we were regenerating the labels on every page view, I'd care about performance. As this is done once when an entity is created or edited, it could call preg_replace() 20 times and it wouldn't matter.

To get the ball re-started in D8 (and to have a publicly available patch for folks that want to safely deploy this to live sites), here's a trivial patch to add a simple trim option. If I'm feeling inspired, maybe I'll give it some further love to handle stray whitespace inside a label, too. I'm not sure this even needs to be a config option, but since that was so easy, I started there.

dww’s picture

Here's a version that converts 2 or more whitespace chars to a single space after we trim().

dww’s picture

Whoops, sorry, failed to attach. ;)

dww’s picture

And, if you think the config option is stupid (which it probably is), here's a version that does it automatically (with a better code comment when it happens.

Let's get one of these into the 8.x-2.x branch ASAP. Anyone up for a review and testing? Works great for my use cases on my dev sites.

Thanks!
-Derek

dww’s picture

Sorry, I was being hasty with the config option. It worked because I put it directly in my dev site's config. ;) Patches #19 and #21 don't actually work via the UI, and don't add the 'trim' option to the config schema like they should.

But, I think #22 is the way to go, anyway. I can't imagine anyone not wanting this to happen automatically. If we need it, I (or someone else) can make #21 do the right things (and perhaps harvest the code comment from #22 when the trim() is happening).

Cheers,
-Derek

mandclu’s picture

Status: Needs review » Reviewed & tested by the community

I agree 100% that this patch is needed. Tested and seems to work exactly as intended.

alison’s picture

Hi! Thank you for the patchwork! (I'm the first person to make that joke ever, right?)

TBH I haven't used this module on Drupal 8 yet, and I no longer work for the company where we were using this module on a D7 site, but I'm really happy things seem to be working out.

Am I supposed to do something to make this be finish-able, as the original reporter, or is it up to the module maintainer(s)?

Thanks again!

dqd’s picture

Priority: Normal » Major
Status: Reviewed & tested by the community » Needs review

@alisonjo2786: dww is a long term Drupal (and core) contributor and very accredited Drupal community member. You can consider his approach and reports to be qualified by any means. (EDIT: seems the previous user has changed the comment afterwards so this intial answer of me makes no sense no more.)

The most important question in the moment is if this patch still applies against latest Drupal core (8.6.x and 8.7.x -dev) If you could test this (I am on the road atm), then we can push this to get committed ASAP. I am in contact with @bforchhammer in the meantime.

Set this to major since this can be considered as a "MUST" have feature for pattern based title generation.

Just to let you know (FYI): The "make base fields managable" task in Drupal core makes a lot progress ATM with a chance for 8.7 and will hopefully render title field more flexible in the near future.

weekbeforenext’s picture

Status: Needs review » Reviewed & tested by the community

#22 Looks good to me, applies cleanly and works well.

  • dww authored b06edc7 on 8.x-2.x
    Issue #2120055 by dww, alisonjo2786: Trim auto-generated labels
    
colan’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

Status: Fixed » Closed (fixed)

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

wylbur’s picture

Parent issue: » #3117185: 8.x-3.0 Release