TypeError: Drupal\single_content_sync\ContentExporter::isReferenceCached()

Created on 11 January 2025, 3 months ago

Problem/Motivation

TypeError: Drupal\single_content_sync\ContentExporter::isReferenceCached(): Argument #1 ($entity) must be of type Drupal\Core\Entity\FieldableEntityInterface, null given, called in /var/www/docroot/modules/contrib/single_content_sync/src/Plugin/SingleContentSyncFieldProcessor/TextField.php on line 139 in Drupal\single_content_sync\ContentExporter->isReferenceCached() (line 196 of modules/contrib/single_content_sync/src/ContentExporter.php).

Steps to reproduce

  1. Add a link using a reference to a node either through a link field or in CKEditor via Entity embed.
  2. Export the entity

It seems like some links in my content throw an error when trying to export.

Heres some info on what my $linked_entities looks like;

Drupal\node\Entity\Node^ {#6981
  #entityTypeId: "node"
  #enforceIsNew: null
  #typedData: null
  #cacheContexts: []
  #cacheTags: []
  #cacheMaxAge: -1
  #_serviceIds: []
  #_entityStorages: []
  #values: array:25 [
    "nid" => array:1 [
      "x-default" => "3856"
    ]
    "vid" => array:1 [
      "x-default" => "4371"
    ]
    "type" => array:1 [
      "x-default" => "basic_page"
    ]
    "uuid" => array:1 [
      "x-default" => "a5839a75-1c05-4a9e-b827-0f890ed3c15b"
    ]
    "langcode" => array:1 [
      "x-default" => "en"
    ]
    "revision_default" => array:1 [
      "x-default" => "1"
    ]
    "revision_uid" => array:1 [
      "x-default" => "1"
    ]
    "revision_timestamp" => array:1 [
      "x-default" => "1494264346"
    ]
    "revision_log" => array:1 [
      "x-default" => null
    ]
    "isDefaultRevision" => array:1 [
      "x-default" => "1"
    ]
    "title" => array:1 [
      "x-default" => "How to Apply"
    ]
    "uid" => array:1 [
      "x-default" => "1"
    ]
    "status" => array:1 [
      "x-default" => "1"
    ]
    "created" => array:1 [
      "x-default" => "1493923655"
    ]
    "changed" => array:1 [
      "x-default" => "1724071673"
    ]
    "promote" => array:1 [
      "x-default" => "0"
    ]
    "sticky" => array:1 [
      "x-default" => "0"
    ]
    "revision_translation_affected" => array:1 [
      "x-default" => "1"
    ]
    "default_langcode" => array:1 [
      "x-default" => "1"
    ]
    "rh_action" => array:1 [
      "x-default" => "bundle_default"
    ]
    "rh_redirect" => array:1 [
      "x-default" => null
    ]
    "rh_redirect_response" => array:1 [
      "x-default" => "301"
    ]
    "rh_redirect_fallback_action" => array:1 [
      "x-default" => null
    ]
    "field_main_region" => array:1 [
      "x-default" => array:1 [
        0 => array:2 [
          "target_id" => "4681"
          "target_revision_id" => "5376"
        ]
      ]
    ]
    "field_metatags" => array:1 [
      "x-default" => array:1 [
        0 => array:1 [
          "value" => "a:0:{}"
        ]
      ]
    ]
  ]
  #fields: []
  #fieldDefinitions: null
  #languages: null
  #langcodeKey: "langcode"
  #defaultLangcodeKey: "default_langcode"
  #activeLangcode: "x-default"
  #enforceDefaultTranslation: null
  #defaultLangcode: "en"
  #translations: array:1 [
    "x-default" => array:1 [
      "status" => 1
    ]
  ]
  #translationInitialize: false
  #newRevision: false
  #isDefaultRevision: "1"
  #entityKeys: array:4 [
    "bundle" => "basic_page"
    "id" => "3856"
    "revision" => "4371"
    "uuid" => "a5839a75-1c05-4a9e-b827-0f890ed3c15b"
  ]
  #translatableEntityKeys: array:8 [
    "label" => array:1 [
      "x-default" => "How to Apply"
    ]
    "langcode" => array:1 [
      "x-default" => "en"
    ]
    "status" => array:1 [
      "x-default" => "1"
    ]
    "published" => array:1 [
      "x-default" => "1"
    ]
    "uid" => array:1 [
      "x-default" => "1"
    ]
    "owner" => array:1 [
      "x-default" => "1"
    ]
    "default_langcode" => array:1 [
      "x-default" => "1"
    ]
    "revision_translation_affected" => array:1 [
      "x-default" => "1"
    ]
  ]
  #validated: false
  #validationRequired: false
  #loadedRevisionId: "4371"
  #revisionTranslationAffectedKey: "revision_translation_affected"
  #enforceRevisionTranslationAffected: []
  #isSyncing: false
  +in_preview: null
}

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

1.4

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada AaronChristian Kelowna, BC

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

Merge Requests

