getPathByAlias example


  protected function assertUrlInboundAlter(string $original, string $final): void {
    // Test inbound altering.     $result = $this->container->get('path_alias.manager')->getPathByAlias($original);
    $this->assertSame($final$resultnew FormattableMarkup('Altered inbound URL %original, expected %final, and got %result.', ['%original' => $original, '%final' => $final, '%result' => $result]));
  }

}

  protected function assertUrlInboundAlter(string $original, string $final): void {
    // Test inbound altering.     $result = $this->container->get('path_alias.manager')->getPathByAlias($original);
    $this->assertSame($final$resultnew FormattableMarkup('Altered inbound URL %original, expected %final, and got %result.', ['%original' => $original, '%final' => $final, '%result' => $result]));
  }

}
$this->languageManager->expects($this->any())
      ->method('getCurrentLanguage')
      ->with(LanguageInterface::TYPE_URL)
      ->willReturn($language);

    $this->aliasRepository->expects($this->once())
      ->method('lookupByAlias')
      ->with($alias$language->getId())
      ->willReturn(NULL);

    $this->assertEquals($alias$this->aliasManager->getPathByAlias($alias));
    // Call it twice to test the static cache.     $this->assertEquals($alias$this->aliasManager->getPathByAlias($alias));
  }

  /** * Tests the getPathByAlias method for an alias that have a matching path. * * @covers ::getPathByAlias */
  public function testGetPathByAliasMatch() {
    $alias = $this->randomMachineName();
    
'#description' => $this->t('This page is displayed when no other content matches the requested document. Leave blank to display a generic "page not found" page.'),
    ];

    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Get the normal path of the front page.     $form_state->setValueForElement($form['front_page']['site_frontpage']$this->aliasManager->getPathByAlias($form_state->getValue('site_frontpage')));
    // Validate front page path.     if (($value = $form_state->getValue('site_frontpage')) && $value[0] !== '/') {
      $form_state->setErrorByName('site_frontpage', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_frontpage')]));

    }
    if (!$this->pathValidator->isValid($form_state->getValue('site_frontpage'))) {
      $form_state->setErrorByName('site_frontpage', $this->t("Either the path '%path' is invalid or you do not have access to it.", ['%path' => $form_state->getValue('site_frontpage')]));
    }
    // Get the normal paths of both error pages.     if (!$form_state->isValueEmpty('site_403')) {
      $form_state->setValueForElement($form['error_page']['site_403']$this->aliasManager->getPathByAlias($form_state->getValue('site_403')));
    }
// Check that the French alias is not available. We check the unprefixed     // alias because we disabled URL language negotiation above. In this     // situation only aliases in the default language and language neutral ones     // should keep working.     $this->drupalGet($french_alias);
    $this->assertSession()->statusCodeEquals(404);

    // The alias manager has an internal path lookup cache. Check to see that     // it has the appropriate contents at this point.     $this->container->get('path_alias.manager')->cacheClear();
    $french_node_path = $this->container->get('path_alias.manager')->getPathByAlias('/' . $french_alias, 'fr');
    $this->assertEquals('/node/' . $english_node_french_translation->id()$french_node_path, 'Normal path works.');
    // Second call should return the same path.     $french_node_path = $this->container->get('path_alias.manager')->getPathByAlias('/' . $french_alias, 'fr');
    $this->assertEquals('/node/' . $english_node_french_translation->id()$french_node_path, 'Normal path is the same.');

    // Confirm that the alias works.     $french_node_alias = $this->container->get('path_alias.manager')->getAliasByPath('/node/' . $english_node_french_translation->id(), 'fr');
    $this->assertEquals('/' . $french_alias$french_node_alias, 'Alias works.');
    // Second call should return the same alias.     $french_node_alias = $this->container->get('path_alias.manager')->getAliasByPath('/node/' . $english_node_french_translation->id(), 'fr');
    $this->assertEquals('/' . $french_alias$french_node_alias, 'Alias is the same.');

    

  public function testLookupPath() {
    // Create AliasManager and Path object.     $aliasManager = $this->container->get('path_alias.manager');

    // Test the situation where the source is the same for multiple aliases.     // Start with a language-neutral alias, which we will override.     $path_alias = $this->createPathAlias('/user/1', '/foo');
    $this->assertEquals($path_alias->getAlias()$aliasManager->getAliasByPath($path_alias->getPath()), 'Basic alias lookup works.');
    $this->assertEquals($path_alias->getPath()$aliasManager->getPathByAlias($path_alias->getAlias()), 'Basic source lookup works.');

    // Create a language specific alias for the default language (English).     $path_alias = $this->createPathAlias('/user/1', '/users/Dries', 'en');

    $this->assertEquals($path_alias->getAlias()$aliasManager->getAliasByPath($path_alias->getPath()), 'English alias overrides language-neutral alias.');
    $this->assertEquals($path_alias->getPath()$aliasManager->getPathByAlias($path_alias->getAlias()), 'English source overrides language-neutral source.');

    // Create a language-neutral alias for the same path, again.     $path_alias = $this->createPathAlias('/user/1', '/bar');
    $this->assertEquals("/users/Dries", $aliasManager->getAliasByPath($path_alias->getPath()), 'English alias still returned after entering a language-neutral alias.');

    

  public function __construct(AliasManagerInterface $alias_manager) {
    $this->aliasManager = $alias_manager;
  }

  /** * {@inheritdoc} */
  public function processInbound($path, Request $request) {
    $path = $this->aliasManager->getPathByAlias($path);
    return $path;
  }

  /** * {@inheritdoc} */
  public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
    if (empty($options['alias'])) {
      $langcode = isset($options['language']) ? $options['language']->getId() : NULL;
      $path = $this->aliasManager->getAliasByPath($path$langcode);
      // Ensure the resulting path has at most one leading slash, to prevent it
Home | Imprint | This part of the site doesn't use cookies.