0

Custom twig template into hook_mail()

$body_content = array(
    '#theme' => 'mail_body_content',
    '#node' => $node,
);

I would like to access to node Entity and display value into the twig

myfield_name: {{ node.get('field_myfield_name').value }}

Render code

$renderedContent = \Drupal::service('renderer')->renderPlain($body_content);

Only myfield_name: is display. How to get / pass the variable to twig ?

0

Rather than using node get to print a value just use this in twig.

node.field_myfield_name.value

Otherwise preprocess your twig and get only required fields over sending complete node object to twig.

function theme_name_preprocess_mail_body_content(&$variables) {
  $variables['myfield_name'] = $variables['node']->field_myfield_name->value; //$variables['node'] will be render $node from your hook_mail() function.
}

And in Twig myfield_name will work

0

Passing content (hook_mail):

$body_content = array(
    '#context' => [
        'node' => $node,
    ],
);
$renderedContent = \Drupal::service('renderer')->renderPlain($body_content);
$message['body'][] = $renderedContent;

Get content (twig template file):

myfield_name: {{ context.node.get('field_myfield_name').value }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.