Co-Authors Token

Created on 30 April 2024, 7 months ago

Problem/Motivation

We have a webform for page feedback that will send an email to the authors of the attached page if somebody fills it out. It uses the [webform-submission:source-entity:author:mail] token in the configuration for that form, to be able to fill in the email of the author of that page.

What we would like is to be able to have this form notification go to the primary author AND all the co-authors through a similar token.

Steps to reproduce

Create a webform and in the Email/Handlers section, set up an email. In our context, the desired token would go in the To field with a custom To email address.

Proposed resolution

I think we can probably get to our end goal of having co-authors also receive the email with a hook intercepting when it starts to send out, but a token seems like it would be cleaner, easier to maintain, and potentially useful for others.

Feature request
Status

Active

Version

1.1

Component

Code

Created by

🇨🇦Canada ryanrobinson_wlu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @ryanrobinson_wlu
  • 🇧🇪Belgium dieterholvoet Brussels

    You could send the mail to the author and CC to the co-authors. According to the CC field description 'multiple email addresses may be separated by commas'.

    I would have expected a token like [webform_submission:source-entity:co_authors] to already exist automatically, but seems like it doesn't. We should probably look into why the Token module isn't providing a token for that field.

  • 🇨🇦Canada ryanrobinson_wlu

    I've made a custom module that adds a new token like below. You're welcome to use/improve this if you think it would be helpful to others. If you decide to incorporate something like this into the main module, I'd be happy to phase out mine.

    From the coauthor_tokens.module file:

    <?php
    
    /**
     * @file
     * Token functions extending node_co_author.
     */
    
    use Drupal\Core\Render\BubbleableMetadata;
    use Drupal\user\Entity\User;
    
    /**
     * Implements hook_token_info().
     *
     * Defines tokens.
     */
    function coauthor_tokens_token_info() {
      $info = [];
      $info['tokens']['node']['coauthors_email'] = [
        'name' => t("Coauthors' emails."),
        'description' => t("The coauthors' email addresses, separated by commas."),
      ];
    
      return $info;
    }
    
    /**
     * Implements hook_tokens().
     *
     * Defines values for tokens.
     */
    function coauthor_tokens_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
      $replacements = [];
      foreach ($tokens as $name => $original) {
        if ($type == "node") {
          switch ($name) {
            case 'coauthors_email':
              $node = $data['node'];
              if ($node) {
                $coauthor_emails = [];
                $coauthors = $node->get('co_authors')->getValue();
                foreach ($coauthors as $coauthor) {
                  $coauthor_emails[] = User::load($coauthor['target_id'])->getEmail();
                }
                $replacements[$original] = implode(',', $coauthor_emails);
              }
              break;
          }
        }
      }
      return $replacements;
    }
    
    
  • 🇧🇪Belgium dieterholvoet Brussels

    It seems like it's not yet possible to automatically provide all user-related tokens for an array of co-authors (see Need to figure out how to create nested tokens from the array token type Needs review ), so we'll need to define individual tokens per field as @ryanrobinson_wlu does in his example. I'll start a MR.

  • 🇧🇪Belgium dieterholvoet Brussels
  • 🇧🇪Belgium dieterholvoet Brussels

    I added a new [node:co_authors_email] token to the MR. To get the email addresses as a comma-separated list, you can use [node:co_authors_email:join]. Can someone test the MR?

  • Merge request !13Add co_authors_email token → (Open) created by dieterholvoet
Production build 0.71.5 2024