1

I created a local action button with the following in my_module.links.action.yml:

my_module.content.action:
  route_name: node.add_page
  title: 'My action'
  deriver: 'Drupal\my_module\Plugin\Derivative\ContentByDomainLocalActions'
  appears_on:
    - view.affiliated_content.page_1

Then, the local action button appears in the page admin/content/domain-content/firstdomain_local

I'd like use the argument firstdomain_local from this URL path to pass it in my local action button.

So, I retrieve the param from \Drupal::routeMatch() in getDerivativeDefinitions, like in this tutorial:

class CustomLocalActions extends DeriverBase {
  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->derivatives['example.action_id'] = $base_plugin_definition;
    $this->derivatives['example.action_id']['title'] = "Add content";
    $domain = \Drupal::routeMatch()->getParameter('arg_0');
    $this->derivatives['example.action_id']['route_parameters'] = [
      'domain' => $domain
    ];
    return $this->derivatives;
  }
}

But the param of the local action button is only updated after I clear the cache. I have to clear the cache each time I use different parameter value in the URL.

What's the correct way to pass dynamically this argument?

2
  • Are you serving the same URL from different (sub-) domains (...and want different content [action links] per domain)? In that case you'll need a custom cache context for domains
    – Hudri
    Commented Oct 9, 2018 at 15:32
  • No, it's always the same domain (for administration). I just need to retrieve the param from the URL path.
    – Kwadz
    Commented Oct 10, 2018 at 7:41

2 Answers 2

3

Local actions and local tasks are quite similar, so looking at the Customising local task behavior I've managed to pass in dynamic route parameters to my local actions by specifying the class parameter (MyClassName) in my_module.links.action.yml and overriding the LocalActionDefault::getRouteParameters() method in that class. Only one initial cache rebuild was required.

use Drupal\Core\Menu\LocalActionDefault;
use Drupal\Core\Routing\RouteMatchInterface;

class MyClassName extends LocalActionDefault {

  public function getRouteParameters(RouteMatchInterface $route_match) {
    return array(
      'my_local_action_parameter' => $route_match->getParameter('existing_parameter_from_route')
    );
  }
}
2
  • Exactly what I needed. Cheers!
    – Beau
    Commented Jul 7, 2019 at 16:41
  • I'd also override getCacheMaxAge() to return zero to avoid caching it.
    – Gábor
    Commented Jul 12, 2022 at 16:12
1

Since it's a cache issue, awaiting an official fix, I solved it by rebuilding actions on the page in question:

function my_module_preprocess_page(&$variables) {
  if (\Drupal::routeMatch()->getRouteName() == 'view.affiliated_content.page_1') {
    \Drupal::service('plugin.manager.menu.local_action')->clearCachedDefinitions();
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.