V8-archive: Filter hooks do not run

Created on 21 Oct 2019  路  7Comments  路  Source: directus/v8-archive

Bug Report

Hi, I am making custom hooks. I want to modify the data on creation and update, and thus I would like to use the filters.

My starting point is just the example code from the Directus documentation, namely the following, notice the minor edit of the filter key to be generic, rather than being related to the particular articles collection.

<?php

return [
  'filters' => [
    'item.create' => function (\Directus\Hook\Payload $payload) {
      $payload->set('uuid', \Directus\generate_uuid4());

      return $payload;
    }
  ]
];

However the filters do not run.

If I have the same things as an action (and with array as input rather than a Payload), it runs just fine. I've tried directus/public/extensions/custom/hooks/hooks.php, directus/public/extensions/custom/hooks/OurOwnProject/hooks.php as well as the shipped example project called products.

Steps to Reproduce

  1. Create file directus/public/extensions/custom/hooks/hooks.php and copypaste example code from documentation-
  2. Create an item in any collection.
  3. Observe the value of uuid field of the created object.

Expected Behavior

Item is created, and uuid field is set.

Actual Behavior

Item gets created, uuid field is not set.

Other Context & Screenshots

Stepping away from the example code from the documentation and writing my own code using logging, if I have
directus/public/extensions/custom/hooks/OurOwnProject/hooks.php

use \Directus\Application\Application;
use \Directus\Hook\Payload;

return [
    // FIXME: Filters are not triggering, sadly.
    'filters' => [
        'item.create.collection' => function (Payload $payload) {
            $container = Application::getInstance()->getContainer();
            $logger = $container->get('logger');

            $logger->debug('Payload in filter', $payload);

            $payload->set('image_recognition_tag_id', 'kitties');

            $logger->debug('Payload in filter after setting the tag UUID.', $payload);

            return $payload;
        }
    ]
];

Nothing gets logged in directus/logs/debug*.log.

Further if I try to run something outrageous in the item.create filter, such as this_function_does_not_exists();, no errors are thrown.

Like above, if I define this as an action rather than as a filter (and with array input rather than a Payload), it runs just fine and I'm seeing the expected log output etc.

I conclude with confidence that the filter hooks are not running.

Thank you :)

Technical Details

  • Device: Computer machine
  • OS: Ubuntu 18.04 LTS
  • Web Server: Apache 2.4.29.
  • PHP Version: 7.2.19
  • Database: MySQL 5.7.27
  • Install Method: cloned master branch
bug

Most helpful comment

Perfect, thank you @bjgajjar and I must say Directus is a very cool thing and thanks for it!! Also super support online, which is totally appreciated by me and I'm sure everyone. Thank you 馃弲

All 7 comments

What, to me, looks like a related item from 2019-09-19 https://rangerstudio.slack.com/archives/CDVJD9M1B/p1568924257047600.

Let me know if there is something I can help with :)

This is the referenced Slack message:

Hi,
1st of all to all developers involved in Directus: very great work!
I've tried crating a custom hook for an custom interface as it is mentioned under: https://docs.directus.io/extensions/hooks.html#creating-hooks
But currently without any efford - the hook doesn't get called
I've registered the hooks for: 'filters' => 'item.create:before' and 'filters' => 'item.update:before' - but they just doesn't get called at all
I've tried it with Directus 190521A, 190827A and current master but it seems to make no difference.
Is there something special to respect for interface-hooks? Or does they currently doesn't work?
Thanks for your help!

Sorry, have missed creating a ticket due to the lack of time. I'think it's mainly a problem that the docs should be improved.
Below filters item.create:before will be called - but item.create or item.create:after won't. Below actions on the other hand item.create:after will be called.

See more in my anser: https://rangerstudio.slack.com/archives/CDVJD9M1B/p1571689034187200?thread_ts=1568925668.049500&cid=CDVJD9M1B

But this may be a nice improvement: interface-specific-hooks will be get called for every collection - even if it isn't used in this collection. This maybe improved. Even it would be nice to have the involved fields within the payload (beside the collection_name attribute already included with payload).

Ooh super, it is a documentation issue. My filters are now working. The

The documentation, on the Hooks page

Name | Description
-- | --
item.create | Item is created. You can also limit to a specific collection using item.create.[collection-name]. Supports :before and :after (default)
item.read | Item is read. You can also limit to a specific collection using item.read.[collection-name]. Supports :before and :after (default)
item.update | Item is updated. You can also limit to a specific collection using item.update.[collection-name]. Supports :before and :after (default)
response | Before adding the content into the HTTP response body. You can also limit to a specific collection using response.[collection-name].
response.[method] | Same as response but only executes for a specific http method, such as GET, POST, DELETE, PATCH, PUT, OPTIONS. You can also limit to a specific collection using response.[method].[collection-name].

says that :after is the default. These do not fire... and thus, can they be called defaults? 馃 . Anyway, attaching filters to item.create.collection:before works.

Thus, either to be considered a documentation kink (because the default does not run), or maybe change the default to :before and don't disregard :after, or clarify wording, or maybe it's just me. Anyway, I'm sorted (and in production 馃帀 ). Thanks @git-solatis 馃

attaching filters to item.create.collection:before works.

Agree here. API is not supporting the after for any filter events. IMO, it is not necessary too. So updating the doc and will send the PR :)

Perfect, thank you @bjgajjar and I must say Directus is a very cool thing and thanks for it!! Also super support online, which is totally appreciated by me and I'm sure everyone. Thank you 馃弲

I think this fix for https://github.com/directus/api/issues/1364 is not correct as ':after' is default for filters. item.create and item.update supports ':before' only but ':before' has to be set explicitly. On the other hand item.read supports ':after' (which is default).
Created a pull request for what i think is correct: https://github.com/directus/docs/pull/219
I think this ticket should be re-opened but i can't this im not the owner. @xmacex can you please re-open the issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

magikstm picture magikstm  路  3Comments

vuhrmeister picture vuhrmeister  路  3Comments

24js picture 24js  路  3Comments

gitlabisbetterthangithub picture gitlabisbetterthangithub  路  3Comments

cdwmhcc picture cdwmhcc  路  3Comments