getAliasByPath example

/** * @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');

    // 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 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       // becoming an external URL without a protocol like //example.com. This       // is done in \Drupal\Core\Routing\UrlGenerator::generateFromRoute()       // also, to protect against this problem in arbitrary path processors,       // but it is duplicated here to protect any other URL generation code       // that might call this method separately.       if (str_starts_with($path, '//')) {
        $path = '/' . ltrim($path, '/');
      }
    }
    return $path;
  }
'#description' => $this->t('Use path alias instead of internal path.'),
    ];
  }

  /** * {@inheritdoc} */
  public function getArgument() {
    // Don't trim the leading slash since getAliasByPath() requires it.     $path = rtrim($this->currentPath->getPath($this->view->getRequest()), '/');
    if ($this->options['use_alias']) {
      $path = $this->aliasManager->getAliasByPath($path);
    }
    $args = explode('/', $path);
    // Drop the empty first element created by the leading slash since the path     // component index doesn't take it into account.     array_shift($args);
    if (isset($args[$this->options['index']])) {
      return $args[$this->options['index']];
    }
  }

  /** * {@inheritdoc} */
'#url' => $url,
    ];

    if ($this->languageManager->isMultilingual()) {
      $row['data']['language_name'] = $this->languageManager->getLanguageName($langcode);
    }

    $row['data']['operations']['data'] = $this->buildOperations($entity);

    // If the system path maps to a different URL alias, highlight this table     // row to let the user know of old aliases.     if ($alias != $this->aliasManager->getAliasByPath($path$langcode)) {
      $row['class'] = ['warning'];
    }

    return $row;
  }

}
'#description' => $this->t("The <em>From</em> address in automated emails sent during registration and new password requests, and other notifications. (Use an address ending in your site's domain to help prevent this email being flagged as spam.)"),
      '#required' => TRUE,
    ];
    $form['front_page'] = [
      '#type' => 'details',
      '#title' => $this->t('Front page'),
      '#open' => TRUE,
    ];
    $form['front_page']['site_frontpage'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Default front page'),
      '#default_value' => $this->aliasManager->getAliasByPath($site_config->get('page.front')),
      '#required' => TRUE,
      '#size' => 40,
      '#description' => $this->t('Specify a relative URL to display as the front page.'),
      '#field_prefix' => $this->requestContext->getCompleteBaseUrl(),
    ];
    $form['error_page'] = [
      '#type' => 'details',
      '#title' => $this->t('Error pages'),
      '#open' => TRUE,
    ];
    $form['error_page']['site_403'] = [
      
// 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.');

    // Confirm that the alias is removed if the translation is deleted.     $english_node->removeTranslation('fr');
    $english_node->save();
    $this->assertPathAliasNotExists('/' . $french_alias, 'fr', NULL, 'Alias for French translation is removed when translation is deleted.');

    // Check that the English alias still works.
    // with different case. Ex: /Page, /page, /PAGE.     $pages = mb_strtolower($this->configuration['pages']);
    if (!$pages) {
      return TRUE;
    }

    $request = $this->requestStack->getCurrentRequest();
    // Compare the lowercase path alias (if any) and internal path.     $path = $this->currentPath->getPath($request);
    // Do not trim a trailing slash if that is the complete path.     $path = $path === '/' ? $path : rtrim($path, '/');
    $path_alias = mb_strtolower($this->aliasManager->getAliasByPath($path));

    return $this->pathMatcher->matchPath($path_alias$pages) || (($path != $path_alias) && $this->pathMatcher->matchPath($path$pages));
  }

  /** * {@inheritdoc} */
  public function getCacheContexts() {
    $contexts = parent::getCacheContexts();
    $contexts[] = 'url.path';
    return $contexts;
  }
$this->assertSession()->pageTextContains($node->label());

    // Confirm custom language path alias works.     $this->drupalGet($prefix . '/' . $custom_language_path);
    $this->assertSession()->pageTextContains($node->label());

    // Create a custom path.     $custom_path = $this->randomMachineName(8);

    // Check priority of language for alias by source path.     $path_alias = $this->createPathAlias('/node/' . $node->id(), '/' . $custom_path, LanguageInterface::LANGCODE_NOT_SPECIFIED);
    $lookup_path = $this->container->get('path_alias.manager')->getAliasByPath('/node/' . $node->id(), 'en');
    $this->assertEquals('/' . $english_path$lookup_path, 'English language alias has priority.');
    // Same check for language 'xx'.     $lookup_path = $this->container->get('path_alias.manager')->getAliasByPath('/node/' . $node->id()$prefix);
    $this->assertEquals('/' . $custom_language_path$lookup_path, 'Custom language alias has priority.');
    $path_alias->delete();

    // Create language nodes to check priority of aliases.     $first_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => 'en']);
    $second_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);

    // Assign a custom path alias to the first node with the English language.
$this->aliasWhitelist->expects($this->any())
      ->method('get')
      ->with($path_part1)
      ->willReturn(FALSE);

    // The whitelist returns FALSE for that path part, so the storage should     // never be called.     $this->aliasRepository->expects($this->never())
      ->method('lookupBySystemPath');

    $this->assertEquals($path$this->aliasManager->getAliasByPath($path));
  }

  /** * Tests the getAliasByPath method for a path that has no matching alias. * * @covers ::getAliasByPath */
  public function testGetAliasByPathNoMatch() {
    $path_part1 = $this->randomMachineName();
    $path_part2 = $this->randomMachineName();
    $path = '/' . $path_part1 . '/' . $path_part2;

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