0

Please excuse me for the title.. I have no idea how to describe my problem better - if someone knows a better description for this question feel free to edit it.

I made a screenshot to clarify what I want to know.. Is it somehow possible to hook a custom option there?
enter image description here

I made an excel export for our customer and I would like to add an "export to excel" button there that sends an ajax request to my controller. Could you please explain me in detail how to do this?

Thank you very much

Edit
So thanks to your answers I created this test (just to see if I can add something)

<?php

namespace Craft;

class SRExcelExport_ExportElementAction extends BaseElementAction
{
    /**
     * Get element action name.
     *
     * @return string
     */
    public function getName()
    {
        return Craft::t('Export Files');
    }

    public function isDestructive()
    {
        return true;
    }

    public function performAction(ElementCriteriaModel $criteria)
    {
        // Remove all the ingredients from the selected elements
        $elements = $criteria->find();

        foreach ($elements as $element)
        {
            //do something
        }

        // Success!
        $this->setMessage(Craft::t('success'));
        return true;
    }

    public function getTriggerHtml()
    {
        $js = 'new Craft.ElementActionTrigger({' .
            'handle: '.JsonHelper::encode($this->getClassHandle()).', ' .
            'batch: true, ' .
            'validateSelection: function($selectedElements){ return true; }, ' .
            'activate: function($selectedElements){ alert("Made you click"); }' .
            '});';

        craft()->templates->includeJs($js);
    }
}

With the Plugin Handle SRExcelExport with the folder structure
srexcelexport
--elementactions
-----SRExcelExport_ExportElementAction.php

But nothing happens, the file is ignored, even a die() in the constructor does nothing. Do I need to do something to "call" the class myself or is there just a stupid error I made? I bet its just an obvious minor error I can't see right now

0

1 Answer 1

3

Indeed there is! You're looking for Element Actions...

When you select elements on their index pages, the regular toolbar options get replaced with new ones that will trigger various actions, like batch-deleting all of the selected elements.

That functionality is provided by Element Action classes.

6
  • This commit might be a helpful example for how to execute some lines of JavaScript directly from the getTriggerHtml method.
    – carlcs
    Commented Apr 11, 2017 at 7:05
  • thanks for that fast answer. Could you please read my edit? I copied the code from pixel and tonic - just to see how it works as a first test but it seems like the file is ignored. Do you see my mistake? Commented Apr 11, 2017 at 7:39
  • Did you have a look at the code I posted @anubarak?
    – carlcs
    Commented Apr 11, 2017 at 7:41
  • Yes of course, it does not work as well (I changed the pluginhandles to mine) edit: everything else in my plugin works - so its not a general problem Commented Apr 11, 2017 at 7:46
  • @anubarak so you have registered the element action in your main plugin class?
    – carlcs
    Commented Apr 11, 2017 at 7:53

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.