Problem/Motivation

"description" Meta tag from token "[node:summary]" is resulting in linebreaks in code. I guess meta tag values should have no line breaks?

Possibly related Drupal 7 issue: #1978568: Don't output line breaks in meta tags

Steps to reproduce

Write node entry with line breaks in summary
Set up Node meta tag for Meta description
Set "[node:summary]" as value
Check source and see the line breaks

Proposed resolution

Filter line breaks before output

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork metatag-3218647

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Anybody created an issue. See original summary.

luisrhaas made their first commit to this issue’s fork.

damienmckenna’s picture

This should be added to MetaNameBase::output().

damienmckenna’s picture

Title: Remove line breaks from Meta Description (and others?) » Remove line breaks from meta tag output
damienmckenna’s picture

Issue tags: +Needs tests

Some test coverage would be useful, to make sure this works as expected.

luisrhaas’s picture

Status: Active » Needs review

I created a method to allow subclasses implement a custom filter when it is necessary. Please review.

damienmckenna’s picture

That's a good start, but can you please move the logic back into the parent MetaNameBase meta tag? There's no point in having to do this on a per-tag basis.

luisrhaas’s picture

Hi Damien! I did a new commit moving the logic to parent class, please let me know if now the solution is ok.

I did that to avoid affect any other class which extends MetaNameBase.

damienmckenna’s picture

Issue summary: View changes
Status: Needs review » Needs work

The only issue I can see with this is that meta tags with URLs to file names which include multiple concurrent spaces would have problems, but honestly that's something sites should resolve using something like Transliterate Filenames.

Beyond that, this looks good.

Now for some test coverage.

anybody’s picture

Just one crazy idea outside the box... (may really be nonsense ;) )

Allow to use a text format (input filter) to let the user control the behavior wouldn't make sense, would it? I tend to say it would only further complicate things, but thought it would be worth to write down that idea... ;D

damienmckenna’s picture

@Anybody: No, that wouldn't fix the underlying problem and would just make it more complicated to manage.

luisrhaas’s picture

Hi Damien!

I made a unit test for the new function filterValue, please let me know if it's ok.

damienmckenna’s picture

Issue tags: -Needs tests
Parent issue: » #3203686: Plan for Metatag 8.x-1.17

Other than some minor whitespace / coding standards corrections, this looks great!

Idea - let's combine all the logic into the existing tidy() method.

luisrhaas’s picture

Hi Damien! Coding standard fixed, please check.

luisrhaas’s picture

Status: Needs work » Needs review

Changing to needs review

damienmckenna’s picture

Status: Needs review » Needs work

That's better, thank you.

The logic needs to be moved into the tidy() method.

luisrhaas’s picture

Status: Needs work » Needs review

Hi Damien! I moved all the logic to tidy function and fixed the tests, thank you for the support

damienmckenna’s picture

Status: Needs review » Reviewed & tested by the community

Excellent! I love it!

I'll work on the comments a little prior to committing it, but otherwise this is perfect.

damienmckenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed, with some comment improvements. Thank you!

anybody’s picture

So great to see, thank you both very very much!

Status: Fixed » Closed (fixed)

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

idebr’s picture

In addition to line breaks, meta tag output is now also cleared on duplicate spaces. This is a BC break for projects that implemented a custom filter for empty elements in the output, eg:

/**
 * Implements hook_metatags_attachments_alter().
 */
function hook_metatags_attachments_alter(array &$metatag_attachments) {
  foreach ($metatag_attachments['#attached']['html_head'] as $id => &$attachment) {
    if ($attachment[1] === 'title') {
      $attachment[0]['#attributes']['content'] = str_replace('-  -', '-', $attachment[0]['#attributes']['content']);
    }
  }
}
anybody’s picture

Thank you for your feedback @idebr - any suggestion how to solve this for all cases? I'm not sure what's worse...
Perhaps add a setting or config value to disable cleanup? I guess for most cases (as metatag is based on token, which tend not to be "clean") a cleanup is more useful than double blanks like in the edge-case?

Another alternative that comes to my mind would be to run the hook later or add a further later hook?

lendude’s picture

Well the point of this issue was to remove newlines from the output, so why are we stripping double whitespace characters here?

$value = preg_replace('/\s+/', ' ', $value);

From just reading this issue, this seems out of scope and potentially not needed at all. Or are double whitespace characters violating some metatag markup standards?

damienmckenna’s picture

It's common practice to replace line breaks with a space, to make sure that sentences have a gap between them. It's also common to find extra spaces after the last period in a paragraph. Put these together and multiple concurrent spaces can be a side effect of joining paragraphs together. So then we strip out multiple spaces so that there's only one space separating sentences. This is a common practice and actually brings it in line with Metatag on D7.

I'm sorry this resulted in a subtle break in backwards compatibility for your sites. Looking at the custom code provided in #24 it should be simple to adjust to the new logic. FWIW we won't be reverting this change, and making it optional is over-engineering the problem and unnecessary, but I thank you for raising the point.