2

I created a forum on a site using the core Forum module. When we create a new forum, Drupal creates it as a taxonomy term. When we create the new forum, if we associate a path alias, that URL alias is only associated to the taxonomy term and not to the forum. If we click on a specific forum from the index page (example.com/forum) we are sent to example.com/forum/nn.

How can I associate a path alias? I'm not being able to do it via the Pathauto module. Can someone tell me how to do it with that module or using any other solution?

7
  • The Pathauto module does support the Forum module. What did you get from it, and how did it differ from what you expected to get?
    – apaderno
    Commented Apr 22, 2018 at 20:14
  • I was able to get a path alias for each forum, using the Pathauto module, but it seems that in the /forum page (the one listing all the existing forums) the path aliases aren't used. I get the path alias in the page showing a forum topic, in the link to to the forum.
    – apaderno
    Commented Apr 22, 2018 at 20:50
  • As I pointed out below you solved my issue teaching me an additional functionality of Path/pathauto (The generation of individual alias, that I didn’t realise it was still available after installing Pathauto). Regarding the Pathauto usage I think that there might be a bug ibn it, because after enabling the forum module (from the core), when I tried to use the “Bulk Generation”, I had available a “Forum” option on the sub tab: “Select the types of paths for which to generate URL aliases”, but, at the same time on the tab: “Settings”, subtab: “ENABLED ENTITY TYPES” there is no “Forum” option Commented Apr 23, 2018 at 11:25
  • After I created a Pattern of type: “Forum”, and that Pattern runs, but doesn’t generate Alias. May be a Drupal Apprentice with 5 months experience shouldn't use the word “bug” but I think the interaction of Pathauto and Forum modules is not working properly. If you want I will give you “admin” permission to my site so that you can inspect it. Nevertheless your solution solved my issue via the individual alias generation, and I’m very thankful to you. Commented Apr 23, 2018 at 11:26
  • Yes, it is a little confusing that in the settings you don't explicitly enable the automatic path aliases the Pathauto module creates for forums, but you need to select Forum when you create a pattern for path aliases. It is even more confusing that the path aliases assigned to forums are really assigned to the taxonomy terms associated to the forums. I added the screenshots to show the Pathauto settings I used in the test site.
    – apaderno
    Commented Apr 23, 2018 at 13:03

3 Answers 3

4

It is possible to add a path alias to a forum, without installing any third-party modules.
You could install the Pathauto module, but that is more for automatically assigning the path aliases when creating/editing an entity, or for bulk-assigning path aliases to existing entities. If you install the Pathauto module to create path aliases for the forums, notice that under Enabled entity types on /admin/config/search/path/settings, you don't see an entry like Forum. Use the default values for that form field (which include Content and Taxonomy term that you cannot deselect), and you will be able to set path aliases for the forums.

screenshot

It is a quite confusing, since in /admin/config/search/path/patterns/add you need to select Forum for Pattern type.

screenshot

Maybe it would be less confusing if Forum would be an option for Enabled entity types too.

In this case, anyway, the path aliases that the Pathauto module says to assign to forums are really assigned to the taxonomy terms associated with the forums. The effect of this is that the /forum page would not use any path alias for the forum links.

I created a sandbox site on simplytest.me, and I just enabled the Forum module. I created two forums (Module development and Theme development), and created a forum topic (Porting the "User activity" module to Drupal 8). I went to /admin/config/search/path, and created the following path aliases.

  • /forums/1/general-discussion pointing to /forum/1
  • /forums/2/module-development pointing to /forum/2
  • /forums/3/theme-development pointing to /forum/3

Doing so, the /forum page used the path aliases I set, in the links to the forums, but the forum topic pages used links like https://dsze.ply.st/taxonomy/term/2 under Forums.

So, or you set a path alias that is used from the /forum page (but not from the forum topic pages), or you set a path alias that is used from the forum topics page (but not from the /forum page), or you set two different sets of path aliases (a set used from the /forum page, and a set used in the forum topic pages).

1
  • 1
    My most Sincere gratitude for the solution and lesson on Drupal. You are not only an expert, but you invest your time to help the apprentices like me. Rare and remarkable, thank you very much Commented Apr 23, 2018 at 11:02
0

I have just tested this feature on Drupal 10 and forum URL aliases worked fine.

  1. First install Pathauto if you don't yet have it: https://www.drupal.org/project/pathauto
  2. Then, from Extend in the administration panel, install Pathauto, Chaos tools, and Token. If you used Composer to install Pathauto, the other modules were also downloaded automatically.

Once Pathauto is installed, assuming the Forum module is also authorized, create a forum post for testing (if you don’t have any yet).

Now, let's create a URL pattern via Pathauto for your forum posts.

  1. Go to "Configuration", "Search and Metadata", then "URL Aliases".
  2. Do not click on Add alias.
  3. Instead, switch from List to Patterns (top tabs).
  4. From Patterns, click on Add Pathauto pattern.
  5. Once there, select Content from Pattern type (do not select forum).
  6. On Path pattern, write forum[node:title] as the pattern.
  7. On Content type, select Forum Topic.
  8. Write label title on the next window. You can type anything to remind you what this pattern represents (I typed "URL Patter Forums").
  9. SAVE.

The next thing to do is to convert your existing URLs.

  1. From the Search and Metadata page (you should be there now), select the "Bulk Generate tab".
  2. Select the type of paths for which to generate URL aliases. You can select all, since it will only do the ones that are authorized, but at least Content and Forum should be selected.
  3. Keep the "General URL alias for un-aliased paths only" checked. However, if you already had some aliases that you would like to override, check, instead, one of the other options.
  4. Press Update.

Lastly, you may have to clean the cache.

  1. Go Configuration.
  2. Performance.
  3. Clear all caches.

I hope it helps.


Note: As for general-discussions (this is the forum name). Since it is not user generated, you can use the core alias functionality.

There are two entities that point to general-discussions: node/x and term/x. (By x I mean the node number you have in your Drupal).

Here, you click on Configuration/Search and metada

Then, from the List tab, click on Add Alias.

then, you could enter /node/x as /general-discussion/, then save it.

And then add another alias for term/x using the same general-discussion label or something else, then save it.

Clear the cache.

-1

We can set forum patterns using the following trick. First create a forum pattern: enter image description here

Then we can use the following code in custom module:

function my_custom_pathauto_alias_alter(&$alias, array &$context) {

  if (!empty($context['data']['term']) && $context['data']['term']->bundle() == 'forums') {

    $alias_bk = $alias;
    $alias = preg_replace('~^/forum/~', '/forum_term/', $alias);

    $tid = $context['data']['term']->id();

    $pids = \Drupal::entityQuery('path_alias')
        ->condition('path', '/forum/' . $tid)
        ->accessCheck(FALSE)
        ->execute();

    if ($pids) {
      $pids = array_values($pids);
      $pid_main = $pids[0];

      unset($pids[0]);

      foreach ($pids as $pid) {
        $alias_to_del = \Drupal\path_alias\Entity\PathAlias::load($pid);
        if ($alias_to_del) {
          $alias_to_del->delete();
        }
      }

      $alias_to_change = \Drupal\path_alias\Entity\PathAlias::load($pid_main);
      if ($alias_to_change) {
        $alias_to_change->set('alias', $alias_bk)->save();
      }
    }
    else {
      $path_alias = \Drupal\path_alias\Entity\PathAlias::create([
            'path'  => '/forum/' . $tid,
            'alias' => $alias_bk,
      ]);

      $path_alias->save();
    }
  }
}

So the generated pattern will be used for forums, but forum terms aliases will be changed to something else we want.

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.