getRouteObject example

public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
    $response = new AjaxResponse();

    // First render the main content, because it might provide a title.     $content = $this->renderer->renderRoot($main_content);
    // Attach the library necessary for using the OpenOffCanvasDialogCommand and     // set the attachments for this Ajax response.     $main_content['#attached']['library'][] = 'core/drupal.dialog.off_canvas';
    $response->setAttachments($main_content['#attached']);

    // If the main content doesn't provide a title, use the title resolver.     $title = $main_content['#title'] ?? $this->titleResolver->getTitle($request$route_match->getRouteObject());

    // Determine the title: use the title provided by the main content if any,     // otherwise get it from the routing information.     $options = $request->request->all('dialogOptions');
    $response->addCommand(new OpenOffCanvasDialogCommand($title$content$options, NULL, $this->position));
    return $response;
  }

}

  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /** * {@inheritdoc} */
  public function check(Response $response, Request $request) {
    if (($route = $this->routeMatch->getRouteObject()) && $route->getOption('_admin_route')) {
      return static::DENY;
    }
  }

}
public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /** * {@inheritdoc} */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $result = [];
    $context_definition = EntityContextDefinition::create('taxonomy_term')->setRequired(FALSE);
    $value = NULL;
    if ($route_object = $this->routeMatch->getRouteObject()) {
      $route_parameters = $route_object->getOption('parameters');

      if (isset($route_parameters['taxonomy_term']) && $term = $this->routeMatch->getParameter('taxonomy_term')) {
        $value = $term;
      }
      elseif ($this->routeMatch->getRouteName() == 'entity.taxonomy_term.add_form') {
        $vocabulary = $this->routeMatch->getParameter('taxonomy_vocabulary');
        $value = Term::create(['vid' => $vocabulary->id()]);
      }
    }

    
$route_match = RouteMatch::createFromRequest($request);
    return $this->check($route_match$account$request$return_as_object);
  }

  /** * {@inheritdoc} */
  public function check(RouteMatchInterface $route_match, AccountInterface $account = NULL, Request $request = NULL, $return_as_object = FALSE) {
    if (!isset($account)) {
      $account = $this->currentUser;
    }
    $route = $route_match->getRouteObject();
    $checks = $route->getOption('_access_checks') ?: [];

    // Filter out checks which require the incoming request.     if (!isset($request)) {
      $checks = array_diff($checks$this->checkProvider->getChecksNeedRequest());
    }

    $result = AccessResult::neutral();
    if (!empty($checks)) {
      $arguments_resolver = $this->argumentsResolverFactory->getArgumentsResolver($route_match$account$request);
      $result = AccessResult::allowed();
      

  protected function prepare(array $main_content, Request $request, RouteMatchInterface $route_match) {
    // Determine the title: use the title provided by the main content if any,     // otherwise get it from the routing information.     $get_title = function Darray $main_content) use ($request$route_match) {
      return $main_content['#title'] ?? $this->titleResolver->getTitle($request$route_match->getRouteObject());
    };

    // If the _controller result already is #type => page,     // we have no work to do: The "main content" already is an entire "page"     // (see html.html.twig).     if (isset($main_content['#type']) && $main_content['#type'] === 'page') {
      $page = $main_content;
      $title = $get_title($page);
    }
    // Otherwise, render it as the main content of a #type => page, by selecting     // page display variant to do that and building that page display variant.
// First render the main content, because it might provide a title.     $content = $this->renderer->renderRoot($main_content);

    // Attach the library necessary for using the OpenDialogCommand and set the     // attachments for this Ajax response.     $main_content['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $response->setAttachments($main_content['#attached']);

    // Determine the title: use the title provided by the main content if any,     // otherwise get it from the routing information.     $title = $main_content['#title'] ?? $this->titleResolver->getTitle($request$route_match->getRouteObject());

    // Determine the dialog options and the target for the OpenDialogCommand.     $options = $request->request->all('dialogOptions');
    $target = $this->determineTargetSelector($options$route_match);

    $response->addCommand(new OpenDialogCommand($target$title$content$options));
    return $response;
  }

  /** * Determine the target selector for the OpenDialogCommand. * * @param array &$options * The 'target' option, if set, is used, and then removed from $options. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * When no 'target' option is set in $options, $route_match is used instead * to determine the target. * * @return string * The target selector. */
while (count($path_elements) > 1) {
      array_pop($path_elements);
      // Copy the path elements for up-casting.       $route_request = $this->getRequestForPath('/' . implode('/', $path_elements)$exclude);
      if ($route_request) {
        $route_match = RouteMatch::createFromRequest($route_request);
        $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);
        }
      }
    }

    
