‪TYPO3CMS  ‪main
EmailViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
28 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
29 
75 final class ‪EmailViewHelper extends AbstractTagBasedViewHelper
76 {
80  protected ‪$tagName = 'a';
81 
82  public function ‪initializeArguments(): void
83  {
84  parent::initializeArguments();
85  $this->registerArgument('email', 'string', 'The email address to be turned into a link', true);
86  $this->registerArgument('cc', 'string', 'The email address(es) for CC of the email link');
87  $this->registerArgument('bcc', 'string', 'The email address(es) for BCC of the email link');
88  $this->registerArgument('subject', 'string', 'A prefilled subject for the email link');
89  $this->registerArgument('body', 'string', 'A prefilled body for the email link');
90  $this->registerUniversalTagAttributes();
91  $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
92  $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
93  $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
94  $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
95  }
96 
97  public function ‪render(): string
98  {
99  $email = $this->arguments['email'];
100  $linkHref = GeneralUtility::makeInstance(EmailLinkHandler::class)->asString($this->arguments);
101  $attributes = [];
102  $linkText = htmlspecialchars($email);
104  $renderingContext = $this->renderingContext;
105  $request = $renderingContext->getRequest();
106  if ($request instanceof ServerRequestInterface && ‪ApplicationType::fromRequest($request)->isFrontend()) {
107  // If there is no request, backend is assumed.
109  $frontend = ‪$GLOBALS['TSFE'];
110  // passing HTML encoded link text
111  try {
112  $linkResult = GeneralUtility::makeInstance(LinkFactory::class)->create($linkText, ['parameter' => $linkHref], $frontend->cObj);
113  $linkText = (string)$linkResult->getLinkText();
114  $attributes = $linkResult->getAttributes();
115  } catch (‪UnableToLinkException $e) {
116  // Just render the email as is (= Backend Context), if LinkBuilder failed
117  }
118  }
119  $tagContent = $this->renderChildren();
120  if ($tagContent !== null) {
121  $linkText = $tagContent;
122  }
123  $this->tag->setContent($linkText);
124  $this->tag->addAttribute('href', $linkHref);
125  $this->tag->forceClosingTag(true);
126  $this->tag->addAttributes($attributes);
127  return $this->tag->render();
128  }
129 }
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:58
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55