How to override Elgg's user icon upload action

34 Views Asked by At

I've been trying to override the default user icon action.

my_plugin/classes/Elgg/MyHooksOverrides/EntityIconOverride.php :

<?php

namespace Elgg\MyHooksOverrides;

class EntityIconOverride {
    public function __invoke(\Elgg\Hook $hook) {
        return false;
    }
}

my_plugin/elgg-plugin.php :

<?php

return [
    'plugin' => [
        'name' => 'My Plugin',
        'activate_on_install' => true,
    ],
    'view_extensions' => [],
    'hooks' => [
        'prepare' => [
            'entity:avatar:prepare' => [
                \Elgg\MyHooksOverrides\EntityIconOverride::class => [],
            ],
        ],
    ],
];

I've also tried this setup with entity:icon:prepare :

<?php

return [
    'plugin' => [
        'name' => 'My Plugin',
        'activate_on_install' => true,
    ],
    'view_extensions' => [],
    'hooks' => [
        'prepare' => [
            'entity:avatar:prepare' => [
                \Elgg\MyHooksOverrides\EntityIconOverride::class => [],
            ],
        ],
    ],
];

However, this is not having any effect. The hook should prevent the upload from proceeding, but the upload goes on successfully meaning that the plugin hook never gets called.

How can I make sure that my plugin hook gets called / OR, in other words, how can I override the default user icon upload action?

Thank you all in advance.

1

There are 1 best solutions below

0
ShadowTrix On

You should override 'avatar/upload' action:

return [
    'plugin' => [
        'name' => 'My Plugin',
        'activate_on_install' => true,
    ],
    'actions' => [
        'avatar/upload' => [
             'controller' => \MyPlugin\Actions\AvatarUpload::class,
         ],
    ],
];

Elgg docs.

Also, you can use 'action/validate' hook:

By the way, I don't recommend use \Elgg namespace in your custom plugin. Use \MyPlugin instead