public function ObfuscateEmail::process in Obfuscate Email 8
Performs the filter processing.
Parameters
string $text: The text string to be filtered.
string $langcode: The language code of the text to be filtered.
Return value
\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.
Overrides FilterInterface::process
See also
\Drupal\filter\FilterProcessResult
File
- src/
Plugin/ Filter/ ObfuscateEmail.php, line 54
Class
- ObfuscateEmail
- Provide a filter to obfuscate mailto anchor tags and optionally replace inner text.
Namespace
Drupal\obfuscate_email\Plugin\FilterCode
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
if (stristr($text, 'mailto') === FALSE) {
return $result;
}
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
/** @var \DOMElement $domElement */
foreach ($xpath
->query('//a[starts-with(@href, "mailto:")]') as $domElement) {
// Read the href attribute value and delete it.
$href = str_replace('mailto:', '', $domElement
->getAttribute('href'));
$domElement
->setAttribute('href', '#');
// Convert to rot13
$mail_string = str_rot13(str_replace([
'.',
'@',
], [
'/dot/',
'/at/',
], $href));
$domElement
->setAttribute('data-mail-to', $mail_string);
// Replace occurrence of the address in the anchor text.
if (strpos($domElement->nodeValue, $href) !== FALSE) {
$domElement->nodeValue = str_replace($href, '@email', $domElement->nodeValue);
$domElement
->setAttribute('data-replace-inner', '@email');
if ($this->settings['click']) {
$domElement
->setAttribute('data-mail-click-link', true);
$domElement->nodeValue = $this
->t($this->settings['click_label']);
$domElement
->setAttribute('data-replace-inner', $domElement->nodeValue);
}
}
}
$result
->setProcessedText(Html::serialize($dom));
return $result;
}