verbb / hyper

A Craft CMS plugin for managing links, focusing on user experience.
Other
25 stars 15 forks source link

Nested hyper fields not saving #217

Open corneliusio opened 2 days ago

corneliusio commented 2 days ago

Describe the bug

We've suddenly started having an issue where nested hyper fields are not updating.

Steps to reproduce

We currently have the following structure for a set of fields to allow for a site's main navigation:

To break it down fully, we have a matrix field with two types, "Dropdown" and "Menu Item". The "Dropdown" matrix type has a plain text field for the dropdown label in the menu and a Hyper field that can be populated with standard link types as well as a custom one that allows for an additional layer of navigation links. This custom link type uses the default "Link Text" input for it's label and has a different hyper field that populates this dropdown (this hyper field does not have an additional "dropdown" type so the nesting stops here).

Now there are a couple things happening here that are odd:

  1. The link value input of the custom Hyper "Dropdown" link type will automatically populate with a serialized array of ids that appear to correspond to the element ids of it's child hyper field.
    1. I originally expected I'd need to add an event handler before save or validation to populate this input with something like "#" just to allow the field to pass validation, but this never ended up being the case due to this behavior.
  2. No data for hyper related inputs or child hyper fields will update in the custom "Dropdown" link type except the type itself. This plays out as follows:
    1. If update the "Link Text" field for the "Dropdown" type, it reverts on save.
    2. If I add a child link to the "Dropdown" type, it's values are lost on save.
    3. If I update an existing URL child link to be an Entry type, the link will remain an Entry type after save but with no element attached.
    4. If I updated an existing child link Entry type to a URL, the URL value will be lost and a serialized array with a single id associated with the element assigned to the link before save will populate the link value input.

Craft CMS version

5.5.3

Plugin version

2.2.1

Multi-site?

No

Additional context

I'm more than happy to pass along project config files if it would help.

engram-design commented 10 hours ago

Should be fixed for the next release. To get this early, run composer require verbb/hyper:"dev-craft-5 as 2.2.1".

corneliusio commented 2 hours ago

@engram-design Fantastic! That's fixed the issues for sure. I am a bit curious about this part:

The link value input of the custom Hyper "Dropdown" link type will automatically populate with a serialized array of ids that appear to correspond to the element ids of it's child hyper field. I originally expected I'd need to add an event handler before save or validation to populate this input with something like "#" just to allow the field to pass validation, but this never ended up being the case due to this behavior.

This is no longer occurring which makes sense to me, however when I go to implement the solution I'd mentioned it doesn't seem to work:

Event::on(
    Entry::class,
    Entry::EVENT_BEFORE_VALIDATE,
    function (YiiModelEvent $event): void {
        /** @var Entry $block */
        $block = $event->sender;

        if ($block->type->handle === 'dropdown') {
            foreach ($block->menu as $menu_item) {
                if ($menu_item->label === 'Dropdown') {
                    // none of the following appear to work. no matter how 
                    // I try to programmatically populate this field, 
                    // I get a `Link cannot be blank.` validation error.
                    $menu_item->setAttributes([ 'linkValue' => '#' ], false);
                    $menu_item->setAttribute('linkValue', '#');
                    $menu_item->linkValue = '#';
                }
            }
        }
    },
);

Is there any way to bypass the validation for the link if needed? I know I'm stretching the functionality here a bit but I'd thought I'd gotten away with one since it was working for quite some time.

Also, while messing with this I came across another issue. The following line references an undefined variable $item which I'm assuming should be $value: https://github.com/verbb/hyper/blob/craft-5/src/models/LinkCollection.php#L134