Coding and more – Concrete5, Flex, JavaScript

concrete5.7 – Custom Toolbar Button

concrete5 had an API to add a custom button to the inline editing toolbar. Here’s the article for the older 5.6 version: http://www.codeblog.ch/2012/04/concrete5-custom-toolbar-button/

In 5.7 a few things have changed. The idea is still the same, but you’ll have to use namespaces and loading classes is a bit different too.

Here’s how to get access to the menu helper:

1
$menuHelper = Core::make('helper/concrete/ui/menu');

You then have to call the addPageHeaderMenuItem method. Assuming you’ve already got a package, this is how the on_start method could look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public function on_start()
{
    $req = \Request::getInstance();
 
    // Make sure we don't inject our code if it's called by an AJAX request
    if (!$req->isXmlHttpRequest()) {
        /* @var $menuHelper \Concrete\Core\Application\Service\UserInterface\Menu */
        $menuHelper = Core::make('helper/concrete/ui/menu');
 
        $menuHelper->addPageHeaderMenuItem('ortic_btn', $this->pkgHandle, array(
            'icon' => 'question',
            'label' => t('Ortic'),
            'position' => 'right',
            'href' => 'http://www.ortic.com',
            'linkAttributes' => array('id' => 'ortic-button', 'target' => '_blank')
        ));
    }
}

Please note that we’re checking if the request is handled by an AJAX request. This can help to avoid collisions if you inject more things.

After that you’ll only have to create one more file in your package. Within “menu_items”, create a new folder with the button name, in my case “ortic_btn” (the first argument of addPageHeaderMenuItem ) and add a content like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
namespace Concrete\Package\OrticToolbar\MenuItem\OrticBtn;
 
class Controller extends \Concrete\Core\Application\UserInterface\Menu\Item\Controller
{
    /**
     * Return false if you don't want to display the button
     * @return bool
     */
    public function displayItem()
    {
        return true;
    }
}

Please note that “OrticToolbar” refers to my package.

You can find the complete example on github: https://github.com/ortic/ortic-button

Leave a Reply