Skip to content

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
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

sippsolutions
Copy link
Member

Description (*)

Fix trim issue with empty page title
See app/code/Magento/Theme/view/frontend/templates/html/title.phtml:12

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • [ x All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Apr 17, 2023

Hi @sippsolutions. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.

Add the comment under your pull request to deploy test or vanilla Magento instance:
  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@m2-github-services m2-github-services added Partner: TechDivision partners-contribution Pull Request is created by Magento Partner labels Apr 17, 2023
@sippsolutions
Copy link
Member Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Hotel engcom-Hotel added the Priority: P3 May be fixed according to the position in the backlog. label Apr 18, 2023
Copy link
Member

@fredden fredden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution. I've left a comment regarding completeness of the suggested fix.

@@ -79,7 +79,7 @@ public function getPageHeading()
{
$pageTitle = !empty($this->pageTitle) ? $this->pageTitle : $this->pageConfig->getTitle()->getShortHeading();

return $this->shouldTranslateTitle() ? __($pageTitle) : $pageTitle;
return $this->shouldTranslateTitle() ? __($pageTitle) : (string)$pageTitle;
Copy link
Member

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.

Copy link
Member Author

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 and getShortHeading are not typed and will not be in the near future.

Copy link
Member

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.

function __(...$argc)
{
$text = array_shift($argc);
if (!empty($argc) && is_array($argc[0])) {
$argc = $argc[0];
}
return new \Magento\Framework\Phrase($text, $argc);
}

* @param string $text
* @param array $arguments
*/
public function __construct($text, array $arguments = [])
{
$this->text = (string)$text;

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.

Copy link
Member Author

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() in title.phtml, if you prefer that ...

We won't have the issue anywhere else, the only problem is that trim() expects a string as input, while Magento is not and probably will never be able to provide strict types in 2.x.

Copy link
Member

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() returns mixed and not string. Can we fix this in that method instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->pageTitle can be null as well.
See for example \Magento\Catalog\Helper\Product\View::preparePageMetadata where $pageMainTitle->setPageTitle($product->getName()) is used, while $product could possibly have a null 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.

@sippsolutions
Copy link
Member Author

@magento run all tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Partner: TechDivision partners-contribution Pull Request is created by Magento Partner Priority: P3 May be fixed according to the position in the backlog. Progress: review
Projects
Status: Review in Progress
Development

Successfully merging this pull request may close these issues.

4 participants