File tree 7 files changed +80
-13
lines changed
lib/Drupal/Core/Hook/Attribute
modules/system/tests/modules/hook_collector_hook_attribute
7 files changed +80
-13
lines changed Original file line number Diff line number Diff line change 1633
1633
* - On a method, use the attribute with the hook name:
1634
1634
* @code
1635
1635
* #[Hook('user_cancel')]
1636
- * public method userCancel(...)
1636
+ * public function userCancel(...) {}
1637
1637
* @endcode
1638
1638
* - On a class, specify the method name as well as the hook name:
1639
1639
* @code
1640
1640
* #[Hook('user_cancel', method: 'userCancel')]
1641
1641
* class Hooks {
1642
- * method userCancel(...) {}
1642
+ * public function userCancel(...) {}
1643
1643
* }
1644
1644
* @endcode
1645
1645
* - On a class with an __invoke method, which is taken to be the hook
1646
1646
* implementation:
1647
1647
* @code
1648
1648
* #[Hook('user_cancel')]
1649
1649
* class Hooks {
1650
- * method __invoke(...) {}
1650
+ * public function __invoke(...) {}
1651
1651
* }
1652
1652
* @endcode
1653
1653
*
Original file line number Diff line number Diff line change 12
12
* - On a method, use this attribute with the hook name:
13
13
* @code
14
14
* #[Hook('user_cancel')]
15
- * public method userCancel(...)
15
+ * public function userCancel(...) {}
16
16
* @endcode
17
17
* - On a class, specifying the method name:
18
18
* @code
19
19
* #[Hook('user_cancel', method: 'userCancel')]
20
20
* class Hooks {
21
- * method userCancel(...) {}
21
+ * public function userCancel(...) {}
22
22
* }
23
23
* @endcode
24
24
* - On a class with an __invoke method, which is taken to be the hook
25
25
* implementation:
26
26
* @code
27
27
* #[Hook('user_cancel')]
28
28
* class Hooks {
29
- * method __invoke(...) {}
29
+ * public function __invoke(...) {}
30
30
* }
31
31
* @endcode
32
32
*
Original file line number Diff line number Diff line change
1
+ name : ' Test Hook attribute'
2
+ type : module
3
+ description : ' Test Hook attribute with named arguments, and class with invoke method'
4
+ package : Testing
5
+ version : VERSION
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Drupal \hook_collector_hook_attribute \Hook ;
6
+
7
+ use Drupal \Core \Hook \Attribute \Hook ;
8
+
9
+ /**
10
+ * Test Hook attribute named arguments.
11
+ */
12
+ #[Hook('cache_flush ' )]
13
+ class HookAttributeInvokeHook {
14
+
15
+ /**
16
+ * Implements hook_cache_flush().
17
+ */
18
+ public function __invoke (): void {
19
+ // Set a global value we can check in test code.
20
+ $ GLOBALS ['hook_invoke_method ' ] = 'hook_invoke_method ' ;
21
+ }
22
+
23
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Drupal \hook_collector_hook_attribute \Hook ;
6
+
7
+ use Drupal \Core \Hook \Attribute \Hook ;
8
+
9
+ /**
10
+ * Test Hook attribute named arguments.
11
+ */
12
+ #[Hook(hook: 'cache_flush ' , method: 'flush ' )]
13
+ class HookAttributeNamedArgumentsHook {
14
+
15
+ /**
16
+ * Implements hook_cache_flush().
17
+ */
18
+ public function flush (): void {
19
+ // Set a global value we can check in test code.
20
+ $ GLOBALS ['hook_named_arguments ' ] = 'hook_named_arguments ' ;
21
+ }
22
+
23
+ }
Original file line number Diff line number Diff line change @@ -124,4 +124,17 @@ public function testProceduralHooksSkippedWhenConfigured(): void {
124
124
125
125
}
126
126
127
+ /**
128
+ * Test Hook attribute with named arguments, and class with invoke method.
129
+ */
130
+ public function testHookAttribute (): void {
131
+ $ module_installer = $ this ->container ->get ('module_installer ' );
132
+ $ this ->assertTrue ($ module_installer ->install (['hook_collector_hook_attribute ' ]));
133
+ $ this ->assertFalse (isset ($ GLOBALS ['hook_named_arguments ' ]));
134
+ $ this ->assertFalse (isset ($ GLOBALS ['hook_invoke_method ' ]));
135
+ drupal_flush_all_caches ();
136
+ $ this ->assertTrue (isset ($ GLOBALS ['hook_named_arguments ' ]));
137
+ $ this ->assertTrue (isset ($ GLOBALS ['hook_invoke_method ' ]));
138
+ }
139
+
127
140
}
Original file line number Diff line number Diff line change @@ -93,14 +93,7 @@ public function testGetHookAttributesInClass(): void {
93
93
$ getHookAttributesInClass = fn ($ class ) => $ this ->getHookAttributesInClass ($ class );
94
94
$ p = new HookCollectorPass ();
95
95
$ getHookAttributesInClass = $ getHookAttributesInClass ->bindTo ($ p , $ p );
96
- $ x = new class {
97
-
98
- #[Hook('install ' )]
99
- function foo (): void {}
100
96
101
- };
102
- $ this ->expectException (\LogicException::class);
103
- $ hooks = $ getHookAttributesInClass (get_class ($ x ));
104
97
$ x = new class {
105
98
106
99
#[Hook('foo ' )]
@@ -111,6 +104,16 @@ function foo(): void {}
111
104
$ hook = reset ($ hooks );
112
105
$ this ->assertInstanceOf (Hook::class, $ hook );
113
106
$ this ->assertSame ('foo ' , $ hook ->hook );
107
+
108
+ $ x = new class {
109
+
110
+ #[Hook('install ' )]
111
+ function foo (): void {}
112
+
113
+ };
114
+ $ this ->expectException (\LogicException::class);
115
+ // This will throw exception, and stop code execution.
116
+ $ getHookAttributesInClass (get_class ($ x ));
114
117
}
115
118
116
119
}
You can’t perform that action at this time.
0 commit comments