-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Fix trim issue with empty page title #37385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sippsolutions
wants to merge
3
commits into
magento:2.4-develop
Choose a base branch
from
sippsolutions:fix-trim-issue
base: 2.4-develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we fix this elsewhere? The
__()
function also requires its argument to be a string, so the current suggestion isn't necessarily enough to fix the problem advertised.Does the problem value come from
$this->pageTitle
or...->getShortHeading()
?Perhaps we can improve the type information in the
setPageTitle()
method in this class, or fix the return value of...->getShortHeading()
if that's the source of the "bad" value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__()
does not require a string:gettype(__(null)->render())
=string
so the given fix works perfectly for us.Both
pageTitle
andgetShortHeading
are not typed and will not be in the near future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps the code has changed in the years since my comment. It looks like the Phrase constructor now does its own cast to a string for its argument.
magento2/lib/internal/Magento/Framework/Phrase/__.php
Lines 16 to 24 in 869d8b6
magento2/lib/internal/Magento/Framework/Phrase.php
Lines 67 to 72 in 869d8b6
However, my comment regarding fixing the definition of the
$pageTitle
variable is still relevant here. We should not need to cast the value on this line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pageTitle
is not typed and will probably never be. So it could be anything, especially when dealing with 3rd party code :-)getPageHeading
is not typed as well, so about your point fixing it somewhere else, I don't quite get where exactly.We could use
trim((string)$block->getPageHeading()
intitle.phtml
, if you prefer that ...We won't have the issue anywhere else, the only problem is that
trim()
expects astring
as input, while Magento is not and probably will never be able to provide strict types in 2.x.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a quick search just now, it looks like the property is typed (via a comment) as a string, so the root cause of the bug you're witnessing seems to be that
getShortHeading()
returnsmixed
and notstring
. Can we fix this in that method instead?magento2/lib/internal/Magento/Framework/View/Page/Title.php
Line 88 in 869d8b6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->pageTitle
can benull
as well.See for example
\Magento\Catalog\Helper\Product\View::preparePageMetadata
where$pageMainTitle->setPageTitle($product->getName())
is used, while$product
could possibly have anull
name if imported via a wrongly formatted CSV-file by the customer (a case we experienced sometimes already)I think there are too many possibilities all which only could be resolved by typing all corresponding methods and properties, which imo has a too big impact on stability, thinking about the thousands of extensions that are out there that may probably stop working after we enforce a
string
type on e.g.setPageTitle
. The casting to string was the least risky option in my head, but feel free to adjust the PR if you feel other.