0

How do you load a custom element action using getAvailableActions inside my Element Type class?

public function getAvailableActions($source = null)
{
    $customAction = craft()->elements->getAction('Custom');

    $customAction->setParams(array(
        'label' => Craft::t('Custom'),
    ));

    $actions[] = $customAction;
}

The above renders a PHP error PHP Fatal error: Call to a member function setParams() on a non-object

PluginName_CustomElementAction is my file inside the elementactions folder, inside my plugin. Which is copied directly from https://craftcms.com/docs/plugins/element-actions

Here's what it looks like:

namespace Craft;
class PluginName_CustomElementAction extends BaseElementAction
{
    public function getName()
    {
        return Craft::t('Custom');
    }

    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)
        {
            craft()->cocktailRecipes->removeAllIngredientsFromElement($element);
        }

        // Success!
        $this->setMessage(Craft::t('Ingredients removed successfully.'));
        return true;
    }
}

What is wrong with my getAvailableActions object?

  • What's the code in your getAvailableActions method? It'd probably help to share what's in your main element class as well. – Brad Bell Mar 6 '16 at 5:25
  • @BradBell- just edited the first part to show the function. I can share the full file/plugin privately if this isn't enough. – Adam McCombs Mar 6 '16 at 6:15
2

I think your issue may be that the ElementsService getAction method expects the full classname for your custom ElementAction (sans the "ElementAction" suffix), i.e.

$customAction = craft()->elements->getAction('PluginName_Custom');
| improve this answer | |
1

I'd recommend to follow the guide in the docs. https://craftcms.com/docs/plugins/element-actions

Update: There's a dedicated section about "Adding actions to index pages".

If your Element Action is intended for your own custom Element Type, you can do that by implementing the getAvailableActions() method on your Element Type class.

The link takes you to the method's documentation.

/**
 * Returns the available element actions for a given source (if one is provided).
 *
 * The actions can either be represented by their class handle (e.g. 'SetStatus'), or by an
 * instance.
 *
 * @param string|null $source The selected source’s key, if any.
 *
 * @return array|null The available element actions.
 */
public function getAvailableActions($source = null);

It's also explained how to implement actions for Craft’s built-in Element Types.

[…] Craft’s built-in Element Types: addEntryActions, addCategoryActions, …

There's example code provided if you click those links.

public function addEntryActions($source)
{
    return array(
        'Foo',
        new BarElementAction(),
    );
}
| improve this answer | |
  • Yep. That was copied into PluginName_CustomElementAction. My question actually centers around getAvailableActions and why my custom action isn't an object. – Adam McCombs Mar 5 '16 at 20:15
  • Only wanted to help. Sorry. – carlcs Mar 5 '16 at 20:16
  • Very kind reaction of you down voting me and my attempt to do so! – carlcs Mar 5 '16 at 20:20
  • Didn't mean to offend but I don't think it's relevant unless there is an issue with the action file. This isn't documented very well and I used what was made available. – Adam McCombs Mar 5 '16 at 20:24
  • So you really want to tell me, the element actions guide with a dedicated section about "Adding actions to index pages" isn't relevant? There's links to the methods with further explanations and example code, it states you should return an array of IElementAction instance or the handles representing them. But yeah, not relevant, even misleading so we better down vote! – carlcs Mar 5 '16 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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