1

This question is following this one: Where to implement Craft Webhook FilterInterface.

I'm trying to implement a Craft Webhook FilterInterface but failed to load it. Below is the content of my setup, which was initialized by composer dump-autoload -a.

config/app.php

<?php

use craft\helpers\App;

return [
    'id' => App::env('APP_ID') ?: 'CraftCMS',
    'modules' => [
        'utilities-module' => [
            'class' => \modules\utilitiesmodule\UtilitiesModule::class,
            'components' => [
                'companiesFilter' => [
                    'class' => 'modules\utilitiesmodule\filters\CompaniesFilter',
                ]
            ],
        ],
    ],
    'bootstrap' => ['utilities-module']
];

composer.json

[…]

"autoload": {
    "psr-4": {
      "modules\\utilitiesmodule\\": "modules/utilitiesmodule/src/"
    }
}

/modules/utilitiesmodule/src/UtilitiesModule.php

<?php

use modules\utilitiesmodule\filters\CompaniesFilter as CompaniesFilter;

[…]

Event::on(
    Webhooks::class, 
    Webhooks::EVENT_REGISTER_FILTER_TYPES, 
    function(RegisterComponentTypesEvent $event) {
        $event->types[] = CompaniesFilter::class;
    }
);

/modules/utilitiesmodule/src/filters/CompaniesFilter.php

<?php

namespace modules\utilitiesmodule\filters;

use modules\utilitiesmodule\UtilitiesModule;

use craft\webhooks\filters\FilterInterface;
use craft\elements\Entry;
use yii\base\Event;

class CompaniesFilter extends BaseElementFilter implements FilterInterface
{
    public static function displayName(): string
    {
        return 'Entry has a “Companies” section';
    }

    public static function show(string $class, string $event): bool
    {
        // Only show this filter if the Sender Class is set to 'craft\elements\Entry' 
        return $class === Entry::class;
    }

    public static function check(Event $event, bool $value): bool
    {
        // Filter based on whether the entry's section is 'companies':
        /** @var Entry $entry */
        $entry = $event->sender;
        return ($entry->section->handle === 'companies') === $value;
    }
}

I'm getting a Class 'modules\utilitiesmodule\filters\BaseElementFilter' not found error.

Stack trace

Error: Class 'modules\utilitiesmodule\filters\BaseElementFilter' not found in /project/modules/utilitiesmodule/src/filters/CompaniesFilter.php:11
Stack trace:
#0 /project/vendor/composer/ClassLoader.php(476): include()
#1 /project/vendor/composer/ClassLoader.php(344): Composer\Autoload\includeFile('/Users/romainpo...')
#2 [internal function]: Composer\Autoload\ClassLoader->loadClass('modules\\utiliti...')
#3 /project/vendor/craftcms/webhooks/src/controllers/WebhooksController.php(89): spl_autoload_call('modules\\utiliti...')
#4 [internal function]: craft\webhooks\controllers\WebhooksController->craft\webhooks\controllers\{closure}('modules\\utiliti...')
#5 /project/vendor/craftcms/webhooks/src/controllers/WebhooksController.php(98): array_map(Object(Closure), Array)
#6 [internal function]: craft\webhooks\controllers\WebhooksController->actionEdit(4, NULL, Object(craft\webhooks\Webhook))
#7 /project/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#8 /project/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#9 /project/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('edit', Array)
#10 /project/vendor/craftcms/cms/src/web/Application.php(287): yii\base\Module->runAction('webhooks/webhoo...', Array)
#11 /project/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction('webhooks/webhoo...', Array)
#12 /project/vendor/craftcms/cms/src/web/Application.php(272): yii\web\Application->handleRequest(Object(craft\web\Request))
#13 /project/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#14 /project/htdocs/index.php(26): yii\base\Application->run()
#15 {main}
3
  • 1
    Can you post the stack trace of this error to see where it's being thrown? Also, try leaving out the components definition in your config/app.php (you probably don't need this anyway) – does the error go away or change in any way?
    – MoritzLost
    Jan 31, 2022 at 13:27
  • I have updated the question with the stack trace. I also commented the components definition, but the error is still there.
    – Romain P.
    Feb 1, 2022 at 8:48
  • Now the error message makes sense ;-) It's just a missing import statement, see my full answer below.
    – MoritzLost
    Feb 1, 2022 at 9:17

1 Answer 1

1

Read the error carefully; for some reason, PHP is looking for a class modules\utilitiesmodule\filters\BaseElementFilter which doesn't exist. The error message also tells you that the error is caused by this line:

class CompaniesFilter extends BaseElementFilter implements FilterInterface

You want to extend craft\webhooks\filters\BaseElementFilter. But you have only specified the class name (BaseElementFilter), so PHP doesn't know to look in the craft\webhooks\filters namespace for that class. By default, class names are regular to the namespace the file is in, so BaseElementFilter expands to modules\utilitiesmodule\filters\BaseElementFilter. This class doesn't exist, so you get an error.

The solution is to either specify the fully-qualified namespace or import the class name using a use statement:

use craft\webhooks\filters\BaseElementFilter;

class CompaniesFilter extends BaseElementFilter implements FilterInterface {}

Read the documentation on importing / aliases in PHP for more information.

1
  • 1
    I thought the BaseElementFilter class was from the Craft core, not from the Webhook plugin. Thank you for your precious help!
    – Romain P.
    Feb 2, 2022 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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