You are here

trait LookupPath in Optimizely 8

Same name and namespace in other branches
  1. 8.0 src/LookupPath.php \Drupal\optimizely\LookupPath

Implements methods for looking up path aliases and system paths.

Hierarchy

File

src/LookupPath.php, line 8

Namespace

Drupal\optimizely
View source
trait LookupPath {

  /**
   * Helper function to lookup a path alias, given a path.
   *
   * This function acts as an adapter and passes back a return value
   * like those of drupal_lookup_path(), which has been removed
   * as of Drupal 8.
   */
  public static function lookupPathAlias($path) {
    $path = LookupPath::checkPath($path);
    $alias = \Drupal::service('path.alias_manager')
      ->getAliasByPath($path);
    return strcmp($alias, $path) == 0 ? FALSE : $alias;
  }

  /**
   * Helper function to lookup a system path, given a path alias.
   *
   * This function acts as an adapter and passes back a return value
   * like those of drupal_lookup_path(), which has been removed
   * as of Drupal 8.
   */
  public static function lookupSystemPath($alias) {
    $alias = LookupPath::checkPath($alias);
    $path = \Drupal::service('path.alias_manager')
      ->getPathByAlias($alias);
    return strcmp($path, $alias) == 0 ? FALSE : $path;
  }

  /**
   * Ensure that $path starts with a forward slash.
   *
   * The alias_manager requires it.
   */
  public static function checkPath($path) {
    return $path[0] == '/' ? $path : '/' . $path;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LookupPath::checkPath public static function Ensure that $path starts with a forward slash.
LookupPath::lookupPathAlias public static function Helper function to lookup a path alias, given a path.
LookupPath::lookupSystemPath public static function Helper function to lookup a system path, given a path alias.