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

Data overrides in template hide chained object properties #9915

Open
OldStarchy opened this issue Apr 12, 2021 · 3 comments
Open

Data overrides in template hide chained object properties #9915

OldStarchy opened this issue Apr 12, 2021 · 3 comments

Comments

@OldStarchy
Copy link
Contributor

Affected Version

4.6.2

Description

Given the following PHP and template

$email = Email::create($from, $to, $subject);
$email->setHTMLTemplate('Email\\Foo');
$email->setData([
    'Link' => 'Literal Link',
    'Page' => $somePage,
]);
$email->send();

Email/Foo.ss

Link             => $Link
Page Action Link => $Page.Link('my_action')

The expected output is

Link             => Literal Link
Page Action Link => page-slug/my_action

However, the actual output comes through as

Link             => Literal Link
Page Action Link => Literal Link

Stepping through the code I found the part where its calling XML_val in the template

$val .= $scope->locally()->obj('Page', null, true)->XML_val('Link', ['my_action'], true);

This leads to SSViewer_DataPresenter.php#L350, where processTemplateOverride sees that Link is defined and ignores the fact that it should be scoped to $Page.

@michalkleiner
Copy link
Contributor

$Page is a special global template variable that can get a page by its slug, and defaults to the current page if no slug is provided (afaik), so using it to provide custom data to a template isn't recommended. If you use any other custom variable to pass in your custom page object, such as MyPage, it will work ok, I believe.

@michalkleiner
Copy link
Contributor

Ok, it seems to fallback to the homepage based on this (https://github.com/silverstripe/silverstripe-cms/blob/4/code/Controllers/ContentController.php#L134) and the subsequent calls.

I'd still recommend renaming the variable, it's the safest thing.

@OldStarchy
Copy link
Contributor Author

I used Page as an example, in my actual code I had

$email->setData([
    'AbsoluteBaseURL' => Director::absoluteBaseURL(),
    'Link' => $productSelection->getSelectionLink(),
    'CustomerFollowupPage' => $customerFollowupPage,
]);

I've changed it to

$email->setData([
    'AbsoluteBaseURL' => Director::absoluteBaseURL(),
    'ProductSelection' => $productSelection,
    'CustomerFollowupPage' => $customerFollowupPage,
]);

@GuySartorelli GuySartorelli changed the title Data overrides in email template hide chained object properties Data overrides in template hide chained object properties Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants