You are here

LookupPath.php in Optimizely 8.0

Same filename and directory in other branches
  1. 8 src/LookupPath.php

Namespace

Drupal\optimizely

File

src/LookupPath.php
View source
<?php

/**
 * @file
 * Contains \Drupal\optimizely\LookupPath
 */
namespace Drupal\optimizely;


/**
 * Implements methods for looking up path aliases and system paths.
 */
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.
   */
  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.
   */
  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
   * because the alias_manager requires it.
   */
  static function checkPath($path) {
    return $path[0] == '/' ? $path : '/' . $path;
  }

}

Traits

Namesort descending Description
LookupPath Implements methods for looking up path aliases and system paths.