Trying to access values from nodes to render in email template using swift mailer
up vote
2
down vote
favorite
I'm currently working on trying to build an email html template using swift mailer. My problem right now is how can I customize the template to fetch the values from the nodes and render them in the email template. Fetch and print/render the node values only from the nodes that will be sent as shown in the screenshot below.
Swift mailer has a default way to output the values from all the fields using {{ body }}
Is there a way I could access the values from the fields and render them on the template. Something like the code below that works on node template
{{content.field_title.value}}
or {{node.field_title.value}}
Swift mailer has a way to render values via preprocess but I can't seem to make it work. Below is the code that is a work in progress
https://www.drupal.org/node/1590184
function swiftmailer_preprocess_swiftmailer(&$variables) {
$variables['node_title'] = $node->getTitle();
}
php email drupal theming
This question had a bounty worth +50
reputation from clestcruz that ended 17 hours ago. Grace period ends in 6 hours
The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.
add a comment |
up vote
2
down vote
favorite
I'm currently working on trying to build an email html template using swift mailer. My problem right now is how can I customize the template to fetch the values from the nodes and render them in the email template. Fetch and print/render the node values only from the nodes that will be sent as shown in the screenshot below.
Swift mailer has a default way to output the values from all the fields using {{ body }}
Is there a way I could access the values from the fields and render them on the template. Something like the code below that works on node template
{{content.field_title.value}}
or {{node.field_title.value}}
Swift mailer has a way to render values via preprocess but I can't seem to make it work. Below is the code that is a work in progress
https://www.drupal.org/node/1590184
function swiftmailer_preprocess_swiftmailer(&$variables) {
$variables['node_title'] = $node->getTitle();
}
php email drupal theming
This question had a bounty worth +50
reputation from clestcruz that ended 17 hours ago. Grace period ends in 6 hours
The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.
1
How do you pass the node to the swiftmailer. With simplenews or ...? Please try to var_dump variables and check what inside. The preprocess is the right thing to do.
– shaxaaa
Nov 22 at 7:57
@shaxaaa actually haven't figured it out yet to pass the node to the swiftmailer. I'm still a beginner so things likepreprocess
I haven't figured it out yet
– clestcruz
Nov 22 at 9:14
1
Is it always the content of one node. I am not sure what you need to develop.
– shaxaaa
Nov 22 at 9:26
@shaxaaa yes, it is the content of one node
– clestcruz
Nov 23 at 4:40
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm currently working on trying to build an email html template using swift mailer. My problem right now is how can I customize the template to fetch the values from the nodes and render them in the email template. Fetch and print/render the node values only from the nodes that will be sent as shown in the screenshot below.
Swift mailer has a default way to output the values from all the fields using {{ body }}
Is there a way I could access the values from the fields and render them on the template. Something like the code below that works on node template
{{content.field_title.value}}
or {{node.field_title.value}}
Swift mailer has a way to render values via preprocess but I can't seem to make it work. Below is the code that is a work in progress
https://www.drupal.org/node/1590184
function swiftmailer_preprocess_swiftmailer(&$variables) {
$variables['node_title'] = $node->getTitle();
}
php email drupal theming
I'm currently working on trying to build an email html template using swift mailer. My problem right now is how can I customize the template to fetch the values from the nodes and render them in the email template. Fetch and print/render the node values only from the nodes that will be sent as shown in the screenshot below.
Swift mailer has a default way to output the values from all the fields using {{ body }}
Is there a way I could access the values from the fields and render them on the template. Something like the code below that works on node template
{{content.field_title.value}}
or {{node.field_title.value}}
Swift mailer has a way to render values via preprocess but I can't seem to make it work. Below is the code that is a work in progress
https://www.drupal.org/node/1590184
function swiftmailer_preprocess_swiftmailer(&$variables) {
$variables['node_title'] = $node->getTitle();
}
php email drupal theming
php email drupal theming
edited Nov 24 at 2:13
asked Nov 22 at 5:49
clestcruz
3551237
3551237
This question had a bounty worth +50
reputation from clestcruz that ended 17 hours ago. Grace period ends in 6 hours
The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.
This question had a bounty worth +50
reputation from clestcruz that ended 17 hours ago. Grace period ends in 6 hours
The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.
1
How do you pass the node to the swiftmailer. With simplenews or ...? Please try to var_dump variables and check what inside. The preprocess is the right thing to do.
– shaxaaa
Nov 22 at 7:57
@shaxaaa actually haven't figured it out yet to pass the node to the swiftmailer. I'm still a beginner so things likepreprocess
I haven't figured it out yet
– clestcruz
Nov 22 at 9:14
1
Is it always the content of one node. I am not sure what you need to develop.
– shaxaaa
Nov 22 at 9:26
@shaxaaa yes, it is the content of one node
– clestcruz
Nov 23 at 4:40
add a comment |
1
How do you pass the node to the swiftmailer. With simplenews or ...? Please try to var_dump variables and check what inside. The preprocess is the right thing to do.
– shaxaaa
Nov 22 at 7:57
@shaxaaa actually haven't figured it out yet to pass the node to the swiftmailer. I'm still a beginner so things likepreprocess
I haven't figured it out yet
– clestcruz
Nov 22 at 9:14
1
Is it always the content of one node. I am not sure what you need to develop.
– shaxaaa
Nov 22 at 9:26
@shaxaaa yes, it is the content of one node
– clestcruz
Nov 23 at 4:40
1
1
How do you pass the node to the swiftmailer. With simplenews or ...? Please try to var_dump variables and check what inside. The preprocess is the right thing to do.
– shaxaaa
Nov 22 at 7:57
How do you pass the node to the swiftmailer. With simplenews or ...? Please try to var_dump variables and check what inside. The preprocess is the right thing to do.
– shaxaaa
Nov 22 at 7:57
@shaxaaa actually haven't figured it out yet to pass the node to the swiftmailer. I'm still a beginner so things like
preprocess
I haven't figured it out yet– clestcruz
Nov 22 at 9:14
@shaxaaa actually haven't figured it out yet to pass the node to the swiftmailer. I'm still a beginner so things like
preprocess
I haven't figured it out yet– clestcruz
Nov 22 at 9:14
1
1
Is it always the content of one node. I am not sure what you need to develop.
– shaxaaa
Nov 22 at 9:26
Is it always the content of one node. I am not sure what you need to develop.
– shaxaaa
Nov 22 at 9:26
@shaxaaa yes, it is the content of one node
– clestcruz
Nov 23 at 4:40
@shaxaaa yes, it is the content of one node
– clestcruz
Nov 23 at 4:40
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
function swiftmailer_preprocess_swiftmailer(&$variables) {
// Don't forget to use the Node class on top of your .module or .theme file.
$node = Node::load(YourNodeId); // Here add your node id. If you don't want it to be hardcoded I would suggest you to create a config page for it. The other way is to put a checkbox in it and then find it here with a query.
$variables['node_title'] = $node->getTitle();
}
After you do this in your swiftmailer.html.twig you will be able to render the node title like so {{ node_title }}
add a comment |
up vote
0
down vote
@clestcruz, Preprocess is good option for this. But You can create service as well for this.
Create custom module named as foo
.
Create all required files and then create foo.services.yml
.
mention service like :
services:
foo.twig.TwigExtension:
class: DrupalfooXYZ
tags:
- {name: twig.extension}
create service file in foo/src/XYZ.php
<?php
namespace Drupalfoo;
use DrupalblockEntityBlock;
use DrupaluserEntityUser;
use DrupalnodeEntityNode;
use DrupaltaxonomyEntityTerm;
use DrupalparagraphsEntityParagraph;
use DrupalCoreUrl;
/**
* Class DefaultService.
*
* @package Drupalfoo
*/
class XYZ extends Twig_Extension {
/**
* {@inheritdoc}
* This function must return the name of the extension. It must be unique.
*/
public function getName() {
return 'product_listing_extend_display';
}
/**
* In this function we can declare the extension function.
*/
public function getFunctions() {
return array(
'getData' => new Twig_Function_Method( $this, 'getData', array('is_safe' => array('html'))),
);
}
// Function to get tax childs by tid
function getData($id) {
// query for data
// return value
}
}
Call method {{ getData() }} in twig file.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
function swiftmailer_preprocess_swiftmailer(&$variables) {
// Don't forget to use the Node class on top of your .module or .theme file.
$node = Node::load(YourNodeId); // Here add your node id. If you don't want it to be hardcoded I would suggest you to create a config page for it. The other way is to put a checkbox in it and then find it here with a query.
$variables['node_title'] = $node->getTitle();
}
After you do this in your swiftmailer.html.twig you will be able to render the node title like so {{ node_title }}
add a comment |
up vote
2
down vote
function swiftmailer_preprocess_swiftmailer(&$variables) {
// Don't forget to use the Node class on top of your .module or .theme file.
$node = Node::load(YourNodeId); // Here add your node id. If you don't want it to be hardcoded I would suggest you to create a config page for it. The other way is to put a checkbox in it and then find it here with a query.
$variables['node_title'] = $node->getTitle();
}
After you do this in your swiftmailer.html.twig you will be able to render the node title like so {{ node_title }}
add a comment |
up vote
2
down vote
up vote
2
down vote
function swiftmailer_preprocess_swiftmailer(&$variables) {
// Don't forget to use the Node class on top of your .module or .theme file.
$node = Node::load(YourNodeId); // Here add your node id. If you don't want it to be hardcoded I would suggest you to create a config page for it. The other way is to put a checkbox in it and then find it here with a query.
$variables['node_title'] = $node->getTitle();
}
After you do this in your swiftmailer.html.twig you will be able to render the node title like so {{ node_title }}
function swiftmailer_preprocess_swiftmailer(&$variables) {
// Don't forget to use the Node class on top of your .module or .theme file.
$node = Node::load(YourNodeId); // Here add your node id. If you don't want it to be hardcoded I would suggest you to create a config page for it. The other way is to put a checkbox in it and then find it here with a query.
$variables['node_title'] = $node->getTitle();
}
After you do this in your swiftmailer.html.twig you will be able to render the node title like so {{ node_title }}
answered Nov 23 at 9:00
shaxaaa
10918
10918
add a comment |
add a comment |
up vote
0
down vote
@clestcruz, Preprocess is good option for this. But You can create service as well for this.
Create custom module named as foo
.
Create all required files and then create foo.services.yml
.
mention service like :
services:
foo.twig.TwigExtension:
class: DrupalfooXYZ
tags:
- {name: twig.extension}
create service file in foo/src/XYZ.php
<?php
namespace Drupalfoo;
use DrupalblockEntityBlock;
use DrupaluserEntityUser;
use DrupalnodeEntityNode;
use DrupaltaxonomyEntityTerm;
use DrupalparagraphsEntityParagraph;
use DrupalCoreUrl;
/**
* Class DefaultService.
*
* @package Drupalfoo
*/
class XYZ extends Twig_Extension {
/**
* {@inheritdoc}
* This function must return the name of the extension. It must be unique.
*/
public function getName() {
return 'product_listing_extend_display';
}
/**
* In this function we can declare the extension function.
*/
public function getFunctions() {
return array(
'getData' => new Twig_Function_Method( $this, 'getData', array('is_safe' => array('html'))),
);
}
// Function to get tax childs by tid
function getData($id) {
// query for data
// return value
}
}
Call method {{ getData() }} in twig file.
add a comment |
up vote
0
down vote
@clestcruz, Preprocess is good option for this. But You can create service as well for this.
Create custom module named as foo
.
Create all required files and then create foo.services.yml
.
mention service like :
services:
foo.twig.TwigExtension:
class: DrupalfooXYZ
tags:
- {name: twig.extension}
create service file in foo/src/XYZ.php
<?php
namespace Drupalfoo;
use DrupalblockEntityBlock;
use DrupaluserEntityUser;
use DrupalnodeEntityNode;
use DrupaltaxonomyEntityTerm;
use DrupalparagraphsEntityParagraph;
use DrupalCoreUrl;
/**
* Class DefaultService.
*
* @package Drupalfoo
*/
class XYZ extends Twig_Extension {
/**
* {@inheritdoc}
* This function must return the name of the extension. It must be unique.
*/
public function getName() {
return 'product_listing_extend_display';
}
/**
* In this function we can declare the extension function.
*/
public function getFunctions() {
return array(
'getData' => new Twig_Function_Method( $this, 'getData', array('is_safe' => array('html'))),
);
}
// Function to get tax childs by tid
function getData($id) {
// query for data
// return value
}
}
Call method {{ getData() }} in twig file.
add a comment |
up vote
0
down vote
up vote
0
down vote
@clestcruz, Preprocess is good option for this. But You can create service as well for this.
Create custom module named as foo
.
Create all required files and then create foo.services.yml
.
mention service like :
services:
foo.twig.TwigExtension:
class: DrupalfooXYZ
tags:
- {name: twig.extension}
create service file in foo/src/XYZ.php
<?php
namespace Drupalfoo;
use DrupalblockEntityBlock;
use DrupaluserEntityUser;
use DrupalnodeEntityNode;
use DrupaltaxonomyEntityTerm;
use DrupalparagraphsEntityParagraph;
use DrupalCoreUrl;
/**
* Class DefaultService.
*
* @package Drupalfoo
*/
class XYZ extends Twig_Extension {
/**
* {@inheritdoc}
* This function must return the name of the extension. It must be unique.
*/
public function getName() {
return 'product_listing_extend_display';
}
/**
* In this function we can declare the extension function.
*/
public function getFunctions() {
return array(
'getData' => new Twig_Function_Method( $this, 'getData', array('is_safe' => array('html'))),
);
}
// Function to get tax childs by tid
function getData($id) {
// query for data
// return value
}
}
Call method {{ getData() }} in twig file.
@clestcruz, Preprocess is good option for this. But You can create service as well for this.
Create custom module named as foo
.
Create all required files and then create foo.services.yml
.
mention service like :
services:
foo.twig.TwigExtension:
class: DrupalfooXYZ
tags:
- {name: twig.extension}
create service file in foo/src/XYZ.php
<?php
namespace Drupalfoo;
use DrupalblockEntityBlock;
use DrupaluserEntityUser;
use DrupalnodeEntityNode;
use DrupaltaxonomyEntityTerm;
use DrupalparagraphsEntityParagraph;
use DrupalCoreUrl;
/**
* Class DefaultService.
*
* @package Drupalfoo
*/
class XYZ extends Twig_Extension {
/**
* {@inheritdoc}
* This function must return the name of the extension. It must be unique.
*/
public function getName() {
return 'product_listing_extend_display';
}
/**
* In this function we can declare the extension function.
*/
public function getFunctions() {
return array(
'getData' => new Twig_Function_Method( $this, 'getData', array('is_safe' => array('html'))),
);
}
// Function to get tax childs by tid
function getData($id) {
// query for data
// return value
}
}
Call method {{ getData() }} in twig file.
answered 2 days ago
Sourabh Bhutani
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424617%2ftrying-to-access-values-from-nodes-to-render-in-email-template-using-swift-maile%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How do you pass the node to the swiftmailer. With simplenews or ...? Please try to var_dump variables and check what inside. The preprocess is the right thing to do.
– shaxaaa
Nov 22 at 7:57
@shaxaaa actually haven't figured it out yet to pass the node to the swiftmailer. I'm still a beginner so things like
preprocess
I haven't figured it out yet– clestcruz
Nov 22 at 9:14
1
Is it always the content of one node. I am not sure what you need to develop.
– shaxaaa
Nov 22 at 9:26
@shaxaaa yes, it is the content of one node
– clestcruz
Nov 23 at 4:40