hasRequirement example


  public function __construct(ModuleHandlerInterface $module_handler) {
    $this->moduleHandler = $module_handler;
  }

  /** * {@inheritdoc} */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($collection as $name => $route) {
      if ($route->hasRequirement('_module_dependencies')) {
        $modules = $route->getRequirement('_module_dependencies');

        $explode_and = $this->explodeString($modules, '+');
        if (count($explode_and) > 1) {
          foreach ($explode_and as $module) {
            // If any moduleExists() call returns FALSE, remove the route and             // move on to the next.             if (!$this->moduleHandler->moduleExists($module)) {
              $collection->remove($name);
              continue 2;
            }
          }

  public function getResponseFormat(RouteMatchInterface $route_match, Request $request) {
    $route = $route_match->getRouteObject();
    $acceptable_response_formats = $route->hasRequirement('_format') ? explode('|', $route->getRequirement('_format')) : [];
    $acceptable_request_formats = $route->hasRequirement('_content_type_format') ? explode('|', $route->getRequirement('_content_type_format')) : [];
    $acceptable_formats = $request->isMethodCacheable() ? $acceptable_response_formats : $acceptable_request_formats;

    $requested_format = $request->getRequestFormat();
    $content_type_format = $request->getContentTypeFormat();

    // If an acceptable response format is requested, then use that. Otherwise,     // including and particularly when the client forgot to specify a response     // format, then use heuristics to select the format that is most likely     // expected.     if (in_array($requested_format$acceptable_response_formats, TRUE)) {
      
/** * Checks access. * * @param \Symfony\Component\Routing\Route $route * The route to check against. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */
  public function access(Route $route) {
    if (!$route->hasRequirement('_has_active_workspace')) {
      return AccessResult::neutral();
    }

    $required_value = filter_var($route->getRequirement('_has_active_workspace'), FILTER_VALIDATE_BOOLEAN);
    return AccessResult::allowedIf($required_value === $this->workspaceManager->hasActiveWorkspace())->addCacheContexts(['workspace']);
  }

}

  public function __construct(CsrfTokenGenerator $csrf_token) {
    $this->csrfToken = $csrf_token;
  }

  /** * {@inheritdoc} */
  public function processOutbound($route_name, Route $route, array &$parameters, BubbleableMetadata $bubbleable_metadata = NULL) {
    if ($route->hasRequirement('_csrf_token')) {
      $path = ltrim($route->getPath(), '/');
      // Replace the path parameters with values from the parameters array.       foreach ($parameters as $param => $value) {
        $path = str_replace("{{$param}}", $value$path);
      }
      // Adding this to the parameters means it will get merged into the query       // string when the route is compiled.       if (!$bubbleable_metadata) {
        $parameters['token'] = $this->csrfToken->get($path);
      }
      else {
        
/** * Determines whether the given route is an HTML route. * * @param \Symfony\Component\Routing\Route $route * The route to analyze. * * @return bool * TRUE if HTML is a valid format for this route. */
  protected static function isHtmlRoute(Route $route) {
    // If a route has no explicit format, then HTML is valid.     $format = $route->hasRequirement('_format') ? explode('|', $route->getRequirement('_format')) : ['html'];
    return in_array('html', $format, TRUE);
  }

}
$request->setRequestFormat($default_format);
    }
    else {
      $format = $request->getRequestFormat($default_format);
    }

    $routes_with_requirement = [];
    $routes_without_requirement = [];
    $result_collection = new RouteCollection();
    /** @var \Symfony\Component\Routing\Route $route */
    foreach ($collection as $name => $route) {
      if (!$route->hasRequirement('_format')) {
        $routes_without_requirement[$name] = $route;
        continue;
      }
      else {
        $routes_with_requirement[$name] = $route;
      }
    }

    foreach ($routes_with_requirement as $name => $route) {
      // If the route has no _format specification, we move it to the end. If it       // does, then no match means the route is removed entirely.

  protected static function isGetAndHtmlRoute(Route $route) {
    $methods = $route->getMethods() ?: ['GET'];
    // If a route has no explicit format, then HTML is valid.     // @see \Drupal\Core\Routing\RequestFormatRouteFilter::getAvailableFormats()     $format = $route->hasRequirement('_format') ? explode('|', $route->getRequirement('_format')) : ['html'];
    return in_array('GET', $methods, TRUE) && in_array('html', $format, TRUE);
  }

}
$this->assertEquals($route$route->setRequirements([]), '->setRequirements() implements a fluent interface');

        $route->setRequirements(['foo' => '\d+']);
        $route->addRequirements(['bar' => '\d+']);
        $this->assertEquals($route$route->addRequirements([]), '->addRequirements() implements a fluent interface');
        $this->assertEquals(['foo' => '\d+', 'bar' => '\d+']$route->getRequirements(), '->addRequirement() keep previous requirements');
    }

    public function testRequirement()
    {
        $route = new Route('/{foo}');
        $this->assertFalse($route->hasRequirement('foo'), '->hasRequirement() return false if requirement is not set');
        $route->setRequirement('foo', '^\d+$');
        $this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the path');
        $this->assertTrue($route->hasRequirement('foo'), '->hasRequirement() return true if requirement is set');
    }

    public function testRequirementAlternativeStartAndEndRegexSyntax()
    {
        $route = new Route('/{foo}');
        $route->setRequirement('foo', '\A\d+\z');
        $this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes \A and \z from the path');
        $this->assertTrue($route->hasRequirement('foo'));
    }
        if (empty($rest_resource_config->getFormats($method))) {
          $this->logger->error('At least one format must be defined for resource @id', ['@id' => $rest_resource_config->id()]);
          continue;
        }

        // The configuration has been validated, so we update the route to:         // - set the allowed response body content types/formats for methods         // that may send response bodies (unless hardcoded by the plugin)         // - set the allowed request body content types/formats for methods that         // allow request bodies to be sent (unless hardcoded by the plugin)         // - set the allowed authentication providers         if (in_array($method['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE'], TRUE) && !$route->hasRequirement('_format')) {
          $route->addRequirements(['_format' => implode('|', $rest_resource_config->getFormats($method))]);
        }
        if (in_array($method['POST', 'PATCH', 'PUT'], TRUE) && !$route->hasRequirement('_content_type_format')) {
          $route->addRequirements(['_content_type_format' => implode('|', $rest_resource_config->getFormats($method))]);
        }
        $route->setOption('_auth', $rest_resource_config->getAuthenticationProviders($method));
        $route->setDefault('_rest_resource_config', $rest_resource_config->id());
        $parameters = $route->getOption('parameters') ?: [];
        $route->setOption('parameters', $parameters + [
          '_rest_resource_config' => [
            'type' => 'entity:' . $rest_resource_config->getEntityTypeId(),
          ],

  protected function overrideApplies($view_path, Route $view_route, Route $route) {
    $route_has_format = $route->hasRequirement('_format');
    $route_formats = $route_has_format ? explode('|', $route->getRequirement('_format')) : [];
    $view_route_formats = $view_route->hasRequirement('_format') ? explode('|', $view_route->getRequirement('_format')) : [];
    return $this->overrideAppliesPathAndMethod($view_path$view_route$route)
      && (!$route_has_format || array_intersect($route_formats$view_route_formats) != []);
  }

  /** * {@inheritdoc} */
  public static function buildResponse($view_id$display_id, array $args = []) {
    $build = static::buildBasicRenderable($view_id$display_id$args);

    

  protected function overrideApplies($view_path, Route $view_route, Route $route) {
    return (!$route->hasRequirement('_format') || $route->getRequirement('_format') === 'html')
      && $this->overrideAppliesPathAndMethod($view_path$view_route$route);
  }

  /** * Determines whether an override for the path and method should happen. * * @param string $view_path * The path of the view. * @param \Symfony\Component\Routing\Route $view_route * The route of the view. * @param \Symfony\Component\Routing\Route $route * The route itself. * * @return bool * TRUE, when the view should override the given route. */
Home | Imprint | This part of the site doesn't use cookies.