<p>Read the error carefully; for some reason, PHP is looking for a class <code>modules\utilitiesmodule\filters\BaseElementFilter</code> which doesn't exist. The error message also tells you that the error is caused by this line:</p>
<pre class="lang-php prettyprint-override"><code>class CompaniesFilter extends BaseElementFilter implements FilterInterface
</code></pre>
<p>You want to extend <code>craft\webhooks\filters\BaseElementFilter</code>. But you have only specified the class name (<code>BaseElementFilter</code>), so PHP doesn't know to look in the <code>craft\webhooks\filters</code> namespace for that class. By default, class names are regular to the namespace the file is in, so <code>BaseElementFilter</code> expands to <code>modules\utilitiesmodule\filters\BaseElementFilter</code>. This class doesn't exist, so you get an error.</p>
<p>The solution is to either specify the fully-qualified namespace or <a href="https://www.php.net/manual/en/language.namespaces.importing.php" rel="nofollow noreferrer">import the class name</a> using a <code>use</code> statement:</p>
<pre class="lang-php prettyprint-override"><code>use craft\webhooks\filters\BaseElementFilter;

class CompaniesFilter extends BaseElementFilter implements FilterInterface {}
</code></pre>
<p>Read the documentation on <a href="https://www.php.net/manual/en/language.namespaces.importing.php" rel="nofollow noreferrer">importing / aliases</a> in PHP for more information.</p>

CC BY-SA 4.0
2022-02-02T10:48:00.500Z
I thought the `BaseElementFilter` class was from the Craft core, not from the Webhook plugin. Thank you for your precious help!
Romain P.
CC BY-SA 4.0
2024-04-19T16:43:56.160Z
@RomainP. did you ever get this working? I'm struggling with the same issue. Could you possibly post the working code please?
Graham Bennett
CC BY-SA 4.0
2024-04-22T07:53:53.000Z
The code is in my question, and the missing code was (as per @MoritzLost answer):
`use craft\webhooks\filters\BaseElementFilter;`.
Romain P.
CC BY-SA 4.0
2022-02-01T09:17:22.213Z
2022-02-01T09:17:22.213Z
MoritzLost
<p><em>This question is following this one: <a href="https://craftcms.stackexchange.com/questions/38898/where-to-implement-craft-webhook-filterinterface/38899">Where to implement Craft Webhook FilterInterface</a>.</em></p>
<p>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 <code>composer dump-autoload -a</code>.</p>
<p><strong>config/app.php</strong></p>
<pre><code>&lt;?php

use craft\helpers\App;

return [
    'id' =&gt; App::env('APP_ID') ?: 'CraftCMS',
    'modules' =&gt; [
        'utilities-module' =&gt; [
            'class' =&gt; \modules\utilitiesmodule\UtilitiesModule::class,
            'components' =&gt; [
                'companiesFilter' =&gt; [
                    'class' =&gt; 'modules\utilitiesmodule\filters\CompaniesFilter',
                ]
            ],
        ],
    ],
    'bootstrap' =&gt; ['utilities-module']
];
</code></pre>
<p><strong>composer.json</strong></p>
<pre><code>[…]

&quot;autoload&quot;: {
    &quot;psr-4&quot;: {
      &quot;modules\\utilitiesmodule\\&quot;: &quot;modules/utilitiesmodule/src/&quot;
    }
}
</code></pre>
<p><strong>/modules/utilitiesmodule/src/UtilitiesModule.php</strong></p>
<pre><code>&lt;?php

use modules\utilitiesmodule\filters\CompaniesFilter as CompaniesFilter;

[…]

Event::on(
    Webhooks::class, 
    Webhooks::EVENT_REGISTER_FILTER_TYPES, 
    function(RegisterComponentTypesEvent $event) {
        $event-&gt;types[] = CompaniesFilter::class;
    }
);
</code></pre>
<p><strong>/modules/utilitiesmodule/src/filters/CompaniesFilter.php</strong></p>
<pre><code>&lt;?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-&gt;sender;
        return ($entry-&gt;section-&gt;handle === 'companies') === $value;
    }
}
</code></pre>
<p>I'm getting a <code>Class 'modules\utilitiesmodule\filters\BaseElementFilter' not found</code> error.</p>
<p><strong>Stack trace</strong></p>
<pre><code>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-&gt;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-&gt;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-&gt;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-&gt;runWithParams(Array)
#9 /project/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller-&gt;runAction('edit', Array)
#10 /project/vendor/craftcms/cms/src/web/Application.php(287): yii\base\Module-&gt;runAction('webhooks/webhoo...', Array)
#11 /project/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application-&gt;runAction('webhooks/webhoo...', Array)
#12 /project/vendor/craftcms/cms/src/web/Application.php(272): yii\web\Application-&gt;handleRequest(Object(craft\web\Request))
#13 /project/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application-&gt;handleRequest(Object(craft\web\Request))
#14 /project/htdocs/index.php(26): yii\base\Application-&gt;run()
#15 {main}
</code></pre>

CC BY-SA 4.0
2022-01-31T13:27:53.633Z
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
CC BY-SA 4.0
2022-02-01T08:48:07.153Z
I have updated the question with the stack trace. I also commented the `components` definition, but the error is still there.
Romain P.
CC BY-SA 4.0
2022-02-01T09:17:45.880Z
Now the error message makes sense ;-) It's just a missing import statement, see my full answer below.
MoritzLost
<filter><modules><composer>
Difficulties to setup a Filter class
# Difficulties to setup a Filter class

(Asked by: username_0 on 2022-01-31)

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
 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
types[] = CompaniesFilter::class;
    }
);

/modules/utilitiesmodule/src/filters/CompaniesFilter.php
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}

**Tags:** filter, modules, composer

> **Comments:**
> **username_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?  
> **username_0**: I have updated the question with the stack trace. I also commented the `components` definition, but the error is still there.  
> **username_1**: Now the error message makes sense ;-) It's just a missing import statement, see my full answer below.  

---

## 1 answers

### Answer by username_1 on 2022-02-01. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_0**: I thought the `BaseElementFilter` class was from the Craft core, not from the Webhook plugin. Thank you for your precious help!  
> **username_2**: @RomainP. did you ever get this working? I'm struggling with the same issue. Could you possibly post the working code please?  
> **username_0**: The code is in my question, and the missing code was (as per @username_1 answer): `use craft\webhooks\filters\BaseElementFilter;`.  

---

