mzur/kirby-uniform

Update documentation for custom actions

trych opened this issue · 19 comments

trych commented

Hi there,

I am trying to create a custom action, but the code in the docs seems to be outdated for Kirby v3's new plugin structure.
When I past the sample code into a plugin file (like plugins/my-actions-plugin/index.php), then I get the error:

Class 'Uniform\Actions\Action' not found

Does anybody know, what structure my plugin needs to have to create a custom action in Kirby v3?

Thanks!

mzur commented

Looks like Uniform wasn't installed correctly. Can you use \Uniform\Form in a controller?

trych commented

Yes, I can use all the other Uniform functionality just fine.

I think I tracked down the issue. I had this in a plugin that was alphabetically placed before the uniform plugin folder. When I rename my plugin zzz it does not give me the error anymore. It looks like Kirby is registering the plugins one by one and only after the uniform plugin is registered I can use the uniform class.

Is this expected behaviour? Or am I setting up my plugin incorrectly? According to the Kirby v3 docs, plugins now should be wrapped in the static Kirby::plugin() method. I have not done this, as I don't know how to insert the snippet from the docs into the array that this method expects. Any ideas how to do this? I am currently not even sure if this dumpAction is working, not sure what output to expect and where.

Edit: I am also trying to use this in an AJAX setup, so the dumpAction is placed in a route, so I guess the var_dump() does not really do anything, right? Interestingly if I use this action, the return function does not return a JSON anymore? I just get a null, while the status seems to be 200. So it returns successfully, but does not return my JSON. I have no idea what's going on or why the dumpAction method would cause an issue like that.

Edit 2: Ok, apparently using var_dump() anywhere in the route causes the route to return null, so I guess the last part is not really a uniform issue. I still would like to know how to correctly set up the action within the Kirby::plugin() method.

mzur commented

I see. We had a similar issue before but with the whole plugin ( #204 ). Can you please try this code sample:

<?php

use Uniform\Actions\Action;

class DumpAction extends Action
{
    public function perform()
    {
        try {
            var_dump($this->form->data());
        } catch (\Exception $e) {
            $this->fail($e->getMessage());
        }
    }
}
trych commented

I gave it a try, unfortunately gives me the very same error:

20210903-121147_Screenshot_GoogleChrome

mzur commented

Ok thanks. I assume that you didn't install Uniform using Composer? I'll have a look.

trych commented

No, I installed it as a git submodule.

mzur commented

Ok, please try this:

  1. Create a file plugins/my-actions-plugin/index.php with the content:
<?php
 
load([
    'Uniform\\Actions\\DumpAction' => 'DumpAction.php'
], __DIR__);
  1. Create a file plugins/my-actions-plugin/DumpAction.php with the content:
<?php

namespace Uniform\Actions;

class DumpAction extends Action
{
    public function perform()
    {
        try {
            var_dump($this->form->data());
        } catch (\Exception $e) {
            $this->fail($e->getMessage());
        }
    }
}

Does it work now?

trych commented

Yes, it does not give me an error anymore, and it dumps the data on submit, however for some bizarre reason I seem not to be able to change the dump action. It always keeps dumping the form data, even though when I remove (!) the line var_dump($this->form->data());??

I'm not sure what to make of this, is there some default built-in action behaviour that dumps the form data? Because whatever I write into the dump action it gets ignored and it just dumps the form data. This is really strange. Thought it was some cache issue, but it happens even after deleting the cache or changing browsers.

So I am not in fact sure, if the dumpAction really works... yes it dumps something, but I cannot change it in any way...?

mzur commented

Yes, you are right, there is an undocumented DumpAction in Uniform. Try to rename the action/class and the changes should apply.

trych commented

Oh my, I thought I was going insane. 😅

Good. Now I can report that it works. You should probably either change the undocumented Action's name or change the example in the doc's, else people will run into the same problem. Also, it might make sense to use a example in the doc that works in an Ajax setup as well, I don't think the var_dump() command does.

This issue is solved for me now, thanks!

mzur commented

Here is a summary of the changes required in the docs:

  • Update the path to the action file (should be an index.php file)
  • Update the index.php example with a custom autoloaded action class file (see above)
  • Update the example action class file to something else than the dump action (should be compatible with ajax)

I'll leave this open as a reminder. Anyone willing to contribute with a PR (@trych?) is welcome to do so.

trych commented

Sorry, working 24/7 on my university finals right now, won't have any time to contribute right now. Would be willing to help in 3 weeks time, if nobody has jumped in by then.

I'll look into this in the next few days.

Just ran into this and thought, I was going crazy, as following the docs didn't seem to work 😄

Do you want to differentiate between composer and non-composer setups? If yes, how do you imagine structuring it?

Huh, more than a few days than 😊

the only additional part for nom-composer setup is explicit loading, i think, although ive been honestly putting this off for months ☹️

Well yeah, my question was on how to best write the docs considering both scenarios. I guess an additional section saying "for non-composer user, also do this" or similar...

mzur commented

I'd tend to write the docs for non-Composer users only. If you want to load the custom action with Composer, you need to write it as a separate PHP package and you should already know how that is done. Or were you thinking about loading the action with a custom classmap?

I didn't know how it's done for Composer users, just saw that you mentioned how it is for non-Composer users.

@adamkiss if you want, I can PR onto your branch or take your branch and create a new PR

@eXpl0it3r I've updated my PR - #217 - with non-composer setup as well, if you can review