spicywebau / craft-neo

A Matrix-like field type for Craft CMS that uses existing fields
Other
402 stars 63 forks source link

Continued Live Preview nesting issues #367

Closed rbaud closed 4 years ago

rbaud commented 4 years ago

Description

We are continuing to face an issue in Neo 2.7.15 and Craft 3.4.22.1 where making changes to an Entry with a Neo field (while in Live Preview) causes deeply-nested blocks to break out of their nesting. This is likely related to some or all of the following issues: #305, #323, #325, #356 & #364.

This issue makes editing via Live Preview, a critical tool in our workflow and part of the appeal of Craft itself, unpredictable and therefore unusable.

Here is what we have discovered:

  1. This issue specifically affects changes made while using Live Preview. Making identical changes as described below OUTSIDE of Live Preview does not cause the issues we are describing.

  2. This issue definitely affects Neo structures with 4 levels of nesting, but I have not exhaustively tested issues at lower/higher depths.

Steps for reproducing:

  1. Have a Neo structure with 4 levels of depth and at least two level-3 parents with at least one 4th/final level children each, like so:
    -L1
    --L2
    ---L3-A
    ----L4-X
    ---L3-B
    ----L4-Y
  2. Open Live Preview and make ANY change that will cause the preview to refresh. Do this twice. This could be as simple as collapsing L1 and then expanding it again. It is not necessary for the changes to in any way affect the structure/content of the Neo field or affected blocks.

The first Live Preview refresh/reload/update will be fine. For us, the second throws an "Unable to find one of the following templates" error (this is because our Neo uses different templates at different levels), which is an indication that the nesting/structure has changed.

  1. Close Live Preview, make note that as yet the nesting/structure in the control panel has not changed, and then Publish changes.

  2. After the control panel has refreshed, you should notice that the structure is now broken like so:

    -L1
    --L2
    ---L3-A
    ----L4-X
    **----L3-B**
    **----L4-Y**

    Other information

    • Neo version: 2.7.15
    • Craft version: 3.4.22.1
    • Is eager-loading used? No
rbaud commented 4 years ago

I have just tested this with Neo 2.7.15 and Craft 3.4.22.1 on a fresh install with no other plugins. The issue exists under these conditions, and I have recorded the results of my test below.

  1. First, I setup a Neo field with 4 levels of nesting. Each level can only contain the next level (e.g. L1 only can contain L2). I added this field to the "Homepage" Single Section.

  2. I then created an index template that displays the following recursive code, based on the sample in the docs:

    <ol>
      {% nav block in entry.neoField.all() %}
          <li>
              {{ block.level }}
              {% ifchildren %}
                  <ol>
                      {% children %}
                  </ol>
              {% endifchildren %}
          </li>
      {% endnav %}
    </ol>
  3. Next, I added the following blocks and opened Live Preview.

    -L1
    --L2
    ---L3-A
    ----L4-X
    ---L3-B
    ----L4-Y
    ----L4-Z

On load, Live Preview displayed the following, as expected: image

  1. I then collapsed the L1 block, which yielded no effect, and then expanded it again (both times via double-clicking the block's top bar), which yielded this: image

  2. I closed Live Preview, which displayed the Neo structure as it was originally created: image

  3. And finally, I published the changes, which refreshed the page and displayed the new, broken structure/nesting: image

Tim-Wils commented 4 years ago

I have experienced this to last week! Unfortunately it makes the plugin unusable for clients to work with. Let's hope this will be fixed soon.

ttempleton commented 4 years ago

I've been testing with Neo 2.7.16 and Craft 3.4.24, and have been unable to reproduce the issue -- can you update both and let me know if it still happens?

rbaud commented 4 years ago

Ok, I've tried a couple of things:

First, I rolled up a 100% fresh install with an empty test database via composer create-project and ./craft setup, installed Neo 2.7.16 via the Plugin Store, setup a Home Entry and Neo field as I did above, and couldn't reproduce the issue.

Next, I tried upgrading from Neo 2.7.15 and Craft 3.4.23 using the following steps:

1) Removed all tables in the test DB, cleared the /vendor folder, deleted composer.lock, and ran composer install and ./craft setup after adjusting composer.json like so:

{
  "require": {
    "craftcms/cms": "3.4.23",
    "spicyweb/craft-neo": "2.7.15",
    "vlucas/phpdotenv": "^3.4.0"
  },
  "require-dev": {
    "yiisoft/yii2-shell": "^2.0.3"
  },
  "autoload": {
    "psr-4": {
      "modules\\": "modules/"
    }
  },
  "config": {
    "sort-packages": true,
    "optimize-autoloader": true,
    "platform": {
      "php": "7.0"
    }
  },
  "scripts": {
    "post-root-package-install": [
      "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ]
  }
}

2) Installed Neo 2.7.15 via Settings > Plugins, setup the Home Entry and Neo field as above, and was able to reproduce the issue.

3) I then manually fixed the structure/nesting and resaved the Entry before upgrading to Craft 3.4.24 and Neo 2.7.16 via the web Updater.

4) I then went back to the Home Entry and, following the steps in Live Preview I detailed above, was able to reproduce the issue.

I took snapshots of the database all along the way. Please let me know where to send them if they'd be helpful.

ttempleton commented 4 years ago

Thanks for that, @rbaud, with that information I could reliably reproduce the issue for entry drafts in general, not just with live preview.

So, when Craft prepares to save the draft, part of Craft's preparation of the draft data to be POSTed involves replacing original element IDs with their duplicates' IDs. Because of the way Craft's element ID replacing needs to work, and the way Neo's form data is formatted, Neo block levels could get caught up in this process (Neo's form data is formatted in pretty much the same way as Matrix, but Matrix obviously doesn't have levels, or any other properties that could be affected by this, as far as I can see).

In my case, and I assume in your case as well, the homepage has element ID 2. So any blocks at level 3 (in the block form data, levels start from 0) were getting their level replaced with the element ID of the draft, which will almost certainly be far higher than 3.

Fortunately, adding a leading 0 to the level in the block form data is sufficient to avoid Craft's ID replacing and fix the issue, and this fix has been released in Neo 2.7.17.

Tim-Wils commented 4 years ago

Looks like the leading 0 fix works :) thanks Thomas!

rbaud commented 4 years ago

@ttempleton, 2.7.17 fixes this issue for us across our sites. Thank you for addressing this!