verbb / hyper

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

Additional helper for setting link text and defaults #140

Closed gateszies closed 1 month ago

gateszies commented 2 months ago

What are you trying to do?

I'm migrating from Typed Link and am having difficulty handling link text without writing if/else statements for different link types.

Right now, there is text which returns the custom text or element title, but I lose the ability to override with a default. Ex. for URL link types, text will return as the URL string. Because text has a value, I can't override with a default (and don't want the button text to be /product/example).

There is also linkText which will always use the custom inputted text, but I lose the ability to fall back to the element title if the custom text isn't defined.

What's your proposed solution?

I would love a functionality similar the TypedLink helper function getText(). This gives the ability to pass in more intelligent defaults for link text which from my understanding currently aren't possible in Hyper without writing several if/and statements based on Link Type. The logic for getText() applies the link text as follows:

By using logic similar to the getText() helper, I'd have greater flexibility to use custom text if its there, fallback to the element title if not, and finally use a default if there is neither custom text or an element title exists (great for URL link types)

Additional context

No response

engram-design commented 2 months ago

So getText() should work like that In Hyper, if you call it like a getter, but I can see there's an issue with that.

Fixed for the next release. To get this early run composer require verbb/hyper:"dev-craft-4 as 1.1.27"

gateszies commented 2 months ago

@engram-design Amazing, thanks for getting back to me so quickly 🎉 This is now working as expected.

umkasanki commented 1 month ago

Is it possible to get the default text defined in a placeholder? Thank you. изображение

engram-design commented 1 month ago

Updated for the next release. To get this early, run composer require verbb/hyper:"dev-craft-4 as 1.1.29".

gateszies commented 1 month ago

Has any functionality for getText() changed in v2.0.0? We just updated to Craft 5 and Hyper 2.0.0 and instead of buttons grabbing from the default text defined in the code option.urlLink.getText('Shop Now') they're displaying the text Read more which isn't defined anywhere by us.

It looks like the native field linkText has a Placeholder field that can be defined in its settings. The field is auto-populated with e.g. Read more and that value remains even after deleting it in the field settings. I wonder if because this field can't be truly empty, the default defined in the template with getText('Shop Now') isn't properly overriding it?

Screen Shot 2024-05-29 at 9 48 51 AM
gateszies commented 1 month ago

I believe the issue lies here with how the Default Text is defined.

Typically I wouldn't expect Placeholder text to be used for anything on the frontend, but I see it's used as Default Text if the Link Text isn't defined. That also looks like where the Read more fallback text is coming from, which is an issue when you cannot truly delete e.g. Read More.

It'd be helpful if default text was a separate field in Link Text to separate the concerns of default text and placeholder text. It'd also be beneficial to get back the functionality of getText('default text here') to define default text in the template.

engram-design commented 1 month ago

There were indeed some unexpected breaking changes to getText() but they should be resolved in the dev-craft-5 and dev-craft-4 branches. These changes were to fix some issues with URL fields that used their link as the text for the link, which was admittedly pretty gross. A default text was introduced, but incorrectly a bit heavy-handed and a breaking change (it shouldn’t have been).

Using the placeholder text was requested to be used. I can see value in having a default value used separately.

Being unable to edit the placeholder is also fixed.

I should note calling getText('Some Text'); is the default text and won’t override any text derived from the link. This only really happens on element-based links. So the title of the element will take precedence over text to provide. Then, any text you define in the Link Text field will override that.

For URL links, it seemed silly to not render a link just because it didn’t have text. We initially used the url value as the text, but that’s kind of rare in practice with content. But I’ll admit that’s more of an opinionated choice on our end.

engram-design commented 1 month ago

Updated in 2.0.1 and 1.1.31