Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-source-drupal] Filters Not Applied for Entities Referenced in "translatableEntities" Config #38277

Open
2 tasks done
badaczewski opened this issue Jun 30, 2023 · 2 comments
Labels
status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer type: bug An issue or pull request relating to a bug in Gatsby

Comments

@badaczewski
Copy link

badaczewski commented Jun 30, 2023

Preliminary Checks

Description

When I associate a Drupal entity within the languageConfig -> translatableEntities array it causes my filters to no longer have an effect. Specifically my restriction of only displaying published content stops working.

Once I remove that entity reference from the list of translatableEntities the filter has an works again.

The root of the problem appears to be that the URL is passed in as a STRING and not an OBJECT. The code to apply filters checks for type object.

Reproduction Link

https://github.com/badaczewski/gatsby-multi/

Steps to Reproduce

  1. Apply Filter to Drupal Content Type.
  2. Add Content Type as a Translatable Entity.
  3. Create an Unpublished content entity for that type.
  4. Build Gatsby and notice that the unpublished content is still pulled even though filter is applied for "Published Only".
  5. Remove reference to as a translatableEntity.
  6. Build again, notice that the content is no longer applied and the filter is applied.
    {
      resolve: "gatsby-source-drupal",
      options: {
        baseUrl: `https://${process.env.DRUPAL_HOST}/`,
        apiBase: "jsonapi",
        filters: {
          "node--reference_page":"filter[status][value]=1"
        },
        fastBuilds: true,
        skipFileDownloads: true,
        imageCDN: false,
        languageConfig: {
          defaultLanguage: "en",
          enabledLanguages: ["en", "es"],
          translatableEntities: [
            "node--reference_page"
          ],
          nonTranslatableEntities: [],
        },
      },
    },

Expected Result

Only published Drupal content will be pulled in (i.e status=1)

Actual Result

Published and Unpublished content is pulled in.

Environment

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
  Binaries:
    Node: 18.13.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.9.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 7.24.0 - C:\Program Files\nodejs\npm.CMD
  Languages:
    Python: 3.10.10
  Browsers:
    Chrome: 114.0.5735.199
    Edge: Spartan (44.19041.1266.0), Chromium (114.0.1823.67)
  npmPackages:
    gatsby: ^5.5.0 => 5.11.0
    gatsby-cli: ^5.5.0 => 5.11.0
    gatsby-plugin-image: ^3.5.0 => 3.11.0
    gatsby-plugin-manifest: ^5.5.0 => 5.11.0
    gatsby-plugin-react-i18next: ^3.0.1 => 3.0.1
    gatsby-plugin-robots-txt: ^1.5.5 => 1.8.0
    gatsby-plugin-sass: ^6.5.0 => 6.11.0
    gatsby-plugin-sharp: ^5.5.0 => 5.11.0
    gatsby-plugin-sitemap: ^6.5.0 => 6.11.0
    gatsby-source-drupal: ^6.5.0 => 6.11.0
    gatsby-source-drupal-menu-links: ^3.0.1 => 3.0.1
    gatsby-source-filesystem: ^5.5.0 => 5.11.0
    gatsby-transformer-sharp: ^5.5.0 => 5.11.0
  npmGlobalPackages:
    gatsby-cli: 5.5.0

Config Flags

FAST_DEV: true,
DEV_SSR: true,
@badaczewski badaczewski added the type: bug An issue or pull request relating to a bug in Gatsby label Jun 30, 2023
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jun 30, 2023
@badaczewski badaczewski changed the title translatableEntities Override filters translatableEntities reference prevents JSON:API Filter for same entity reference from working Jul 1, 2023
@badaczewski
Copy link
Author

badaczewski commented Jul 5, 2023

It I've had success with changing LINE: 540 of gatsby-node.js in the plugin from

await getNext(joinedUrl, currentLanguage, filterByLanguages, renamedEnabledLanguages);

to

await getNext(new URL(joinedUrl), currentLanguage, filterByLanguages, renamedEnabledLanguages);

Making this change satisifies the OBJECT requirement on LINE 426 and filters get applied.

@badaczewski badaczewski changed the title translatableEntities reference prevents JSON:API Filter for same entity reference from working JSON:API Filters not Applied for Entities Referenced in "translatableEntities" Config Jul 6, 2023
@badaczewski badaczewski changed the title JSON:API Filters not Applied for Entities Referenced in "translatableEntities" Config [gatsby-source-drupal] Filters not Applied for Entities Referenced in "translatableEntities" Config Jul 6, 2023
@badaczewski badaczewski changed the title [gatsby-source-drupal] Filters not Applied for Entities Referenced in "translatableEntities" Config [gatsby-source-drupal] Filters Not Applied for Entities Referenced in "translatableEntities" Config Jul 6, 2023
@bb-nerd
Copy link

bb-nerd commented Jul 11, 2023

@badaczewski I ran into the same issue. I'm working around by pulling the filters check outside the if (typeof url === 'object') { since the filter logic assumes to be operating on a string anyway.

// Apply any filters configured in gatsby-config.js. Filters
        // can be any valid JSON API filter query string.
        // See https://www.drupal.org/docs/8/modules/jsonapi/filtering
        if (typeof filters === `object`) {
          if (filters.hasOwnProperty(type)) {
            url = new URL(url);
            const filterParams = new URLSearchParams(filters[type]);
            const filterKeys = Array.from(filterParams.keys());
            filterKeys.forEach(filterKey => {
              // Only add filter params to url if it has not already been
              // added.
              if (!url.searchParams.has(filterKey)) {
                url.searchParams.set(filterKey, filterParams.get(filterKey));
              }
            });
            url = url.toString();
          }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants