Skip to content

Custom link attributes can be lost on save #262

@i-just

Description

@i-just

Describe the bug

An issue was raised in the cms repo which affects the Hyper plugin. The issue is that custom attributes (e.g., classes, link text, link title, etc.) are lost in some cases. I dug into it, and it looks like those values are lost when the Hyper field is considered empty.

This is the cms code that prevents empty values from being stored for elements that are new for the site: https://github.com/craftcms/cms/blob/5.8.22/src/services/Elements.php#L3984-L3988.

And this is why the link to entry with no entry selected is considered an emtpy field: Link::isEmpty() > ElementLink::count() > ElementLink::getElement().

Proposed changes:
It might be worth considering a similar approach as the native Link field uses - don’t store any additional values (link text, classes, etc) unless the link has a value in the first place - that way, there’s no confusion around those values not persisting in some cases.

It might also be a good idea to treat a link to an element as not empty when the selected element is disabled.

Steps to reproduce

  1. clean 5.8.22 installation with the Hyper plugin installed (I used version 2.2.11)
  2. create a Hyper field, with all the default settings and a “Default Link Type” set to “Entry”
  3. create a section with an entry type containing that Hyper field
  4. create an entry in that section, fill out the title and for the Hyper field, add a Link Text and nothing else; fully save that entry
  5. edit the entry from the previous step; the link text is still present; now edit the title of the entry; wait for the autosave to complete - a provisional draft has now been created
  6. if you reload the page or fully save that entry, the Link Text is gone

Craft CMS version

5.8.22

Plugin version

2.2.11

Multi-site?

no

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions