Saved Pattern Entities Include Drupal Root In Cached Asset Paths

Created on 8 August 2023, 11 months ago
Updated 26 September 2023, 9 months ago

Problem/Motivation

When a pattern is used by a block, the version of the pattern used at the time is saved in the database as a Pattern (patternkit_pattern) entity. This entity captures the current state of the assets, schema, and template values for reference by the block whenever it is rendered and stability preventing later changes to the pattern from breaking existing content already using the pattern.

In most cases, this system has worked well, but starting since the refactors introduced in the Beta 7 release, anytime the block is tested for saving or updating, the saved asset paths are loaded to compare the contents of the schema and template values to determine if the pattern needs to be updated. The problem comes in that the saved asset paths on the Pattern entity being loaded and tested are saved as a full path from root on the server at the time of the pattern discovery, including the path to the Drupal root. In many site deployments this is fine and causes no issue since the Drupal root path is the same across all environments, but in environments where this may change and the database content is synced between them, attempts to load the assets will fail.

Example error output from this issue may look like this:

In SqlContentEntityStorage.php line 815:
Unable to decode the schema for pattern @my/pattern/name: Syntax error
In TwigPatternLibraryParser.php line 87:
Unable to decode the schema for pattern @my/pattern/name: Syntax error
In PatternLibraryJSONParserTrait.php line 181:
Syntax error

Steps to reproduce

  1. Install Patternkit, Patternkit Example, and Layout Builder modules
  2. Configure a Node type to display with Layout Builder and per-node layout customizations
  3. Create and save a new test Node
  4. Edit the layout for the new Node, and create a new [Patternkit] Example block with test content
  5. Save the layout
  6. Move the Patternkit Example module folder to a new location in the Drupal installation, such as modules/contrib/patternkit_example/
  7. Run the following Drush command to remove cached asset values:drush sqlq "UPDATE pattern_revision AS revision SET revision.hash='ALTERED', revision.version='OLD'"
  8. Rebuild all caches
  9. Attempt to view the test page
  10. Observe failed rendering for affected blocks

At this point, the resolution for this issue may be tested:

  1. Install the patch for this issue
  2. Run update hooks
  3. View the test page
  4. Expect to see the failed blocks loading successfully

Proposed resolution

  • Revise discovery and storage of all saved pattern entities to record asset paths without the prefixed Drupal root.
  • Update pattern asset loading to handle unprefixed paths and add the Drupal root at load time.
  • Implement an update hook to update the asset paths of all existing saved Pattern entities and remove the Drupal root from all paths.

Remaining tasks

User interface changes

None

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

9.1

Component

Module Core

Created by

πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas

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

Comments & Activities

  • Issue created by @slucero
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 10 months ago
    358 pass, 2 fail
  • @slucero opened merge request.
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 10 months ago
    359 pass
  • Status changed to Needs review 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 10 months ago
    359 pass
  • πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas

    I'm uploading a patch of the current state of the MR for convenient access. This may be applied in a composer.json file with the following snippet:

    {
      [...]
      "extra": {
            "patches": {
                "drupal/patternkit": {
                    "#3380008: Saved Pattern Entities Include Drupal Root In Cached Asset Paths": "https://www.drupal.org/files/issues/2023-09-08/patternkit-3380008--fix-cached-asset-paths--4.patch"
                }
            }
      }
    }
    
  • πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas
  • πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas

    Testing revealed that the testing instructions I originally posted don't successfully reproduce the issue. Below is a revised set of instructions that should reproduce the issue as expected:

    1. Install Patternkit, Patternkit Example, and Layout Builder modules
    2. Configure a Node type to display with Layout Builder and per-node layout customizations
    3. Create and save a new test Node
    4. Edit the layout for the new Node, and create a new [Patternkit] Example block with test content
    5. Save the layout
    6. Move the Patternkit Example module folder to a new location in the Drupal installation, such as modules/contrib/patternkit_example/
    7. Run the following Drush command to remove cached asset values:drush sqlq "UPDATE pattern_revision AS revision SET revision.schema=NULL, revision.template=NULL, revision.hash=NULL WHERE revision.revision IN (SELECT revision FROM pattern_revision)"
    8. Rebuild all caches
    9. Attempt to view the test page
    10. Observe failed rendering for affected blocks

    At this point, the resolution for this issue may be tested:

    1. Install the patch for this issue
    2. Run update hooks
    3. View the test page
    4. Expect to see the failed blocks loading successfully
  • Thanks for the thorough explanation and updating the instructions Stephen !!

    I tried to reproduce as well re-verify the issue. Result looks good to me now.

    Steps :

    • Install Patternkit, Patternkit Example, and Layout Builder modules
    • Configure a Node type to display with Layout Builder and per-node layout customizations
    • Create and save a new test Node
    • Edit the layout for the new Node, and create a new [Patternkit] Example block with test content
    • Save the layout
    • Move the Patternkit Example module folder to a new location in the Drupal installation, such as modules/contrib/patternkit_example/
    • Run the following Drush command to remove cached asset values.
    • drush sqlq "UPDATE pattern_revision AS revision SET revision.schema=NULL, revision.template=NULL, revision.hash=NULL WHERE revision.revision IN (SELECT revision FROM pattern_revision)"
    • Rebuild all caches
    • Attempt to view the test page
    • Observe failed rendering for affected blocks


    Recreation Results (Before Fix)

    Getting this error : Unable to decode the schema for pattern @my/pattern/name: Syntax error

    Retest Results (After Fix)
    Tested the issue with merge_requests/82 and failed block has been loaded successfully

  • Status changed to RTBC 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 9 months ago
    359 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 9 months ago
    359 pass
    • slucero β†’ committed 9bff8da0 on 9.1.x
      Issue #3380008 by slucero, minsharm: Saved Pattern Entities Include...
  • Status changed to Fixed 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas

    Merged for inclusion in the Beta 8 release: 🌱 Patternkit Beta 8 Release Plan Active

  • πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024