You are here

forward.module in Forward 4.0.x

Allows forwarding of entities by email.

File

forward.module
View source
<?php

/**
 * @file
 * Allows forwarding of entities by email.
 */
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function forward_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.forward':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Adds a "forward this page" link to pages.') . '</p>';
      $output .= '<p>' . t('This module allows users to email a page on your site to a friend.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_page_attachments().
 */
function forward_page_attachments(array &$page) {
  if (\Drupal::routeMatch()
    ->getRouteName() == 'forward.form') {
    $settings = \Drupal::config('forward.settings')
      ->get();

    // Tell SEO to ignore this page on request.
    if ($settings['forward_form_noindex']) {
      $element = [
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => [
          'name' => 'robots',
          'content' => 'noindex, nofollow',
        ],
      ];
      $page['#attached']['html_head'][] = [
        $element,
        'forward_meta_noindex',
      ];
    }
  }
}

/**
 * Implements hook_theme().
 */
function forward_theme($existing, $type, $theme, $path) {
  return [
    'forward' => [
      'variables' => [
        'email' => NULL,
        'recipient' => NULL,
        'header' => NULL,
        'footer' => NULL,
        'message' => NULL,
        'settings' => NULL,
        'entity' => NULL,
        'content' => NULL,
        'view_mode' => NULL,
        'email_format' => NULL,
      ],
    ],
    'forward_link_formatter' => [
      'variables' => [
        'link' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function forward_theme_suggestions_forward(array $variables) {
  $suggestions = [];
  $suggestions[] = 'forward__' . $variables['email_format'];
  $suggestions[] = 'forward__' . $variables['email_format'] . '__' . $variables['view_mode'];
  return $suggestions;
}

/**
 * Implements template_preprocess_module().
 */
function template_preprocess_forward(&$variables) {

  // Site info.
  $url = 'internal:' . \Drupal::config('system.site')
    ->get('page.front');
  $variables['site_url'] = Url::fromUri($url, [
    'absolute' => TRUE,
  ]);
  $variables['site_name'] = \Drupal::config('system.site')
    ->get('name');

  // Read more link. Use a custom template to remove.
  $variables['link'] = '';
  $entity = $variables['entity'];
  if ($entity && $entity
    ->access('view')) {
    $variables['link'] = t('<a href="@entity-url">Read more about @entity-name</a>', [
      '@entity-url' => $entity
        ->toUrl('canonical', [
        'absolute' => TRUE,
      ])
        ->toString(),
      '@entity-name' => $entity
        ->label(),
    ]);
  }

  // Logo.
  if (!empty($variables['settings']['forward_email_logo'])) {
    $url = 'base:' . $variables['settings']['forward_email_logo'];
    $url = Url::fromUri($url, [
      'absolute' => TRUE,
    ])
      ->toString();
  }
  else {
    $url = theme_get_setting('logo.url');
    if (!UrlHelper::isExternal($url)) {
      $url = Url::fromUri('base:/' . $url, [
        'absolute' => TRUE,
      ])
        ->toString();
    }
  }
  $variables['logo'] = [
    '#markup' => '<img src="' . $url . '" />',
  ];
}

/**
 * Implements hook_mail().
 */
function forward_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
  $message['body'][] = $params['body'];
}

/**
 * Implements hook_mail_alter().
 */
function forward_mail_alter(&$message) {
  if ($message['module'] == 'forward') {
    if ($message['key'] == 'mail_entity' || $message['key'] == 'send_entity') {
      if (!empty($message['params']['plain_text'])) {
        $message['headers']['Content-Type'] = 'text/plain; charset=utf-8';
      }
      else {
        $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
      }
      $message['headers']['MIME-Version'] = '1.0';
      $message['headers']['Reply-to'] = $message['params']['headers']['Reply-To'];
      $message['headers']['Sender'] = $message['headers']['From'];
    }
  }
}

Functions