lookupByAlias example

$loaded_node->get('path')->alias = '/bar';
    $this->assertFalse($loaded_node->get('path')->isEmpty());
    $this->assertEquals('/bar', $loaded_node->get('path')->alias);

    $loaded_node->save();
    $this->assertFalse($loaded_node->get('path')->isEmpty());
    $this->assertEquals('/bar', $loaded_node->get('path')->alias);

    $stored_alias = $alias_repository->lookupBySystemPath('/' . $node->toUrl()->getInternalPath()$node->language()->getId());
    $this->assertEquals('/bar', $stored_alias['alias']);

    $old_alias = $alias_repository->lookupByAlias('/foo', $node->language()->getId());
    $this->assertNull($old_alias);

    // Reload the node to make sure that it is possible to set a value     // immediately after loading.     $node_storage->resetCache();
    $loaded_node = $node_storage->load($node->id());
    $loaded_node->get('path')->alias = '/foobar';
    $loaded_node->save();

    $node_storage->resetCache();
    $loaded_node = $node_storage->load($node->id());
    
// If we already know that there are no paths for this alias simply return.     if (empty($alias) || !empty($this->noPath[$langcode][$alias])) {
      return $alias;
    }

    // Look for the alias within the cached map.     if (isset($this->lookupMap[$langcode]) && ($path = array_search($alias$this->lookupMap[$langcode]))) {
      return $path;
    }

    // Look for path in storage.     if ($path_alias = $this->pathAliasRepository->lookupByAlias($alias$langcode)) {
      $this->lookupMap[$langcode][$path_alias['path']] = $alias;
      return $path_alias['path'];
    }

    // We can't record anything into $this->lookupMap because we didn't find any     // paths for this alias. Thus cache to $this->noPath.     $this->noPath[$langcode][$alias] = TRUE;

    return $alias;
  }

  
$this->assertEquals('/test-alias', $path_alias_repository->lookupBySystemPath('/test-source-Case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['alias']);
    $this->assertEquals('/test-alias', $path_alias_repository->lookupBySystemPath('/test-source-case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['alias']);
  }

  /** * @covers ::lookupByAlias */
  public function testLookupByAlias() {
    $this->createPathAlias('/test-source', '/test-alias-Case');

    $path_alias_repository = $this->container->get('path_alias.repository');
    $this->assertEquals('/test-source', $path_alias_repository->lookupByAlias('/test-alias-Case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['path']);
    $this->assertEquals('/test-source', $path_alias_repository->lookupByAlias('/test-alias-case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['path']);
  }

  /** * @covers \Drupal\path_alias\AliasManager::getPathByAlias * @covers \Drupal\path_alias\AliasManager::getAliasByPath */
  public function testLookupPath() {
    // Create AliasManager and Path object.     $aliasManager = $this->container->get('path_alias.manager');

    
Home | Imprint | This part of the site doesn't use cookies.