<p>I found an issue importing the enums via the <code>init()</code> method. You will receive a <code>Fatal error: Class 'Craft\PluginStatus' not found</code> on install. It seems during the install the <code>init()</code> method is not called in time to include the custom enums. </p>

<p>However, this works as its called immediately.</p>

<p><strong>pluginname/PluginNamePlugin.php</strong></p>

<pre><code>&lt;?php
namespace Craft;

// include enums for custom statuses
include(dirname(__FILE__) . '/enums/PluginStatus.php');

class PluginNamePlugin extends BasePlugin
{
    // etc
}
</code></pre>

CC BY-SA 3.0
2014-07-15T07:55:19.533Z
2014-07-15T08:18:50.350Z
2014-07-15T08:18:50.350Z
Jason McCallister
<p>Right now that's probably the best way to do it.</p>

<p>For Craft 2.2, I just added a static <code>getConstants()</code> method to BaseEnum, so you can do can do a <code>PluginStatus::getConstants()</code> call that will return an array with a key => value pair of the enum options and their values that should make that cleaner.</p>

CC BY-SA 3.0
2014-07-15T04:47:39.697Z
Glad you're easily impressed. :)
Brad Bell
CC BY-SA 3.0
2014-07-15T04:48:13.577Z
Anything Brad does impresses me as I know getting you to do anything takes an act of God... ;)

But seriously, awesome to hear.
Jason McCallister
CC BY-SA 3.0
2014-07-15T04:45:41.040Z
2014-07-15T04:45:41.040Z
Brad Bell
<p>I am building a plugin that's uses an enum for custom statuses. The statuses are integrated within my plugin with the exception of changing the status in my template code. </p>

<p><strong>pluginname/enums/PluginStatus.php</strong></p>

<pre><code>&lt;?php
namespace Craft;

class PluginNameStatus
{
    const Approved = 'approved';
    const Denied   = 'denied';
    const Pending  = 'pending';
}
</code></pre>

<p>What is the best method to grab the enum values and display those in a select field for my edit template? All of my templates are rendered via controller actions so I am thinking that assigning the enums to an array and passing to the templates as $variables might be the most practical method.</p>

<p><strong>pluginname/controllers/PluginNameController.php</strong></p>

<pre><code>&lt;?php
namespace Craft;

class PluginNameController extends BaseController
{
    public function actionShowIndex()
    {
        $variables['customStatuses'] = array(
            PluginStatus::Approved =&gt; 'Approved',
            PluginStatus::Denied   =&gt; 'Denied',
            PluginStatus::Pending  =&gt; 'Pending',
        ), 
        $this-&gt;renderTemplate('pluginname/_edit', $variables);
    }
}
</code></pre>

<plugin-development><templating><controller><custom-field>
Best way to use custom enums in my plugin templates?
# Best way to use custom enums in my plugin templates?

(Asked by: username_0 on 2014-07-15)

I am building a plugin that's uses an enum for custom statuses. The statuses are integrated within my plugin with the exception of changing the status in my template code. 
pluginname/enums/PluginStatus.php

What is the best method to grab the enum values and display those in a select field for my edit template? All of my templates are rendered via controller actions so I am thinking that assigning the enums to an array and passing to the templates as $variables might be the most practical method.
pluginname/controllers/PluginNameController.php
 'Approved',
            PluginStatus::Denied   => 'Denied',
            PluginStatus::Pending  => 'Pending',
        ), 
        $this->renderTemplate('pluginname/_edit', $variables);
    }
}

**Tags:** plugin-development, templating, controller, custom-field


---

## 2 answers

### Answer by username_1 on 2014-07-15. Score: 5 | Accepted answer

Right now that's probably the best way to do it.

For Craft 2.2, I just added a static `getConstants()` method to BaseEnum, so you can do can do a `PluginStatus::getConstants()` call that will return an array with a key => value pair of the enum options and their values that should make that cleaner.

> **Comments:**
> **username_1**: Glad you're easily impressed. :)  
> **username_0**: Anything Brad does impresses me as I know getting you to do anything takes an act of God... ;)  But seriously, awesome to hear.  

---

### Answer by username_0 on 2014-07-15. Score: 2

I found an issue importing the enums via the init() method. You will receive a Fatal error: Class 'Craft\PluginStatus' not found on install. It seems during the install the init() method is not called in time to include the custom enums. 
However, this works as its called immediately.
pluginname/PluginNamePlugin.php


---

