Error creating multiple custom routes for Webforms

Created on 8 December 2022, over 1 year ago
Updated 18 July 2024, about 2 months ago

Problem/Motivation

When I try to add a new custom route of a new form I get the following:
"There are already metatags created for this route."

Steps to reproduce

Activate
Metatag Custom Routes (Paths)

- Create first form with webform module and add meta tag for custom path eg webform path /contact us

- Create second form and add meta tag for the custom route for the second form, for example /custom-contact there you will see the following error "There are already metatags created for this route." even though it's a new route, show error.

🐛 Bug report
Status

Active

Version

1.22

Component

Code

Created by

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

Comments & Activities

Not all content is available!

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

  • 🇨🇳China hongqing

    I have the same issue with Metatag 2.0.0 and Drupal 10.3.0 when add tags to custom route.

  • Status changed to Needs review about 2 months ago
  • 🇮🇳India vinaik Delhi NCR

    Hi @nelo_drup, @hongqing,

    I set the meta tags for a custom route of a webform and got a success message "Created metatags for the path: /form/custom-contact. Internal route: entity.webform.canonical.". but when I tried to set meta tags for second webform using their custom route then got the error message "There are already metatags created for this route".

    The error "There are already metatags created for this route" typically occurs because the Metatag module in Drupal considers the internal route of the webform rather than the URL alias.

    So the internal route for viewing a webform is typically entity.webform.canonical and the path is usually /webform/{webform_id}.

    Kindly refer to the attached screenshot for reference.

    You can use the below custom logic in custom module or theme hook to set meta tags for custom route of the webforms -

      if (\Drupal::routeMatch()->getRouteName() == 'entity.webform.canonical') {
        $alias = \Drupal::service('path_alias.manager')->getAliasByPath(\Drupal::service('path.current')->getPath());
        if ($alias == '/contact-us') {
            // Add meta tags for /contact-us
            $variables['#attached']['html_head'][] = [
                [
                    '#tag' => 'meta',
                    '#attributes' => [
                        'name' => 'description',
                        'content' => 'Contact us form description',
                    ],
                ],
            ];
        } elseif ($alias == '/custom-contact') {
            // Add meta tags for /custom-contact
            $variables['#attached']['html_head'][] = [
                [
                    '#tag' => 'meta',
                    '#attributes' => [
                        'name' => 'description',
                        'content' => 'Custom Contact us form description',
                    ],
                ],
            ];
        }
    }
    
  • Status changed to Active about 2 months ago
Production build 0.71.5 2024