Comments & Activities

  • Issue created by @AaronChristian
  • πŸ‡ΊπŸ‡¦Ukraine nginex
  • I tested with both links and Entity embeds

    Drupal core version 10.2.6
    Entity embed 1.6.0
    Single Content Sync 1.4.8

    I was not able to reproduce this issue

  • πŸ‡¨πŸ‡¦Canada AaronChristian Kelowna, BC

    Thanks for testing @borg_git

    For some reason I'm still encountering this error, even outside of CKEditor.

    Here's a menu link im trying to export.

  • πŸ‡¨πŸ‡¦Canada AaronChristian Kelowna, BC
  • @aaronchristian please clarify what version of the module you are using and what patches have been applied.
    After all, this function "isReferenceCached" was added starting from version "1.4.8", and is located on line "123" of the file "TextField.php"

  • πŸ‡¦πŸ‡ΊAustralia jannakha Brisbane!

    I can confirm this issue exists.
    I've got the same issue on 1.4.10 (upgraded from 1.4.8 - which worked fine)
    doesn't happen on all content types, I'm investigating which field causing the issue

  • πŸ‡¦πŸ‡ΊAustralia jannakha Brisbane!

    I found why this issue is happening! (unfortunately, CKEditor is editable by users and they can copy paste things as they please)!

    Steps to reproduce:

    1. On a content node (article) insert into a text field's CKEditor source (a link to non-existent node):

    <a href="/node/21991" target="_blank" data-entity-type="node" data-entity-uuid="30d77877-0000-0000-0000-14589bc25583" data-entity-substitution="canonical"><em><strong>HERE</strong></em></a>
    

    2. Save node
    3. Go to Export tab of this node
    4. It will crash with same exception
    TypeError: Drupal\single_content_sync\ContentExporter::isReferenceCached():

    Proposed solutions:
    - ignore invalid links, if $linked_entity is NULL don't try to add to $embed_entities
    - report an error - link doesn't exist (so users will fix it and then do export)

    Users are silly - they'll put all sorts of stuff into text fields, so

  • πŸ‡¦πŸ‡ΊAustralia jannakha Brisbane!
  • Pipeline finished with Success
    2 months ago
    Total: 174s
    #416975
  • πŸ‡¨πŸ‡¦Canada AaronChristian Kelowna, BC

    Thanks for looking into this one @jannakh.

    I had a few patches applied previously so my errors line numbers might have been off, I removed them and started with a fresh install of 1.4.10 and am still noticing a failure on export. The patch seems to address import, and that works as expected, but on the export it fails.

    I have a legacy site that has a lot of nodes with a number of paragraphs with links in each of them. When I uncomment lines 116-134 and run the export it actually processes the page and when it finally finishes loading I see it loads all the nodes and their fields, so my file for a single page was over 19MB!

    I've attached a really basic file you can test with to reproduce the issue.

    This is just menu_link_content, but for some reason it will load the whole referenced node and all its fields. Is it necessary to load all the embedded entities and their field values for links?

    Thanks!

  • πŸ‡ΊπŸ‡¦Ukraine nginex

    > This is just menu_link_content, but for some reason it will load the whole referenced node and all its fields. Is it necessary to load all the embedded entities and their field values for links?

    at the moment it exports full entity if it was not exported yet, otherwise it will export base fields only. So in your generated file you should not see the same entity exported multiple times including all fields.

    Anyway, I really want to improve this in ✨ Allow to partially export content Active to reduce size of generated files and just export what is really needed instead of everything

  • First commit to issue fork.
  • Pipeline finished with Failed
    about 2 months ago
    Total: 233s
    #430808
  • πŸ‡§πŸ‡·Brazil igorgoncalves

    Hey guys, i was able to reproduce this issue with:

    • Drupal: 10.4.2
    • PHP version: 8.2.18
    • single_content_sync: 1.4.10
    • entity_reference_revisions: 8.x-1.12

    also, the file @aaronchristian attached indeed import with no problem, but on export the issue happens.

    So, after debugging i find out the issue was inside the exportCustomValues, and following the bug trail i realize the field entity_reference_revisions was unsupported, and when getFieldValue was called we'v got into a kind of loophole.

    The solution i found was to check if the field has that specific unsupported type and then 'get the value as it is' as the previous code comment says.

  • Pipeline finished with Success
    about 2 months ago
    Total: 219s
    #430824
  • πŸ‡¨πŸ‡¦Canada AaronChristian Kelowna, BC

    @igorgoncalves

    This worked really well, thanks for making this a priority this week my friend!

    I tested this on all the various places I was able to crash the export previously and they all seem to work as expected.

    I'll keep my eye on the "partial export" feature, I think that will be the game changer for this module.

    Thanks for the exception module, its always been a HUGE pain point of Drupal, I'm glad there are people out there pushing initiatives like this through for the community.

    I'll leave this one open for a bit for others to test with the supplied file I posted above, and then with the patch in place.

    Looks good from my side, I would be happy to mark this one as RTBC in the next few days as I work through some more use cases.

  • πŸ‡¨πŸ‡¦Canada AaronChristian Kelowna, BC

    Did a bit more testing, looks like some paragraphs don't get included on the export of one of my content blocks.

    Lets take this one back as it looks like it needs a bit more work!

  • πŸ‡ΊπŸ‡¦Ukraine nginex

    let me try to take a look closer

  • πŸ‡ΊπŸ‡¦Ukraine nginex

    @aaronchristian

    Could you please test a new patch I created in MR 152. Let me know if you still get errors during export.

    • nginex β†’ committed 38cb2e21 on 1.4.x
      Issue #3498972: Avoid error during export of text with invalid link to...
  • πŸ‡ΊπŸ‡¦Ukraine nginex
  • Status changed to Fixed 15 days ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024