Skip to content
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

FIX Don't error if template global is null #11331

Conversation

GuySartorelli
Copy link
Member

@GuySartorelli GuySartorelli commented Aug 8, 2024

Description

Fixes a bug where if template variables from a TemplateGlobalProvider return null and you chain off them, you'd get a null reference error.

The getObj() method is only ever called in one place, which sets the current item in scope - so if the current item in scope is null, we just keep null as the item in scope, which results in the behaviour we expect (i.e. nulls result in just not dumping anything to the template, are falsy in if statements, etc).

Manual testing steps

See reproduction steps in the issue description

Issues

Pull request checklist

  • The target branch is correct
  • All commits are relevant to the purpose of the PR (e.g. no debug statements, unrelated refactoring, or arbitrary linting)
    • Small amounts of additional linting are usually okay, but if it makes it hard to concentrate on the relevant changes, ask for the unrelated changes to be reverted, and submitted as a separate PR.
  • The commit messages follow our commit message guidelines
  • The PR follows our contribution guidelines
  • Code changes follow our coding conventions
  • This change is covered with tests (or tests aren't necessary for this change)
  • CI is green

@@ -43,4 +43,9 @@ public static function get_argmix()
$args = func_get_args();
return 'z' . implode(':', $args) . 'z';
}

public static function getNull()
Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to use : null as a typehint here but apparently PHP doesn't allow that 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

isset(null) === false && isnull(null) === true.
PHP doesn't have a concept of "is set, but is null", so I guess that makes sense 😅

Comment on lines +367 to +371
public function testGlobalVariablesReturnNull()
{
$this->assertEquals('<p></p>', $this->render('<p>$SSViewerTest_GlobalReturnsNull</p>'));
$this->assertEquals('<p></p>', $this->render('<p>$SSViewerTest_GlobalReturnsNull.Chained.Properties</p>'));
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Ran this before doing the code change and can confirm it fails by throwing an exception without the code change.

@emteknetnz emteknetnz merged commit 7b91207 into silverstripe:5.2 Aug 8, 2024
15 checks passed
@emteknetnz emteknetnz deleted the pulls/5.2/template-global-null-chain branch August 8, 2024 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants