ReplaceOriginalHook not discovered

Created on 11 April 2024, 5 months ago
Updated 12 April 2024, 5 months ago

Problem/Motivation

I wanted to disable another module's hook by using ReplaceOriginalHook, but it didn't work until I added #[Hook('node_insert')] to the method too.

The replacing hook wasn't called, and the original hook was still invoked. Adding in the Hook made it work as expected.

Discovered in 1.3.1, but the latest version is using the same code.

Steps to reproduce

<?php

declare(strict_types=1);

namespace Drupal\test_module\Hooks;

use Drupal\Core\Entity\EntityInterface;
use Drupal\hux\Attribute\ReplaceOriginalHook;

class DisableHooks {

  #[ReplaceOriginalHook(hook: 'node_update', moduleName: 'another module')]
  public function disable(EntityInterface $entity): void {
    die(); // Never called.
  }

}

Proposed resolution

HuxCompilerPass::getHuxClasses() only discovers classes with a method with the Hook and Alter annotations. I think it ought to discover ReplaceOriginalHook methods too.

Remaining tasks

Clone the 4 lines needed to discover ReplaceOriginalHook too.

User interface changes

None.

API changes

Well, classes with only ReplaceOriginalHook is discovered too.

Data model changes

None.

🐛 Bug report
Status

Active

Version

1.3

Component

Code

Created by

🇩🇰Denmark Xen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Xen
  • Status changed to Postponed: needs info 5 months ago
  • 🇦🇺Australia dpi Perth, Australia

    I just used this feature myself on another project in the last couple weeks, with success. This project is on v1.3.1.

    The example code doesnt look right. (class namespacing, attribute' moduleName param)

    What is the actual code/attributes you're using. and which hook from which module?

    There are also tests for this.

  • Status changed to Active 5 months ago
  • 🇩🇰Denmark Xen

    > The example code doesnt look right. (class namespacing, attribute' moduleName param)

    I replaced the module names as they're irrelevant for the issue and specific to the project where the issue was discovered, just as I removed the usual commenting you'd find in such code.

    > There are also tests for this.

    ReplacementOriginalHook isn't tested with autodiscovery of hook classes. The hook class is registered manually as a service: https://git.drupalcode.org/project/hux/-/blob/1.5.x/tests/modules/hux_re...

    Note how the test is failing in the MR where I've changed it to autodiscorvery.

  • 🇦🇺Australia dpi Perth, Australia

    Right I got you.

    but it didn't work until I added #[Hook('node_insert')] to the method too.

    So what youre saying is it theoretically didnt work until you added a Hook/Alter to any method in the same class, not just the replace method.

    In terms of fix, i believe we just need to update \Drupal\hux\HuxCompilerPass::getHuxClasses. We dont need to update the existing Replace test fixture.

    Lets create a new autodiscovery-only Hooks class fixture, where it only has ReplacementOriginalHook implementations

  • 🇦🇺Australia dpi Perth, Australia

    I just used this feature myself on another project in the last couple weeks

    I see now the class this is on also has adjacent Hooks and Alters.

  • 🇩🇰Denmark Xen

    > So what youre saying is it theoretically didnt work until you added a Hook/Alter to any method in the same class, not just the replace method.

    Exactly. Or rather, not theoretically. I added the #[Hook('node_insert')] annotation to the same disable method that had the ReplaceOriginalHooks. That triggered the autodiscovery. So the method is both a replacement and a hook implementation. I didn't test whether that makes hux call it multiple times, but as the method does nothing it doesn't really matter i my case.

    > In terms of fix, i believe we just need to update \Drupal\hux\HuxCompilerPass::getHuxClasses. We dont need to update the existing Replace test fixture.

    Yeah, it was for demonstration purposes only. I do hope Gitlab will let me force push...

    > Lets create a new autodiscovery-only Hooks class fixture, where it only has ReplacementOriginalHook implementations

    For completeness sake it ought to have 3 classes, one for each autodiscovered annotation.

    I'll whip something up...

  • 🇩🇰Denmark Xen

    Ah, on second thought, adding Alter and ReplaceOriginalHook to hux_auto_test is more proper.

  • 🇩🇰Denmark Xen

    Xen changed the visibility of the branch 3440302-replaceoriginalhook-not-discovered to hidden.

  • 🇩🇰Denmark Xen

    Xen changed the visibility of the branch 3440302-replaceoriginalhook-not-discovered to active.

  • 🇩🇰Denmark Xen

    There, added tests and fix.

Production build 0.71.5 2024