/** * Gets a configuration mapper using a route match. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match to populate the mapper with. * * @return \Drupal\config_translation\ConfigMapperInterface * The configuration mapper. */
  protected function getMapperFromRouteMatch(RouteMatchInterface $route_match) {
    $mapper = $this->configMapperManager->createInstance($route_match->getRouteObject()
      ->getDefault('plugin_id'));
    $mapper->populateFromRouteMatch($route_match);
    return $mapper;
  }

  /** * Checks access given an account, configuration mapper, and source language. * * Grants access if the proper permission is granted to the account, the * configuration has translatable pieces, and the source language is not * locked given it is present. * * @param \Drupal\Core\Session\AccountInterface $account * The account to check access for. * @param \Drupal\config_translation\ConfigMapperInterface $mapper * The configuration mapper to check access for. * @param \Drupal\Core\Language\LanguageInterface|null $source_language * The source language to check for, if any. * * @return \Drupal\Core\Access\AccessResultInterface * The result of the access check. */

  public function testGetRouteName(RouteMatchInterface $route_match) {
    $this->assertSame('test_route', $route_match->getRouteName());
  }

  /** * @covers ::getRouteObject * @dataProvider routeMatchProvider */
  public function testGetRouteObject(RouteMatchInterface $route_match, Route $route) {
    $this->assertSame($route$route_match->getRouteObject());
  }

  /** * @covers ::getParameter * @covers \Drupal\Core\Routing\RouteMatch::getParameterNames * @dataProvider routeMatchProvider */
  public function testGetParameter(RouteMatchInterface $route_match, Route $route$parameters$expected_filtered_parameters) {
    foreach ($expected_filtered_parameters as $name => $expected_value) {
      $this->assertSame($expected_value$route_match->getParameter($name));
    }
    

  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /** * {@inheritdoc} */
  public function check(Response $response, Request $request) {
    if (($route = $this->routeMatch->getRouteObject()) && $route->getOption('no_cache')) {
      return static::DENY;
    }
  }

}
$response = new AjaxResponse();

    // First render the main content, because it might provide a title.     $content = $this->renderer->renderRoot($main_content);

    // Attach the library necessary for using the OpenModalDialogCommand and set     // the attachments for this Ajax response.     $main_content['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $response->setAttachments($main_content['#attached']);

    // If the main content doesn't provide a title, use the title resolver.     $title = $main_content['#title'] ?? $this->titleResolver->getTitle($request$route_match->getRouteObject());

    // Determine the title: use the title provided by the main content if any,     // otherwise get it from the routing information.     $options = $request->request->all('dialogOptions');
    // Override width option.     switch ($this->mode) {
      case 'wide':
        $options['width'] = 700;
        break;

      case 'extra_wide':
        
$response = new AjaxResponse();

    // First render the main content, because it might provide a title.     $content = $this->renderer->renderRoot($main_content);

    // Attach the library necessary for using the OpenModalDialogCommand and set     // the attachments for this Ajax response.     $main_content['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $response->setAttachments($main_content['#attached']);

    // If the main content doesn't provide a title, use the title resolver.     $title = $main_content['#title'] ?? $this->titleResolver->getTitle($request$route_match->getRouteObject());

    // Determine the title: use the title provided by the main content if any,     // otherwise get it from the routing information.     $options = $request->request->all('dialogOptions');

    $response->addCommand(new OpenModalDialogCommand($title$content$options));
    return $response;
  }

}
$request_stack->getCurrentRequest()
      ->willReturn($request);

    $session_configuration = $this->prophesize(SessionConfigurationInterface::class);
    $session_configuration->hasSession(Argument::type(Request::class))
      ->willReturn($request_has_session);

    $route = $this->prophesize(Route::class);
    $route->getOption('_no_big_pipe')
      ->willReturn($route_match_has_no_big_pipe_option);
    $route_match = $this->prophesize(RouteMatchInterface::class);
    $route_match->getRouteObject()
      ->willReturn($route);

    $big_pipe_strategy = new BigPipeStrategy($session_configuration->reveal()$request_stack->reveal()$route_match->reveal());
    $processed_placeholders = $big_pipe_strategy->processPlaceholders($placeholders);

    if ($request->isMethodCacheable() && !$route_match_has_no_big_pipe_option && $request_has_session) {
      $this->assertSameSize($expected_big_pipe_placeholders$processed_placeholders, 'BigPipe is able to deliver all placeholders.');
      foreach (array_keys($placeholders) as $placeholder) {
        $this->assertSame($expected_big_pipe_placeholders[$placeholder]$processed_placeholders[$placeholder], "Verifying how BigPipeStrategy handles the placeholder '$placeholder'");
      }
    }
    


  /** * Creates a new URL object from a route match. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * * @return static */
  public static function fromRouteMatch(RouteMatchInterface $route_match) {
    if ($route_match->getRouteObject()) {
      return new static($route_match->getRouteName()$route_match->getRawParameters()->all());
    }
    else {
      throw new \InvalidArgumentException('Route required');
    }
  }

  /** * Creates a Url object for a relative URI reference submitted by user input. * * Use this method to create a URL for user-entered paths that may or may not * correspond to a valid Drupal route. * * @param string $user_input * User input for a link or path. The first character must be one of the * following characters: * - '/': A path within the current site. This path might be to a Drupal * route (e.g., '/admin'), to a file (e.g., '/README.txt'), or to * something processed by a non-Drupal script (e.g., * '/not/a/drupal/page'). If the path matches a Drupal route, then the * URL generation will include Drupal's path processors (e.g., * language-prefixing and aliasing). Otherwise, the URL generation will * just append the passed-in path to Drupal's base path. * - '?': A query string for the current page or resource. * - '#': A fragment (jump-link) on the current page or resource. * This helps reduce ambiguity for user-entered links and paths, and * supports user interfaces where users may normally use auto-completion * to search for existing resources, but also may type one of these * characters to link to (e.g.) a specific path on the site. * (With regard to the URI specification, the user input is treated as a * @link https://tools.ietf.org/html/rfc3986#section-4.2 relative URI reference @endlink * where the relative part is of type * @link https://tools.ietf.org/html/rfc3986#section-3.3 path-abempty @endlink.) * @param array $options * (optional) An array of options. See Url::fromUri() for details. * * @return static * A new Url object based on user input. * * @throws \InvalidArgumentException * Thrown when the user input does not begin with one of the following * characters: '/', '?', or '#'. */
/** * {@inheritdoc} */
  public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
    $json = [];

    $json['content'] = (string) $this->renderer->renderRoot($main_content);
    if (!empty($main_content['#title'])) {
      $json['title'] = (string) $main_content['#title'];
    }
    else {
      $json['title'] = (string) $this->titleResolver->getTitle($request$route_match->getRouteObject());
    }

    $response = new CacheableJsonResponse($json, 200);
    $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($main_content));
    return $response;
  }

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