How to Edit a Menu Item via Plugin Hooks

(Elgg-4.1.6)

I've been trying to edit the Activity menu, to no success. Nothing happens at all whatsoevr.

In my elgg-plugin.php

<?php

return [
    'plugin' => [
        'name' => 'Plugin Overrides',
        'activate_on_install' => true,
    ],
    'hooks' => [
        'register' => [
            'menu:owner_block' => [
                'Elgg\Activity\Menus\OwnerBlock::changeActivityIcon' => [],
            ],
        ],
    ],
];


In Elgg\Activity\Menus\OwnerBlock::changeActivityIcon


<?php

namespace Elgg\Activity\Menus;

class OwnerBlock {
    public static function changeActivityIcon(\Elgg\Hook $hook) {
         $owner = $hook->getEntityParam();

        $items = $hook->getValue();
        if ($items->has('activity:owner')) {
                $items->get('activity:owner')->setText('OK GO');
        }

        return $items;
    }
}

What could it be that I'm missing?

Thank you all in advance.

  • Have you tried

    $item = $items->get('activity:owner');
    $item->setText('just tesing');
    
    $items->add($item);
    
    return $items;

    Is your plugin below the Activity plugin?

  • Hi there Jerome. I've managed to get it to work by overriding the original menu item and re-registering it all over again. Your solution, however, doesn't do any changes - I'm not sure why. I was hoping it would as my solution doesn't seem like the more clean and consistent way to go about it.

    public static function changeActivityIcon(\Elgg\Hook $hook) {
        $entity = $hook->getEntityParam();
        if (!$entity instanceof \ElggUser) {
            return;
        }
        
        $return = $hook->getValue();
        
        $return[] = \ElggMenuItem::factory([
            'name' => 'activity:owner',
            'icon' => 'clock-regular',
            'text' => elgg_echo('activity:owner'),
            'href' => elgg_generate_url('collection:river:owner', ['username' => $entity->username]),
        ]);
    
        return $return;
    }
  • Revilo,

    I tried (almost) exactly what you did. And it works.

    The only change I made was the namespace of the Class that handles the hook. Since you put your class in exactly the same namespace as the Activity plugin it could be that your plugin overrules the class. Therefor the original item isn't registered.

    If you place your the class in namespace 'YourName\YourPlugin\Menus' it should work. The class file should then be place inside you plugin in the folder 'classes\YourName\YourPlugin\Menus\OwnerBlock.php'