verbb / hyper

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

[dev-craft-5]: When trying to create a link programatically, it always fallbacks to the default type #109

Closed WHITE-developer closed 8 months ago

WHITE-developer commented 9 months ago

Describe the bug

When trying to set the links programatically the link type always is set to the default link type selected https://verbb.io/craft-plugins/hyper/docs/developers/link#creating-links-programatically

$button = new \verbb\hyper\links\Url();
$button->linkText = 'text';
$button->linkValue = 'http://white.nl';
$button2 = new \verbb\hyper\links\Entry();
$button2->linkValue = 3483;
$entry->setFieldValue('ctaButtons', [
        $button,
        $button2,
]);
Craft::$app->getElements()->saveElement($entry);

Depending on how I set the default link type, both buttons will be of that type

Steps to reproduce

  1. Save the field value programatically with a non default link type
  2. The link type will be the default link type

Craft CMS version

5.0.0-alpha.8

Plugin version

dev-craft-5 801a741

Multi-site?

Yes

Additional context

No response

engram-design commented 9 months ago

This is probably more of a documentation failure than anything. It should affect Craft 4 as well.

The reason this is tricky at the moment is that a Hyper field can contain multiple link types of the same class. You can have multiple "Entry" link types, all labelled differently. As such, you'd tell Hyper which one to use:

$button->handle = 'my-link';

However, the handle for a link type isn't exactly exposed anywhere in the UI, so it makes it difficult to actually know what value to use.

As such, in lieu of adding handles to link types (yet), Hyper will now auto-pick the first available link type for the class that you provide. This is going to cover 99% of people's usage, where they don't have any new additional link types to the default ones.

You can get this in the craft-5 branch.

It also goes without saying that we're yet to tag a Craft 5 release as we're still working on the Craft 5 alpha changes until it reaches beta. Just be wary of changes!

engram-design commented 8 months ago

Updated in 2.0.0-beta.1