Get a token of a node's parent group to create a pathauto pattern

Created on 28 July 2016, over 8 years ago
Updated 17 February 2023, almost 2 years ago

Hello,

I'm trying to create a Pathauto pattern for a content type based off of the group(s) that a node is associated with. So, for example, I have a content type called About, and this content type is made available to a group type called Department. Groups that are within the Department group type include Biology and Chemistry. Ideally, when an About node is created and associated with a given group, I'd like to have the path for the node to be generated via Pathauto pattern as something like [node:group:title]/[node:title]. However, there's no token available to do anything like this for a content pattern type. How could this be done? Thank you for any help.

πŸ’¬ Support request
Status

Needs review

Version

1.5

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States cainaru Norwood, NY, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States kroh San Antonio, TX

    Re-rolled patch from #90 to work with 2.0.x

  • The last submitted patch, 93: group-2774827-93.patch, failed testing. View results β†’
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Status changed to Needs work over 1 year ago
  • πŸ‡ͺπŸ‡ΈSpain rteijeiro

    I'm experiencing an issue with this. After applying the patch in #90, I create new content in a group and it seems the tokens in [node:group:*] are empty just when the content is created. If I try to recreate the path aliases, then it works. Not sure if the issue is caused for some misconfiguration but I'm still investigating.

    Sharing this if someone else experienced the same or not.

  • πŸ‡ͺπŸ‡ΈSpain rteijeiro

    I solved the issue in #95 forcing the path alias to be created in `hook_entity_presave` only for newly created entities. I'll test if this is just an issue in my environment or if I can reproduce it in the latest versions of Drupal and the contrib modules. In the meantime I'll leave the code below if someone experiences the same issue:

    if ($entity->isNew()) {
          $entity->path = \Drupal::service('pathauto.generator')->updateEntityAlias($entity, 'insert');
    }
    
  • πŸ‡¦πŸ‡ΊAustralia realityloop

    #90 working on 3.x

  • πŸ‡ΊπŸ‡ΈUnited States caesius

    I assume from the comments and patches this applies up to 3.x, so updating the version for visibility.

  • πŸ‡¨πŸ‡¦Canada JayDarnell Guelph, Ontario

    I've tested the patch from #90 on a few sites now and it seems to work really well.

  • πŸ‡ΊπŸ‡ΈUnited States adriancotter

    I have an error with #93 after I upgraded to 2.2.2, I am getting:
    ArgumentCountError: Too few arguments to function Drupal\group\Access\GroupPermissionsHashGenerator::__construct(), 3 passed in ...web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 and exactly 5 expected in Drupal\group\Access\GroupPermissionsHashGenerator->__construct() (line 70 of modules/contrib/group/src/Access/GroupPermissionsHashGenerator.php).

    Any admin page throws an error. Group pages and related nodes also get the error. But non-group content is still viewable on the front end.

    Steps to reproduce

    We are still on Drupal 9.5.11
    I got this after upgrading my Group module from 2.2.0 to 2.2.2
    I was able to update to 2.2.1 ok

    I decided to check if this patch might have an impact, when I saw the Token functions in the stack trace.
    I don't immediately see where the patch might need to be tweaked.

    When I took out the patch the errors went away.

    Drupal\group\Access\GroupPermissionsHashGenerator->__construct(Object, Object, Object) (Line: 259)
    Drupal\Component\DependencyInjection\Container->createService(Array, 'group_permission.hash_generator') (Line: 177)
    Drupal\Component\DependencyInjection\Container->get('group_permission.hash_generator', 1) (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 237)
    Drupal\Component\DependencyInjection\Container->createService(Array, 'cache_context.user.group_permissions') (Line: 177)
    Drupal\Component\DependencyInjection\Container->get('cache_context.user.group_permissions') (Line: 223)
    Drupal\Core\Cache\Context\CacheContextsManager->getService('user.group_permissions') (Line: 185)
    Drupal\Core\Cache\Context\CacheContextsManager->optimizeTokens(Array) (Line: 111)
    Drupal\Core\Cache\Context\CacheContextsManager->convertTokensToKeys(Array) (Line: 317)
    Drupal\Core\Render\RenderCache->createCacheID(Array) (Line: 66)
    Drupal\Core\Render\RenderCache->get(Array) (Line: 109)
    Drupal\Core\Render\PlaceholderingRenderCache->get(Array) (Line: 77)
    Drupal\Core\Render\RenderCache->get(Array) (Line: 109)
    Drupal\Core\Render\PlaceholderingRenderCache->get(Array) (Line: 273)
    Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 479)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 43)
    __TwigTemplate_f60975c3bcef63585ca1e008bb6405ed->doDisplay(Array, Array) (Line: 405)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
    Twig\Template->display(Array) (Line: 390)
    Twig\Template->render(Array) (Line: 55)
    twig_render_template('themes/contrib/seven/templates/page.html.twig', Array) (Line: 384)
    Drupal\Core\Theme\ThemeManager->render('page', Array) (Line: 433)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 479)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 86)
    __TwigTemplate_18e80ba8011e325cf036296dbb23f15b->doDisplay(Array, Array) (Line: 405)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
    Twig\Template->display(Array) (Line: 390)
    Twig\Template->render(Array) (Line: 55)
    twig_render_template('themes/contrib/seven/templates/classy/layout/html.html.twig', Array) (Line: 384)
    Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 433)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 162)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 163)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 174)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 44)
    Drupal\redirect_after_login\RedirectMiddleware->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
    
  • Merge request !156Patch #90 β†’ (Open) created by karlshea
  • Merge request !157Patch #93 β†’ (Open) created by karlshea
  • Status changed to Needs review 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Created MRs for both 3.x and 2.x from the latest patches.

  • πŸ‡¨πŸ‡¦Canada slydevil Halifax

    Applied the patch for the 2.x branch and it works well. Not moving to RTBC since I did not try the 3x branch.

  • πŸ‡¨πŸ‡¦Canada vamirbekyan Toronto

    pls note that for group v3.x in the subject patch the GroupContent is replaced with GroupRelationship

  • πŸ‡ΊπŸ‡ΈUnited States dave.weiner

    This patch looks exactly like what I need, but I'm new to the group module, and I'm not understanding how this patch works. I've patched the group module (3.x) with the diff from the MR and it applies cleanly, but I'm still not seeing any group tokens when I select Content as the pattern type in pathauto.

    Can someone please explain it to me like I'm a 1st grader? :)

  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    You should be able to use [node:group] tokens. I'm still using Group 2.x (MR 157), if you're on 3.x (MR 156) the MR might be wrong, but I haven't been able to test it.

  • πŸ‡ΊπŸ‡ΈUnited States dave.weiner

    Karlshea, I must be missing something. I'm running version 3.2.2, the patch from MR 156 applied cleanly, and I also have group_content_menu v3.0.3 and group_context_path_prefix v1.1.2 installed. When I go into the Pathauto configuration and select content for the pattern type, I don't have a [node:group] token in the list of available tokens. Selecting group or group relationship, there are no node tokens in the list of available tokens. What am I missing? Is there a configuration option I'm not setting?

  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Is node one of the available types of content in one of your groups? Tried clearing caches? Otherwise you might have to debug yourself or wait for someone running Group 3.x to chime in, I'm not sure.

  • πŸ‡ΊπŸ‡ΈUnited States dave.weiner

    I found the issue -- I don't know if this was part of it, but disabled group_contenxt_path_prefix and then enabled it again. It also looks like I was hitting the pathauto configuration page inside of the group. Once I did it at the top level, I had the token available. I had to delete an existing pattern for article and basic page, and now it's working how I expect it to. Thanks for the help!

  • πŸ‡¨πŸ‡¦Canada mandclu

    This is exactly what I was looking for. After applying the changes in MR!156 I was able to use a node token that references the parent group. Thanks to everyone whose work made this possible!

  • πŸ‡ΊπŸ‡ΈUnited States dalemoore

    Like @mandclu, I've tested the MR!156 in Group 3 and it works! Though I only tested it on content that belongs to one group. Next up: to see if I can create a path based on a Group Content Menu...

  • πŸ‡¬πŸ‡§United Kingdom rachel_norfolk UK

    updating my attribution

  • πŸ‡¬πŸ‡§United Kingdom rachel_norfolk UK

    and trying again - it’s persistent today ;-)

  • πŸ‡ΊπŸ‡ΈUnited States dave.weiner

    MR 156 is working for me as well. Any idea when this might be merged in?

  • πŸ‡¬πŸ‡§United Kingdom rachel_norfolk UK

    I’m going to assume you didn’t mean to change the status, Dave.weiner?

Production build 0.71.5 2024