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

[5.x]: Link field URL will no longer accept a root-relative string. #15579

Closed
thisisjamessmith opened this issue Aug 22, 2024 · 27 comments
Closed
Labels

Comments

@thisisjamessmith
Copy link

thisisjamessmith commented Aug 22, 2024

What happened?

Description

Prior to 5.3 we could use root-relative links in the URL field type, now the new "Link" field.

image

On sites that use root-relative links extensively this will cause validation errors when attempting to save.

We don't want to use absolute URLs for internal links because they need to work on both dev and live environments, which use different TLDs.

We don't want to update all content to use element relationships because 1) Not everything on a given site is necessarily a URL-addressable Entry/Category/Asset, and 2) Your docs state that elements are not currently eager-loadable, and 3) it'll take ages on old sites that already have lots of populated URL fields.

Can we please just allow URLs that start with a slash to maintain backwards compatibility? (We'll have lots of Craft 3 & 4 sites to upgrade soon and this will be a show-stopper for those).

Craft CMS version

5.3.4

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

No response

@thisisjamessmith thisisjamessmith changed the title [5.x]: Link field URL will not accept a root-relative string. [5.x]: Link field URL will no longer accept a root-relative string. Aug 22, 2024
@jacobgraf
Copy link

I'm dealing with this as well. We didn't see anything mentioned in the release notes and didn't catch it in testing. Now we have it deployed to production and can't edit any of our entries that have URL (now Link) fields.

We are dead in the water until this is fixed.

We were told we could change those link fields to text fields but that doesn't seem right either.

I agree, a relative url starting with a slash should pass validation as it has for many years with the URL field type.

@thisisjamessmith
Copy link
Author

We're going to need this to be fixed for in-page anchors too (i.e., URIs starting with a #).

@jacobgraf
Copy link

Heck, even if they leave it as-is and just give us an advanced regex setting to override the validation logic, that would be fine, but we need something, and quick. We can't edit any entries currently. Argh.

@alexanderbuergin
Copy link

Yes, we have the same problem with the base url like /blog or /shop when the Page only has a fixed twig template and no entry.

The Anchor thing you could fix with an extra field, its not the nicest way but okay in some cases.

@jacobgraf
Copy link

This is a big deal for some of us that relied on relative URLs in this field type. This is a breaking change and there was no warning or anything in the release notes. If this won't be reverted, what is the best path forward here? For us, this is preventing us from adding a new product to our main navigation and it's a holiday week for us. It's a big deal.

@mandrasch
Copy link

mandrasch commented Aug 26, 2024

hey, I know this is not ideal - but have you already tried a temporary fix with Events and just disable the validation for Link field with type URL? E.g. craft\base\Field::EVENT_BEFORE_VALIDATE?

image

See https://craftcms.com/docs/5.x/extend/events.html#event-code-generator

@jacobgraf
Copy link

@mandrasch Hmm. I've not used Events before, but it looks promising. I'm going to read up on it a bit and see if CraftQuest or YouTube Has some videos explaining it a bit more. I'm not quite sure how to implement these events. Assuming I'd have to do it through a custom plugin?

@mandrasch
Copy link

@jacobgraf Yes, Plugin or Module (even easier). I would just use a module, you can generate one with ddev craft make module (via craft generator, see https://craftcms.com/docs/5.x/extend/module-guide.html). CraftQuest has a tutorial for modules as well, feel free to write here if you need help. 👍

@mandrasch
Copy link

(And if you want to use a subfolder for your module, check this comment - I was a bit confused in the beginning on how to use the generator correctly ;-))

@jacobgraf
Copy link

@mandrasch Cool. Thanks! I will play around, but just wish they would come out with an official fix. I'm annoyed that this breaking change was pushed out and wasn't even in the release notes. At a minimum, we should be able to override the default RegEx for these types of fields if we want our own validation rules to take over.

@mandrasch
Copy link

mandrasch commented Aug 26, 2024

@jacobgraf I understand the frustration, guess this will be addressed soon by the craft team. 🙂 (By the way - craft is very flexible, it might already be possible to override an existing validation rule and add your own regex. Just haven't found a detailed example yet for a normal field, but here is an example for overriding user field validation https://nystudio107.com/blog/extending-craft-cms-with-validation-rules-and-behaviors)

@brandonkelly
Copy link
Member

Sorry about this – support for root-relative URLs and anchors were overlooked when working on the Link field.

I’ve added new “Allow root-relative URLs” and “Allow anchors” settings to Link fields for Craft 5.4, which can be enabled to opt into allowing those.

If you need this right away, you can switch to the 5.4 branch by changing your craftcms/cms requirement in composer.json to:

"craftcms/cms": "5.4.x-dev as 5.4.0-alpha",

@jacobgraf
Copy link

@brandonkelly Thanks. I was mid-process of building a custom module. I'm a bit leery of opting into dev or alpha since this site is in production. A couple of quick questions.

  1. Is there a timeframe for 5.4 being released? Even a rough estimate? Days? Weeks? Months?
  2. I have this custom module built, but just need the event to temporarily just disable validation on all link fields, or allow relative URLs, whatever is easier. Do you know what the event would look like if I just wanted to do that for now until 5.4 releases and then I can disable the custom module?

I am thinking this is the event like @mandrasch mentioned above, but I'm not quite sure what to put into the function to get it to work how I want?

Event::on(
    Field::class,
    Field::EVENT_BEFORE_VALIDATE,
    function (ModelEvent $event) {
        
    }
);

Thanks again. Didn't want to be an annoyance, but this one has really put us in a bind for the last week.

@brandonkelly
Copy link
Member

I don’t think clearing out validation errors would actually work. There are internal changes to the Link field PHP and JS, plus the URL link type, that were required to get the fields actually handling root-relative and anchor links properly.

We’re going to fast track 5.4 for this and a couple other things that need to be released sooner than later – possibly as soon as later this week or next week.

@jacobgraf
Copy link

@brandonkelly Good to know. I have been told I need to get the content updated today as far as our site goes. Unless you can suggest some other work-around to be able to save my entries that have link fields on them, I may have to roll the dice on the dev release. I'll wait to hear back from you and then decide which path to take. Like I said, with a big holiday coming up and this being content for an e-comm site, I just need to get it working and then can wait for the official launch of 5.4 in stable.

@brandonkelly
Copy link
Member

@jacobgraf You could switch the field type to Plain Text for now, and back to Link once 5.4 is out. You’d just need to ensure your templates aren’t taking advantage of any of the new Link templating features.

@jacobgraf
Copy link

@brandonkelly Sounds good. I will do that then. We haven't refactored any code since the update so it would just be using the old URL field capabilities. Thanks again for all your help here.

@jacobgraf
Copy link

@brandonkelly All converted. So far, so good! I'll watch for the 5.4 official release. Thanks again!

@alexanderbuergin
Copy link

@brandonkelly Thanks. That is great!

@thisisjamessmith
Copy link
Author

Sorry about this – support for root-relative URLs and anchors were overlooked when working on the Link field.

I’ve added new “Allow root-relative URLs” and “Allow anchors” settings to Link fields for Craft 5.4, which can be enabled to opt into allowing those.

If you need this right away, you can switch to the 5.4 branch by changing your craftcms/cms requirement in composer.json to:

"craftcms/cms": "5.4.x-dev as 5.4.0-alpha",

Thanks for the fix Brandon. Out of curiosity though, why are we making options out of these? I can't think of any circumstance where anyone would want to limit editors to not use anchors or or relative links? Surely anything that can go in an href should automatically be accepted or at least it should be the default.

@brandonkelly
Copy link
Member

@thisisjamessmith Root-relative URLs and anchors have limited use compared to absolute URLs. You can’t include them in an RSS feed or HTML email body, for example.

@thisisjamessmith
Copy link
Author

thisisjamessmith commented Aug 29, 2024

@brandonkelly Fair enough! I'd really expect those kind of scenarios to be handled by a developer though - not sure it should be Craft's job to be opinionated in that respect. The situations where we'd use a dedicated Link field will generally (always?) be situations where we have control over the output (unlike a link button in a rich text field), so prepending a domain name to those links for external syndication scenarios would be straight-forward.

Are there any plans to allow the Link field's functionality inside Table fields? That's probably my most common usage of it (e.g. for outputting multiple button-style links). I notice that at the moment in a Table field it is still listed as a "URL" field, and has less functionality.

@brandonkelly
Copy link
Member

@thisisjamessmith Eventually yeah. Table fields are due for a major refactor which would involve letting them have custom column types, like Link.

@alexanderbuergin
Copy link

@thisisjamessmith: We have noticed that too. We look forward to P&T implementing this option.

@brandonkelly
Copy link
Member

Craft 5.4.0 is out with those changes.

@akukral
Copy link

akukral commented Sep 3, 2024

Only issue so far is editing multiple entries on the element view if the type is URL isn't possible.
image

This update is so very welcome and helpful for managing internal links and navigation.

@brandonkelly
Copy link
Member

@akukral That’s a separate bug. Can you post a new GitHub issue about it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants