代码之家  ›  专栏  ›  技术社区  ›  clestcruz

尝试使用swift mailer访问要在电子邮件模板中呈现的节点值

  •  2
  • clestcruz  · 技术社区  · 7 年前

    swift mailer

    enter image description here

    enter image description here

    enter image description here

    Swift mailer有一个默认的方法,可以使用 {{ body }}

    有没有方法可以访问字段中的值并在模板上呈现它们。类似于下面在节点模板上工作的代码

    {{content.field_title.value}} {{node.field_title.value}}

    Swift mailer有一种通过预处理来呈现值的方法,但我似乎无法让它工作。下面是正在进行的工作的代码

    https://www.drupal.org/node/1590184

    function swiftmailer_preprocess_swiftmailer(&$variables) {
        $variables['node_title'] = $node->getTitle();
    }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   shaxaaa    7 年前
    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();
    }
    

    在你做了这些之后swiftmailer.html.twig网站您将能够像这样呈现节点标题{{node\u title}

        2
  •  0
  •   Sourabh Bhutani    7 年前

    创建名为的自定义模块 foo . 创建所有必需的文件,然后创建 foo.services.yml 提到服务,比如:

    services:
      foo.twig.TwigExtension:
        class: Drupal\foo\XYZ
        tags:
          - {name: twig.extension}
    

    在中创建服务文件 foo/src/XYZ.php

    <?php
    namespace Drupal\foo;
    use Drupal\block\Entity\Block;
    use Drupal\user\Entity\User;
    use Drupal\node\Entity\Node;
    use Drupal\taxonomy\Entity\Term;
    use Drupal\paragraphs\Entity\Paragraph;
    use Drupal\Core\Url;
    
    /**
     * Class DefaultService.
     *
     * @package Drupal\foo
     */
    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
      }
    
    }
    

    推荐文章