The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Hot answers tagged

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 ...


4

$pattern = '/user/*/edit'; $route_provider = \Drupal::service('router.route_provider'); $found_routes = $route_provider->getRoutesByPattern($pattern); $route_names = []; foreach ($found_routes as $route_name => $route_object) { $route_names[] = $route_name; }


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

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 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

Thanks everyone! I have solution my for problem. You just had to add it to this my_module.links.contextual.yml file. regions.taxonomy_term.edit_form_header: title: Edit this block group: region_taxonomy_term route_name: regions.taxonomy_term.edit_form_header weight: 10 options: fragment: 'edit-group-header'


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 ...


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 ...


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

You can by altering the existing route. wt_lunch/src/Routing/RouteSubscriber.php namespace Drupal\wt_lunch\Routing; use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Component\Routing\RouteCollection; /** * Listens to the dynamic route events. */ class RouteSubscriber extends RouteSubscriberBase { /** * {@inheritdoc} */ protected ...


2

In Drupal 6 and Drupal 7, hook_menu() implementations don't get any argument. That is the reason of the error message. They got an argument in previous Drupal versions, but it was not passed by reference. It's probable you are getting confused between hook_menu() and hook_menu_alter(). The latter gets a $&items parameter, but the purpose of the hook is ...


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

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

Well if you want to redirect to an external page this can be your code: <?php namespace Drupal\achva_sso\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Routing\TrustedRedirectResponse; class SsoController extends ControllerBase{ public function ssoLogout(){ unset($_SESSION['name']); // Redirect. ...


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

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

This is not possible because the anchor tag is never sent as part of the HTTP request by any browser, it is only interpreted locally within the browser. RFC 1808 Section 2.4.1 - "Note that the fragment identifier is not considered part of the URL."


1

This might work if (($route = $collection->get('entity.example.canonical'))) { // base_route $current_route = \Drupal::routeMatch()->getRouteObject(); // get current route $current_route->setRequirement('_role', 'administrator'); }


1

No need to pass arguments to hook_menu Just remove the param &$items: /** * Implements hook_menu(). */ function wa_simulator_menu() { $items = array(); $items['signin'] = array( 'title' => 'Sign In', 'description' => 'Sign In Form', 'page callback' => 'drupal_get_form', 'page arguments' => array('...


1

In the routing path you want to include the node parameter, so it would be something like this. /admin/lunch/week/{node} For more info look here


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