<p>My guess is it's because your <code>NbspPlugin</code> class has the namespace of <code>craft</code>, when it should be namespace <code>Craft</code>.</p>

<p>FWIW, generating Craft plugin boilerplate code through something like <a href="https://pluginfactory.io/" rel="nofollow noreferrer">https://pluginfactory.io/</a> is a great way to save sanity and avoid things like this. :)</p>

CC BY-SA 3.0
2017-06-14T23:29:49.193Z
2017-06-14T23:29:49.193Z
Brad Bell
<p>So I started building this plugin to add a twig extension to escape but leave <code>&amp;nbsp;</code> intact. For some reason when I try install it or use it without the force argument on the import command in the plugin file set to true it does not work. If i set it to true, though, it installs fine and works fine.</p>

<p>My plugin Directory Structure:</p>

<pre><code>craft
 - plugins
   - nbsp
     NbspPlugin.php
     - twigextensions
       NbspTwigExtension.php
</code></pre>

<p>Here is my NbspPlugin.php file:</p>

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

class NbspPlugin extends BasePlugin {

    function getName()
    {
        return Craft::t('&amp;nbsp Twig Filter');
    }

    function getVersion()
    {
        return '1.0';
    }

    function getDeveloper()
    {
        return 'Rick';
    }

    function getDeveloperUrl()
    {
        return '';
    }


    public function addTwigExtension()
    {
        Craft::import('plugins.nbsp.twigextensions.NbspTwigExtension');

        return new NbspTwigExtension();
    }
}
</code></pre>

<p>Here is my NbspTwigExtension.php file:</p>

<pre><code>&lt;?php

namespace Craft;

class NbspTwigExtension extends \Twig_Extension
{

    public function getName()
    {
        return '&amp;nbsp; filter';
    }

    public function getFilters()
    {
        $returnArray = array();
        $methods = array(
            'escapeNonNbsp',
        );

        foreach ($methods as $methodName) {
            $returnArray[$methodName] = new \Twig_Filter_Method($this, $methodName, ['pre_escape' =&gt; 'html', 'is_safe' =&gt; array('html')]);
        }

        return $returnArray;
    }

    public function escapeNonNbsp($content)
    {
        // perform filter operations
        return str_replace('&amp;amp;nbsp;', '&amp;nbsp;', $content);
    }

}
</code></pre>

<p>I cannot install the plugin. I get an exception 
<code>Can't find Craft\NbspTwigExtension</code> referencing the return line of the <code>addTwigExtension()</code> Method. If I add the <code>force = true</code> argument to the import above it, it installs it, but then upon attempting to use the filter in the twig template it fails even with the import being forced.</p>

<p>What am I doing wrong?</p>

<p>EDIT - I found one bug in my set up where the name of my method in the extension class needs to match how I call the filter from twig. I changed it to:</p>

<pre><code>&lt;?php

namespace Craft;

class NbspTwigExtension extends \Twig_Extension
{

    public function getName()
    {
        return '&amp;nbsp; filter';
    }

    public function getFilters()
    {
        $returnArray = array();
        $methods = array(
            'nbsp',
        );

        foreach ($methods as $methodName) {
            $returnArray[$methodName] = new \Twig_Filter_Method($this, $methodName, ['pre_escape' =&gt; 'html', 'is_safe' =&gt; array('html')]);
        }

        return $returnArray;
    }

    public function nbsp($content)
    {
        // perform filter operations
        return str_replace('&amp;amp;nbsp;', '&amp;nbsp;', $content);
    }

}
</code></pre>

<p>However it still requires "force" true on the import command to work or install. Why!!?</p>

CC BY-SA 3.0
2017-06-09T22:17:49.093Z
My guess is it'll be some typo or case-sensitivity issue in one of your plugin files or class names.  It's helpful to use something like https://pluginfactory.io to generate the boilerplate code and help remove the chance for errors like that.
Brad Bell
CC BY-SA 3.0
2017-06-12T15:07:09.257Z
I've followed through using xdebug and without the force flag it finds the correct class files but just simply does not "load" them, unless I have the force include flag set. I do not think this is a casing issue, as the casing looks good everywhere (all code is in the post so let me know if you think you see something I don't).
Rick Kukiela
CC BY-SA 3.0
2017-06-12T23:39:01.877Z
Your `NbspPlugin` class has `namespace craft`, when it should be `namespace Craft`.  Fix that and see if it helps.
Brad Bell
CC BY-SA 3.0
2017-06-14T22:19:45.583Z
@BradBell - Yes I figured this out yesterday (thanks to your suggestion) I just haven't had time to come back and update the question. Also can you post your response as an answer so that I can accept it and get this closed out?
Rick Kukiela
CC BY-SA 3.0
2017-06-14T23:30:01.097Z
Glad you're all sorted and done!
Brad Bell
<plugin-development><twigextension>
Cannot install custom Twig extension plugin
# Cannot install custom Twig extension plugin

(Asked by: username_0 on 2017-06-09)

So I started building this plugin to add a twig extension to escape but leave   intact. For some reason when I try install it or use it without the force argument on the import command in the plugin file set to true it does not work. If i set it to true, though, it installs fine and works fine.
My plugin Directory Structure:
craft
 - plugins
   - nbsp
     NbspPlugin.php
     - twigextensions
       NbspTwigExtension.php

Here is my NbspPlugin.php file:

Here is my NbspTwigExtension.php file:
 'html', 'is_safe' => array('html')]);
        }

        return $returnArray;
    }

    public function escapeNonNbsp($content)
    {
        // perform filter operations
        return str_replace('&nbsp;', ' ', $content);
    }

}

I cannot install the plugin. I get an exception 
Can't find Craft\NbspTwigExtension referencing the return line of the addTwigExtension() Method. If I add the force = true argument to the import above it, it installs it, but then upon attempting to use the filter in the twig template it fails even with the import being forced.
What am I doing wrong?
EDIT - I found one bug in my set up where the name of my method in the extension class needs to match how I call the filter from twig. I changed it to:
 'html', 'is_safe' => array('html')]);
        }

        return $returnArray;
    }

    public function nbsp($content)
    {
        // perform filter operations
        return str_replace('&nbsp;', ' ', $content);
    }

}

However it still requires "force" true on the import command to work or install. Why!!?

**Tags:** plugin-development, twigextension

> **Comments:**
> **username_1**: My guess is it'll be some typo or case-sensitivity issue in one of your plugin files or class names.  It's helpful to use something like https://pluginfactory.io to generate the boilerplate code and help remove the chance for errors like that.  
> **username_0**: I've followed through using xdebug and without the force flag it finds the correct class files but just simply does not "load" them, unless I have the force include flag set. I do not think this is a casing issue, as the casing looks good everywhere (all code is in the post so let me know if you think you see something I don't).  
> **username_1**: Your `NbspPlugin` class has `namespace craft`, when it should be `namespace Craft`.  Fix that and see if it helps.  
> **username_0**: @BradBell - Yes I figured this out yesterday (thanks to your suggestion) I just haven't had time to come back and update the question. Also can you post your response as an answer so that I can accept it and get this closed out?  
> **username_1**: Glad you're all sorted and done!  

---

## 1 answers

### Answer by username_1 on 2017-06-14. Score: 1 | Accepted answer

My guess is it's because your `NbspPlugin` class has the namespace of `craft`, when it should be namespace `Craft`.

FWIW, generating Craft plugin boilerplate code through something like <https://pluginfactory.io/> is a great way to save sanity and avoid things like this. :)


---

