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.



enter image description here



enter image description here



enter image description here



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();
}









share|improve this question

















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 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















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.



enter image description here



enter image description here



enter image description here



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();
}









share|improve this question

















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 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













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.



enter image description here



enter image description here



enter image description here



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();
}









share|improve this question















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.



enter image description here



enter image description here



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












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 }}






share|improve this answer




























    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.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      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

























      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 }}






      share|improve this answer

























        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 }}






        share|improve this answer























          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 }}






          share|improve this answer












          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 }}







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 9:00









          shaxaaa

          10918




          10918
























              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.






              share|improve this answer

























                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.






                share|improve this answer























                  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.






                  share|improve this answer












                  @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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  Sourabh Bhutani

                  12




                  12






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      How to ignore python UserWarning in pytest?

                      Alexandru Averescu