public static function MimeMailFormatHelper::mimeMailUrl in Mime Mail 8
Helper function to format URLs.
Parameters
string $url: The file path.
bool $to_embed: (optional) Whether the URL is used to embed the file. Defaults to FALSE.
Return value
string A processed URL.
4 calls to MimeMailFormatHelper::mimeMailUrl()
- MimeMailFormatHelper::mimeMailFile in src/Utility/ MimeMailFormatHelper.php 
- Builds a manifest containing metadata about files attached to an email.
- MimeMailFormatHelper::mimeMailHtmlBody in src/Utility/ MimeMailFormatHelper.php 
- Generates a multipart message body with a plaintext alternative.
- MimeMailFormatHelperTest::testUrl in tests/src/ Kernel/ MimeMailFormatHelperTest.php 
- Tests helper function for formatting URLs.
- MimeMailWebTest::testUrl in tests/src/ Functional/ MimeMailWebTest.php 
- Tests that spaces in attachment filenames are properly URL-encoded.
File
- src/Utility/ MimeMailFormatHelper.php, line 454 
Class
- MimeMailFormatHelper
- Utility methods for formatting MIME-encoded email messages.
Namespace
Drupal\mimemail\UtilityCode
public static function mimeMailUrl($url, $to_embed = FALSE) {
  /** @var \Drupal\Core\Config\ImmutableConfig $mimemail_config */
  $mimemail_config = \Drupal::config('mimemail.settings');
  /** @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */
  $language_manager = \Drupal::languageManager();
  $url = urldecode($url);
  $to_link = $mimemail_config
    ->get('linkonly');
  $is_image = preg_match('!\\.(png|gif|jpg|jpeg)!i', $url);
  $is_absolute = StreamWrapperManager::getScheme($url) != FALSE || preg_match('!(mailto|callto|tel)\\:!', $url);
  // Strip the base path as Uri adds it again at the end.
  $base_path = rtrim(base_path(), '/');
  $url = preg_replace('!^' . $base_path . '!', '', $url, 1);
  if (!$to_embed) {
    if ($is_absolute) {
      return str_replace(' ', '%20', $url);
    }
  }
  else {
    if ($is_image) {
      if ($to_link) {
        // Exclude images from embedding if needed.
        $url = file_create_url($url);
        $url = str_replace(' ', '%20', $url);
      }
      else {
        // Remove security token from URL, this allows for styled image
        // embedding.
        // @see https://drupal.org/drupal-7.20-release-notes
        $url = preg_replace('/\\?itok=.*$/', '', $url);
      }
    }
    return $url;
  }
  $url = str_replace('?q=', '', $url);
  @(list($url, $fragment) = explode('#', $url, 2));
  @(list($path, $query) = explode('?', $url, 2));
  // If we're dealing with an intra-document reference, return it.
  if (empty($path)) {
    return '#' . $fragment;
  }
  // Get a list of enabled languages.
  $languages = $language_manager
    ->getLanguages(LanguageInterface::STATE_ALL);
  // Default language settings.
  $prefix = '';
  $language = $language_manager
    ->getDefaultLanguage();
  // Check for language prefix.
  $args = explode('/', $path);
  foreach ($languages as $lang) {
    if ($args[1] == $lang
      ->getId()) {
      $prefix = array_shift($args);
      $language = $lang;
      $path = implode('/', $args);
      break;
    }
  }
  parse_str($query, $arr);
  $options = [
    'query' => !empty($arr) ? $arr : [],
    'fragment' => $fragment,
    'absolute' => TRUE,
    'language' => $language,
    'prefix' => $prefix,
  ];
  $url = Url::fromUserInput($path, $options)
    ->toString();
  // If url() added a ?q= where there should not be one, remove it.
  if (preg_match('!^\\?q=*!', $url)) {
    $url = preg_replace('!\\?q=!', '', $url);
  }
  $url = str_replace('+', '%2B', $url);
  return $url;
}