You are here

function redirect_load_entity_from_path in Redirect 7.2

Same name and namespace in other branches
  1. 7 redirect.module \redirect_load_entity_from_path()

Given a path determine if it is an entity default path.

Parameters

$path: The internal path. The id of the entity should be in the string as '[id]'.

Return value

An array with the entity type and the loaded entity object.

File

./redirect.module, line 1268

Code

function redirect_load_entity_from_path($path) {
  $entity_paths =& drupal_static(__FUNCTION__);
  if (!isset($entity_paths)) {
    $entity_paths = array();
    foreach (entity_get_info() as $entity_type => $entity_info) {
      if (isset($entity_info['default path'])) {
        $default_path = $entity_info['default path'];
        $default_path = preg_quote($default_path, '/');
        $default_path = str_replace(preg_quote('%' . $entity_type, '/'), '(\\d+)', $default_path);
        $entity_paths[$entity_type] = $default_path;
      }
    }
  }
  foreach ($entity_paths as $entity_type => $default_path) {
    if (preg_match("/^{$default_path}\$/", $path, $matches)) {
      if ($entity = entity_load($entity_type, array(
        $matches[1],
      ))) {
        return array(
          'entity_type' => $entity_type,
          'entity' => reset($entity),
        );
      }
      break;
    }
  }
}