Custom JSON LD not working on frontpage when using path aliases

Created on 25 October 2023, about 1 year ago
Updated 30 October 2023, about 1 year ago

Problem/Motivation

On schemadotorg_jsonld_custom, the comparison to detect front page can fail if there is a path alias for the node.

Steps to reproduce

  1. Create a node with a path alias, i.e. /frontpage
  2. Point the site settings front page to that node using the path alias
  3. Create a custom Json LD for path /
  4. Acces the front page, the JSON LD is not added

If you check the code with xedbug or other tools, you can realize that while $path gets evaluated to the path alias (frontpage), the result of $front_path is the /node/nide route, generating a false negative:

  public function buildRouteMatchJsonLd(RouteMatchInterface $route_match): ?array {
    $url = Url::fromRouteMatch($route_match);
    $path = parse_url($url->toString(), PHP_URL_PATH);

    $front_path = $this->configFactory
      ->get('system.site')
      ->get('page.front');
    if ($path === $front_path) {
      $path = '/';
    }
}

Proposed resolution

Ensure that comparison is evaluated using always the original URL (node/nid) and not the path aliases.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain idiaz.roncero Madrid

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

Comments & Activities

  • Issue created by @idiaz.roncero
  • πŸ‡ͺπŸ‡ΈSpain idiaz.roncero Madrid

    Drupal will convert internally any path alias that is stored under system.site.page.front config to its unaliased path, see Drupal\system\Form::validateForm and Drupal\system\Form::submitForm:

      /**
       * {@inheritdoc}
       */
      public function validateForm(array &$form, FormStateInterface $form_state) {
        // Get the normal path of the front page.
        $form_state->setValueForElement($form['front_page']['site_frontpage'], $this->aliasManager->getPathByAlias($form_state->getValue('site_frontpage')));
    

    I think then it is safe to always assume the drupal site front page will be expresed as an un-aliased path and request the current request URL also as the unprocessed one:

        $path = parse_url($url->setOption('path_processing', false)->toString(), PHP_URL_PATH);
    

    As far as i can tell from looking at the code, the quickest solution is to bypass the path processing option when generating the URL string

  • @idiazroncero opened merge request.
  • Status changed to Needs review about 1 year ago
  • πŸ‡ͺπŸ‡ΈSpain idiaz.roncero Madrid

    Submitted MR, ready for review.

  • πŸ‡ͺπŸ‡ΈSpain idiaz.roncero Madrid

    Explanation: i'm not using the more mainstream $url->getInternalPath() method because it returns an un-slashed node/NID path that needs to be compared with an slashed /node/NID path.

    Passing path_processing seems to work as it will return the slashed path.

  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    I tweaked the code and added some more test coverage.

  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    So the new tests pass locally but fail via GitLab CI. I might comment out the failing test and deal with it later.

  • Status changed to Fixed about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024