<p>I think your issue may be that the ElementsService <a href="https://craftcms.com/classreference/services/ElementsService#getAction-detail" rel="nofollow"><code>getAction</code></a> method expects <em>the full classname</em> for your custom ElementAction (sans the "ElementAction" suffix), i.e.</p>

<pre><code>$customAction = craft()-&gt;elements-&gt;getAction('PluginName_Custom');
</code></pre>

CC BY-SA 3.0
2016-03-06T12:15:03.810Z
2016-03-06T12:15:03.810Z
Mats Mikkel Rummelhoff
<p>I'd recommend to follow the guide in the docs. <a href="https://craftcms.com/docs/plugins/element-actions" rel="nofollow">https://craftcms.com/docs/plugins/element-actions</a></p>

<p><strong>Update:</strong> There's a dedicated section about "Adding actions to index pages".</p>

<blockquote>
  <p>If your Element Action is intended for your own custom Element Type, you can do that by implementing the <a href="http://buildwithcraft.com/classreference/elementtypes/IElementType#getAvailableActions-detail" rel="nofollow">getAvailableActions()</a> method on your Element Type class.</p>
</blockquote>

<p>The link takes you to the method's documentation.</p>

<pre class="lang-php prettyprint-override"><code>/**
 * 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);
</code></pre>

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

<blockquote>
  <p>[…] Craft’s built-in Element Types: <a href="https://craftcms.com/docs/plugins/hooks-reference#addEntryActions" rel="nofollow">addEntryActions</a>, <a href="https://craftcms.com/docs/plugins/hooks-reference#addCategoryActions" rel="nofollow">addCategoryActions</a>, …</p>
</blockquote>

<p>There's example code provided if you click those links.</p>

<pre class="lang-php prettyprint-override"><code>public function addEntryActions($source)
{
    return array(
        'Foo',
        new BarElementAction(),
    );
}
</code></pre>

CC BY-SA 3.0
2016-03-05T20:15:17.547Z
Yep. That was copied into PluginName_CustomElementAction. My question actually centers around getAvailableActions and why my custom action isn't an object.
Adam McCombs
CC BY-SA 3.0
2016-03-05T20:16:29.820Z
Only wanted to help. Sorry.
carlcs
CC BY-SA 3.0
2016-03-05T20:20:56.607Z
Very kind reaction of you down voting me and my attempt to do so!
carlcs
CC BY-SA 3.0
2016-03-05T20:24:53.000Z
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
CC BY-SA 3.0
2016-03-05T23:46:10.417Z
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
CC BY-SA 3.0
2016-03-05T20:00:15.257Z
2016-03-06T11:10:26.293Z
2016-03-06T11:10:26.293Z
carlcs
<p>How do you load a custom element action using getAvailableActions inside my Element Type class?</p>

<pre><code>public function getAvailableActions($source = null)
{
    $customAction = craft()-&gt;elements-&gt;getAction('Custom');

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

    $actions[] = $customAction;
}
</code></pre>

<p>The above renders a PHP error <strong>PHP Fatal error: Call to a member function setParams() on a non-object</strong></p>

<p><em>PluginName_CustomElementAction</em> is my file inside the <em>elementactions</em> folder, inside my plugin. Which is copied directly from <a href="https://craftcms.com/docs/plugins/element-actions" rel="nofollow">https://craftcms.com/docs/plugins/element-actions</a> </p>

<p>Here's what it looks like:</p>

<pre><code>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-&gt;find();

        foreach ($elements as $element)
        {
            craft()-&gt;cocktailRecipes-&gt;removeAllIngredientsFromElement($element);
        }

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

<p>What is wrong with my getAvailableActions object?</p>

CC BY-SA 3.0
2016-03-06T05:25:55.300Z
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
CC BY-SA 3.0
2016-03-06T06:15:07.670Z
@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
<plugin-development><elementaction>
Trouble loading custom element action (menu option)
# Trouble loading custom element action (menu option)

(Asked by: username_0 on 2016-03-05)

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?

**Tags:** plugin-development, elementaction

> **Comments:**
> **username_1**: What's the code in your `getAvailableActions` method?  It'd probably help to share what's in your main element class as well.  
> **username_0**: @BradBell- just edited the first part to show the function. I can share the full file/plugin privately if this isn't enough.  

---

## 2 answers

### Answer by username_2 on 2016-03-06. Score: 2 | Accepted answer

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');


---

### Answer by username_3 on 2016-03-05. Score: 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(),
    );
}

> **Comments:**
> **username_0**: Yep. That was copied into PluginName_CustomElementAction. My question actually centers around getAvailableActions and why my custom action isn't an object.  
> **username_3**: Only wanted to help. Sorry.  
> **username_3**: Very kind reaction of you down voting me and my attempt to do so!  
> **username_0**: 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.  
> **username_3**: 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!  

---

