You are here

public function ShortcodeBase::getUrlFromPath in Shortcode 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/ShortcodeBase.php \Drupal\shortcode\Plugin\ShortcodeBase::getUrlFromPath()

Returns a url to be used in a link element given path or url.

If a path is supplied, an absolute url will be returned.

Parameters

string $path: The internal path to be translated.

bool $media_file_url: TRUE If a media path is supplied, return the file url.

2 calls to ShortcodeBase::getUrlFromPath()
ButtonShortcode::process in shortcode_basic_tags/src/Plugin/Shortcode/ButtonShortcode.php
Performs the shortcode processing.
LinkShortcode::process in shortcode_basic_tags/src/Plugin/Shortcode/LinkShortcode.php
Performs the shortcode processing.

File

src/Plugin/ShortcodeBase.php, line 224

Class

ShortcodeBase
Provides a base class for Shortcode plugins.

Namespace

Drupal\shortcode\Plugin

Code

public function getUrlFromPath($path, $media_file_url = FALSE) {
  if ($path === '<front>') {
    $path = '/';
  }

  // Path validator. Return the path if an absolute url is detected.
  if (UrlHelper::isValid($path, TRUE)) {
    return $path;
  }

  // Add a leading slash if not present.
  $path = '/' . ltrim($path, '/');
  if (!empty($media_file_url) && substr($path, 0, 6) === "/media") {
    $mid = $this
      ->getMidFromPath($path);
    if ($mid) {
      return $this
        ->getMediaFileUrl($mid);
    }
  }
  else {

    /** @var \Drupal\Core\Path\AliasManager $alias_manager */
    $alias_manager = \Drupal::service('path.alias_manager');
    $alias = $alias_manager
      ->getAliasByPath($path);
  }

  // Convert relative URL to absolute.
  $url = Url::fromUserInput($alias, [
    'absolute' => TRUE,
  ])
    ->toString();
  return $url;
}