Hot answers tagged

5

Instead of \Drupal::service('router.builder')->rebuild() you can invalidate route matches: Cache::invalidateTags(['route_match']); You don't need to rebuild the router table, because the static routes don't change. What you need to clear are route matches, which are calculated on the processed path and then stored for the unprocessed path. And this is ...


5

The dash is not the problem, you could use a dash in the fixed part, but ... Unlike generic Symfony routes, Drupal requires that a slug occupies a complete path part - the portion between two slashes (or everything after the last slash). https://www.drupal.org/docs/8/api/routing-system/parameters-in-routes/using-parameters-in-routes Alternatively ...


3

The reason why you get that error is because run is not expected to be called statically. \Drupal\Core\Cron is a service class managed by Drupal, registered in core.services.yml. To get a reference to the instance of this service, you'll need to inject the dependency to your controller. namespace Drupal\mymodule\Controller; use Drupal\Core\Controller\...


3

You need to give the router a nudge towards what $politician is supposed to be for the automatic up-casting to kick in. There are two ways to do that: Type-hint the variable in the controller method: function build(Politician $politician) { See EntityResolverManager::setParametersFromReflection for details Explicitly set the parameter type in the route ...


3

I guess you need to do a route parameter upcasting: entity.politician.about_me: path: '/politician/{politician}/about-me' defaults: _controller: '\Drupal\pw_basic\Controller\PoliticianFrontend\AboutMe::build' _title_callback: '\Drupal\pw_basic\Controller\PoliticianFrontend\AboutMe::title' requirements: _entity_access: 'politician.view' ...


3

You probably just need to declare the parameter type: confirm: path: '/node/{node}/submission/{webform_submission}/confirm' defaults: _controller: '\Drupal\mymodule\Controller\MymoduleController::confirm' _title: 'Confirm your submission' requirements: _permission: 'confirm webform submission' options: parameters: ...


3

I do not have enough reputation to add a comment on a reply, but the anwser by @Arosboro is almost complete, except the "build variable is in the wrong spot. It must be the last variable, like so: return $bareHtmlPageRenderer->renderBarePage([], 'Address', 'checkout_address', $build); This way the twig variables I placed in $build were rendered ...


3

Without knowing what is in the controller this would be the second one. The extra custom controller class only makes sense if you do something in myMethod() the form controller invoked by _form is not able to do. See https://www.drupal.org/node/2117411#fapi-in-route This integrates the form in a request so that it can receive route parameters and the ...


3

I see two problems. First your routing file has the path set to: path: '/{user}/settoken' To do what you're describing I would expect to see: path: '/u/{user}/settoken' Second, are you opposed to using a second parameter in the URL instead of the Get Method variable? If not opposed you can add a second parameter that will handle the 'token' variable. In ...


3

I think you can do this with the pathauto (path alias?) module. Go to the Admin -> Config -> Search / Metadata -> URL Aliases. In the settings tab, enable your custom entity type. When you edit them, you should have a URL field added. You can also use the patterns to define default paths if you want. I did a quick test with an ECK custom entity and it ...


2

For the configured custom path: CustomPageExceptionHtmlSubscriber::on404 public function on404(GetResponseForExceptionEvent $event) { $custom_404_path = $this->configFactory->get('system.site')->get('page.404'); if (!empty($custom_404_path)) { $this->makeSubrequestToCustomPath($event, $custom_404_path, Response::HTTP_NOT_FOUND);...


2

drush cc router when using drush 9 do the trick now :-)


2

Also - the answer above is incomplete since it skips a check on access to the node itself. This is possibly a recipe for an access bypass vulnerability. I'd think you'd want a route more like: mymodule.check_test: path: '/checktest/{node}' defaults: _controller: '\Drupal\mymodule\Controller\CheckTestController::content' requirements: ...


2

Turns out I just had to chain it to the AccessResult like so: AccessResult::allowed()->addCacheableDependency($requested_profile); Same for forbidden(). Now the 'View' tab is visible appropriately.


2

Your route doesn't have a parameter called node, so therefore $node is null. In order to make node available as a parameter you have to be on a route that has node as a parameter. So to add the node parameter to your route, you can do like this: rsvplist.form: path: '/rsvplist/{node}' defaults: _form: '\Drupal\rsvplist\Form\RSVPForm' _title: '...


2

The _node_operation_route option respect the Use the administration theme when editing or creating content, if you want to explicitly disable admin theme for this node-related page don't use _node_operation_route The _node_operation_route option overrides _admin_route https://api.drupal.org/api/drupal/core%21modules%21node%21src%21EventSubscriber%...


1

Use more placeholders in your route, like path: 'booking/{url_alias}/{anyother}' Then add a default for the last one, like defaults: anyother: "" Now your Path applies for two and three uri components. Check this page for default parameters in form - routes: https://www.drupal.org/docs/8/api/routing-system/parameters-in-routes/using-parameters-in-...


1

Is this possible? path: 'booking/{url_alias}' or path: '{url_alias}/booking' path: '{url_alias}/booking' paths can never start with a slug/placeholder. You'll have to use path: 'booking/{url_alias}' In your form: /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $url_alias = NULL) { // Some DB ...


1

Implement the redirect in a module. Either in a kernel request subscriber, see this change record https://www.drupal.org/node/2013014 or in a kernel response subscriber, see https://git.drupalcode.org/project/age_verification/blob/8.x/src/EventSubscriber/pathGate.php. The second example is from a contrib module you can try as it is. I don't have first-hand ...


1

The order doesn't matter, routes are stored indexed by name. The first and the second route have a different component count, so they can't interfere. The second route gets priority over the third, because it has one more fixed component. So in this case the route matching doesn't need any complex calculations. In general route matching rarely affects ...


1

Wound up figuring this out I suppose, although I have no idea if this is the proper way. Under the custom module I wanted to test, I created a 'tests' directory with a test module and within that new module placed 3 configuration files from my project config ("Show Profile" field storage, "Show Profile" field instance, user form display configuration). In ...


1

Add a route enhancer: Example how _form gets replaced by _controller: Drupal\Core\Routing\Enhancer\FormRouteEnhancer: <?php namespace Drupal\Core\Routing\Enhancer; use Drupal\Core\Routing\EnhancerInterface; use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; /** *...


1

With the help from @Clive I found a solution: /** * Implements hook_node_update(). */ function MY_MODULE_node_update(\Drupal\node\NodeInterface $node) { foreach ($node->getFieldDefinitions() as $definition) { if (!in_array($definition->getType(), ['entity_reference', 'entity_reference_revisions'])) { continue; } if (!$definition-&...


1

The accepted answer may be appropriate for the intended use case of the OP, however, the title of this question is "Routing match everything". As this is the closest question I've found from searching the interwebs, I'll just share my discovery here on how to accomplish this for SPAs where all paths that start with "/app" need to be redirected to a single ...


1

DynamicRoutesAlter.php Problem #1 - preg_replace() without pattern $url = preg_replace('_', '-', $key); The $url will be always NULL, because you are using preg_replace() without regexp, use instead str_replace() or write appropriate regexp pattern: '/_/'. Solution #1 $url = preg_replace('/_/', '-', $key); Solution #2 $url = str_replace('_', '-', $key)...


1

Now it's in the routing.yml file at the root of your module : Here an example : example.routing.yml example.page_form: path: '/example' defaults: _form: '\Drupal\example\Form\ExampleForm' _title: 'Example' requirements: _access: 'TRUE' You can restrict by role too for example : requirements: _role: 'administrator+editor' Or by ...


1

An event subscriber is just a service tagged with name: event_subscriber and implemented by a class implementing the EventSubscriberInterface interface. The code you are using is supposed to use a service, but the reference to that service isn't defined as parameter of the class constructor, nor it is initialized in any way. The MainContentViewSubscriber ...


1

In you routing.yml change _title to _title_callback as below: amap.distribution_inscription_many_form: path: '/amap/editPlanningMany' defaults: _form: '\Drupal\amap\Form\DistributionInscriptionManyForm' _title_callback: '\Drupal\amap\Controller\TitleController::title' requirements: _permission: 'edit distribution inscription entities' ...


1

You can use a class to generate dynamic tabs, and take example on the core tracker module which supports this. Let's take a look in the tracker.links.task.yml file : tracker.users_recent_tab: route_name: tracker.users_recent_content title: 'My recent content' base_route: tracker.page class: '\Drupal\tracker\Plugin\Menu\UserTrackerTab' And then the \...


1

Is there another way? In Drupal, there's always another way. In fact, easier ways are often overlooked. How can I remove tabs from the user's profile menu? You could hide the Drupal generated tabs specifically from the user pages and add in your own: Go to /admin/structure/block Find "Tabs" Block in the "Content" region and click configure. Under the "...


Only top voted, non community-wiki answers of a minimum length are eligible