I wont get into how to make custom tokens in drupal 8, look here for that info
Originally you would just put some kind of value like this and have it as plain string
$replacements[$original] = $active_profile->get('field_org_title')->value;
But here we are going to have a more complex output, having html markup in it and outputted properlly with calling
case 'recipient-data': $user_id = $activity->get('field_activity_recipient_user')->target_id; $user = \Drupal::entityTypeManager()->getStorage("user")->load($user_id); $active_profile = \Drupal::entityTypeManager()->getStorage('profile')->loadByUser($user, "organisation_profile"); // Designate the field we want to render. $field_name = 'field_org_address'; $display = [ 'label' => 'hidden', 'type' => 'field_address', ]; // Retrieve a render array for that field with the given view mode. $build = $active_profile->$field_name->view($display); // Render the result. $address = \Drupal::service('renderer')->renderRoot($build); $replacements[$original] = \Drupal\Core\Render\Markup::create($active_profile->get('field_org_title')->value . $address); break;
We are setting here token for "recipient-data" (nevermind what is that, its just some custom token I needed). We load used_id then load profile associated with that id and then render a specific field and in the end use \Drupal\Core\Render\Markup::create to add that data to token replacement.