2

I'm trying to add a custom button in the sidebar of one of my entries.

I've found that it could be done in Craft 3 with this template hook: cp.entries.edit.details (e.g. How to add a custom button at the right section of an entry?)

In the Craft 4 upgrade documentation, it mentions that this hook has been removed. I found these two events - EVENT_DEFINE_EDITOR_CONTENT and EVENT_DEFINE_ADDITIONAL_BUTTONS - but so far, I haven't been able to return html to my template.

Is there an easy way to do it in Craft 4?

1 Answer 1

0

You'll want to use the EVENT_DEFINE_SIDEBAR_HTML event like so:

use craft\elements\Entry;
use craft\events\DefineHtmlEvent;
use craft\helpers\UrlHelper;
use yii\base\Event;

Event::on(
    Entry::class,
    Entry::EVENT_DEFINE_SIDEBAR_HTML,
    function(DefineHtmlEvent $event) {

        /** @var Entry $entry */
        $entry = $event->sender;

        // Make sure this is the correct section
        if ($entry->sectionId == 5)
        {
            // Return the button HTML
            $url = UrlHelper::url('some/path/' . $entry->id);
            $event->html .= '<a href="' . $url . '" class="btn">My Button!</a>';
        }

    }
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.