1

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:

<?php
namespace craft;

class NbspPlugin extends BasePlugin {

    function getName()
    {
        return Craft::t('&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();
    }
}

Here is my NbspTwigExtension.php file:

<?php

namespace Craft;

class NbspTwigExtension extends \Twig_Extension
{

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

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

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

        return $returnArray;
    }

    public function escapeNonNbsp($content)
    {
        // perform filter operations
        return str_replace('&amp;nbsp;', '&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:

<?php

namespace Craft;

class NbspTwigExtension extends \Twig_Extension
{

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

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

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

        return $returnArray;
    }

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

}

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

  • 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 pluginfactory.io to generate the boilerplate code and help remove the chance for errors like that. – Brad Bell Jun 9 '17 at 22:17
  • 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 Jun 12 '17 at 15:07
  • Your NbspPlugin class has namespace craft, when it should be namespace Craft. Fix that and see if it helps. – Brad Bell Jun 12 '17 at 23:39
  • @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 Jun 14 '17 at 22:19
  • Glad you're all sorted and done! – Brad Bell Jun 14 '17 at 23:30
1

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. :)

| improve this answer | |

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.