function HookCollectorPass::getHookAttributesInClass

An array of Hook attributes on this class with $method set.

Parameters

string $class: The class.

Return value

\Drupal\Core\Hook\Attribute\Hook[] An array of Hook attributes on this class. The $method property is guaranteed to be set.

1 call to HookCollectorPass::getHookAttributesInClass()
HookCollectorPass::collectModuleHookImplementations in core/lib/Drupal/Core/Hook/HookCollectorPass.php
Collects procedural and Attribute hook implementations.

File

core/lib/Drupal/Core/Hook/HookCollectorPass.php, line 229

Class

HookCollectorPass
Collects and registers hook implementations.

Namespace

Drupal\Core\Hook

Code

protected static function getHookAttributesInClass(string $class) : array {
    if (!class_exists($class)) {
        return [];
    }
    $reflection_class = new \ReflectionClass($class);
    $class_implementations = [];
    // Check for #[Hook] on the class itself.
    foreach ($reflection_class->getAttributes(Hook::class, \ReflectionAttribute::IS_INSTANCEOF) as $reflection_attribute) {
        $hook = $reflection_attribute->newInstance();
        assert($hook instanceof Hook);
        self::checkForProceduralOnlyHooks($hook, $class);
        if (!$hook->method) {
            if (method_exists($class, '__invoke')) {
                $hook->setMethod('__invoke');
            }
            else {
                throw new \LogicException("The Hook attribute for hook {$hook->hook} on class {$class} must specify a method.");
            }
        }
        $class_implementations[] = $hook;
    }
    // Check for #[Hook] on methods.
    foreach ($reflection_class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method_reflection) {
        foreach ($method_reflection->getAttributes(Hook::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute_reflection) {
            $hook = $attribute_reflection->newInstance();
            assert($hook instanceof Hook);
            self::checkForProceduralOnlyHooks($hook, $class);
            $class_implementations[] = $hook->setMethod($method_reflection->getName());
        }
    }
    return $class_implementations;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.