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

Add extension points to Link::getURL() #368

Closed
4 tasks done
Cambis opened this issue Mar 5, 2025 · 4 comments
Closed
4 tasks done

Add extension points to Link::getURL() #368

Cambis opened this issue Mar 5, 2025 · 4 comments

Comments

@Cambis
Copy link
Contributor

Cambis commented Mar 5, 2025

Description

I'm migrating a project off gorriecoe/silverstripe-link. That module provides an extension point to update the url of the link which we use, it would be nice to have that functionality here as well. See the original extension point here.

Additional context or points of discussion

No response

Validations

  • You intend to implement the feature yourself
  • You have read the contributing guide
  • You strongly believe this feature should be in core, rather than being its own community module
  • You have checked for existing issues or pull requests related to this feature (and didn't find any)
@GuySartorelli
Copy link
Member

GuySartorelli commented Mar 6, 2025

What's the use case for that extension hook? The only use cases I can think of would be better served as onBeforeWrite() but I'm probably just missing something obvious.

@Cambis
Copy link
Contributor Author

Cambis commented Mar 6, 2025

No worries :)

My particular use case is adding an subject field to the EmailLink. The subject field is added via an extension. The url of the link should be something like mailto:test@example.com?subject=Subject.

I'm not keen on using onBeforeWrite because I want to keep the Email and EmailSubject fields separate.

The other thing I can do in this case is create a custom EmailLink, or replace the original one via the injector. I thought that might be overkill since I'm just adding a single field.

Keen to hear any alternative solutions as well :)

Here's my extension code with the hook

/**
 * @extends Extension<(EmailLink & static)>
 *
 * @property ?string $EmailSubject
 */
class SubjectExtension extends Extension
{
    private static array $db = [
        'EmailSubject' => 'Varchar(255)',
    ];

    protected function updateCMSFields(FieldList $fields): void
    {
        $fields->addFieldToTab(
            'Root.Main',
            TextField::create(
                'EmailSubject',
                'Email subject'
            )
        );
    }

    protected function updateGetURL(string &$url): void
    {
        $emailSubject = $this->getOwner()->EmailSubject;

        if ($emailSubject === null || $emailSubject === '') {
            return;
        }

        $url = sprintf('%s?subject=%s', $url, $emailSubject);
    }
}

@GuySartorelli
Copy link
Member

Makes sense, that's a very good use case for an extension hook. Thanks for being patient with me and explaining it so well

@GuySartorelli
Copy link
Member

PR merged. This will be included in the April minor release.

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

No branches or pull requests

2 participants