fromRouteMatch example

/** * {@inheritdoc} */
  public function isFrontPage() {
    // Cache the result as this is called often.     if (!isset($this->isCurrentFrontPage)) {
      $this->isCurrentFrontPage = FALSE;
      // Ensure that the code can also be executed when there is no active       // route match, like on exception responses.       if ($this->routeMatch->getRouteName()) {
        $url = Url::fromRouteMatch($this->routeMatch);
        $this->isCurrentFrontPage = ($url->getRouteName() && '/' . $url->getInternalPath() === $this->getFrontPagePath());
      }
    }
    return $this->isCurrentFrontPage;
  }

  /** * Gets the current front page path. * * @return string * The front page path. */
$access = $this->accessManager->check($route_match$this->currentUser, NULL, TRUE);
        // The set of breadcrumb links depends on the access result, so merge         // the access result's cacheability metadata.         $breadcrumb = $breadcrumb->addCacheableDependency($access);
        if ($access->isAllowed()) {
          $title = $this->titleResolver->getTitle($route_request$route_match->getRouteObject());
          if (!isset($title)) {
            // Fallback to using the raw path component as the title if the             // route is missing a _title or _title_callback attribute.             $title = str_replace(['-', '_'], ' ', Unicode::ucfirst(end($path_elements)));
          }
          $url = Url::fromRouteMatch($route_match);
          $links[] = new Link($title$url);
        }
      }
    }

    // Add the Home link.     $links[] = Link::createFromRoute($this->t('Home'), '<front>');

    return $breadcrumb->setLinks(array_reverse($links));
  }

  


  /** * {@inheritdoc} */
  public function build() {
    $build = [];
    $type = $this->getDerivativeId();
    $route_match = \Drupal::routeMatch();
    // If there is no route match, for example when creating blocks on 404 pages     // for logged-in users with big_pipe enabled using the front page instead.     $url = $route_match->getRouteObject() ? Url::fromRouteMatch($route_match) : Url::fromRoute('<front>');
    $links = $this->languageManager->getLanguageSwitchLinks($type$url);

    if (isset($links->links)) {
      $build = [
        '#theme' => 'links__language_block',
        '#links' => $links->links,
        '#attributes' => [
          'class' => [
            "language-switcher-{$links->method_id}",
          ],
        ],
        
$this->expectDeprecation('Drupal\Core\Url::toRenderArray() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3342977');
    $url = Url::fromRoute('entity.node.canonical', ['node' => 3]);
    $this->assertIsArray($url->toRenderArray());
  }

  /** * Tests the fromRouteMatch() method. */
  public function testFromRouteMatch() {
    $route = new Route('/test-route/{foo}');
    $route_match = new RouteMatch('test_route', $route['foo' => (object) [1]]['foo' => 1]);
    $url = Url::fromRouteMatch($route_match);
    $this->assertSame('test_route', $url->getRouteName());
    $this->assertEquals(['foo' => '1']$url->getRouteParameters());
  }

  /** * Data provider for testing entity URIs. */
  public function providerTestEntityUris() {
    return [
      [
        'entity:test_entity/1',
        [],
Home | Imprint | This part of the site doesn't use cookies.