You are here

protected function SpamspanTrait::output in SpamSpan filter 8.2

Same name and namespace in other branches
  1. 8 src/SpamspanTrait.php \Drupal\spamspan\SpamspanTrait::output()

A helper function for the callbacks.

Replace an email address which has been found with the appropriate <span> tags.

Parameters

string $name: The user name.

string $domain: The email domain.

string $contents: The contents of any <a> tag.

array $headers: The email headers extracted from a mailto: URL.

array $vars: Optional parameters. Used only when spamspan_use_form = true.

Return value

string The span with which to replace the email address.

3 calls to SpamspanTrait::output()
SpamspanTrait::callbackBareEmailAddresses in src/SpamspanTrait.php
Callback function for preg_replace_callback.
SpamspanTrait::callbackEmailAddressesWithOptions in src/SpamspanTrait.php
Callback function for preg_replace_callback.
SpamspanTrait::callbackMailto in src/SpamspanTrait.php
Callback function for preg_replace_callback.

File

src/SpamspanTrait.php, line 207

Class

SpamspanTrait
Trait SpamspanTrait.

Namespace

Drupal\spamspan

Code

protected function output($name, $domain, $contents = '', array $headers = [], array $vars = []) {

  // Processing for forms.
  if (!empty($this->settings['spamspan_use_form'])) {
    return $this
      ->outputWhenUseForm($name, $domain, $contents, $headers, $vars);
  }
  $at = $this->settings['spamspan_at'];
  if ($this->settings['spamspan_use_graphic']) {
    $render_at = [
      '#theme' => 'spamspan_at_sign',
      '#settings' => $this->settings,
    ];

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $at = $renderer
      ->renderPlain($render_at);
  }
  if ($this->settings['spamspan_dot_enable']) {

    // Replace .'s in the address with [dot].
    $name = str_replace('.', '<span class="o">' . $this->settings['spamspan_dot'] . '</span>', $name);
    $domain = str_replace('.', '<span class="o">' . $this->settings['spamspan_dot'] . '</span>', $domain);
  }
  $output = '<span class="u">' . $name . '</span>' . $at . '<span class="d">' . $domain . '</span>';

  // Ff there are headers, include them as eg (subject: xxx, cc: zzz).
  // Replace the '=' in the headers with ': ', so it looks nicer.
  if (count($headers)) {
    foreach ($headers as $key => $header) {

      // Url encode header.
      $header = rawurlencode(rawurldecode($header));

      // Replace the first '=' sign.
      $header = preg_replace('/%3D/', ': ', $header, 1);
      $headers[$key] = $header;
    }
    $output .= '<span class="h"> (' . Html::escape(implode(', ', $headers)) . ') </span>';
  }
  $contents = $this
    ->filterTagContents($contents);

  // If there are tag contents, include them between round brackets.
  if (!empty($contents)) {
    $output .= '<span class="t"> (' . $contents . ')</span>';
  }

  // Put in the extra <a> attributes.
  if (!empty($vars['extra_attributes'])) {
    $output .= '<span class="e">' . strip_tags($vars['extra_attributes']) . '</span>';
  }
  $output = '<span class="spamspan">' . $output . '</span>';
  return $output;
}