Merge pull request #16108 from civicrm/5.21
[civicrm-core.git] / CRM / Utils / Hook / DrupalBase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_Hook_DrupalBase extends CRM_Utils_Hook {
18
19 /**
20 * @var bool
21 */
22 private $isBuilt = FALSE;
23
24 /**
25 * All Modules.
26 *
27 * @var string[]
28 */
29 private $allModules = NULL;
30
31 /**
32 * CiviCRM Modules.
33 *
34 * @var string[]
35 */
36 private $civiModules = NULL;
37
38 /**
39 * Drupal modules.
40 *
41 * @var string[]
42 */
43 private $drupalModules = NULL;
44
45 /**
46 * @param int $numParams
47 * Number of parameters to pass to the hook.
48 * @param mixed $arg1
49 * Parameter to be passed to the hook.
50 * @param mixed $arg2
51 * Parameter to be passed to the hook.
52 * @param mixed $arg3
53 * Parameter to be passed to the hook.
54 * @param mixed $arg4
55 * Parameter to be passed to the hook.
56 * @param mixed $arg5
57 * Parameter to be passed to the hook.
58 * @param mixed $arg6
59 * @param string $fnSuffix
60 * Function suffix, this is effectively the hook name.
61 *
62 * @return array|bool
63 * @throws \Exception
64 * @see CRM_Utils_Hook::invoke()
65 */
66 public function invokeViaUF(
67 $numParams,
68 &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6,
69 $fnSuffix) {
70
71 $this->buildModuleList();
72
73 return $this->runHooks($this->allModules, $fnSuffix,
74 $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6
75 );
76 }
77
78 /**
79 * Build the list of modules to be processed for hooks.
80 */
81 public function buildModuleList() {
82 if ($this->isBuilt === FALSE) {
83 if ($this->drupalModules === NULL) {
84 $this->drupalModules = $this->getDrupalModules();
85 }
86
87 if ($this->civiModules === NULL) {
88 $this->civiModules = [];
89 $this->requireCiviModules($this->civiModules);
90 }
91
92 // CRM-12370
93 // we should add civicrm's module's just after main civicrm drupal module
94 // Note: Assume that drupalModules and civiModules may each be array() or NULL
95 if ($this->drupalModules !== NULL) {
96 foreach ($this->drupalModules as $moduleName) {
97 $this->allModules[$moduleName] = $moduleName;
98 if ($moduleName == 'civicrm') {
99 if (!empty($this->civiModules)) {
100 foreach ($this->civiModules as $civiModuleName) {
101 $this->allModules[$civiModuleName] = $civiModuleName;
102 }
103 }
104 }
105 }
106 }
107 else {
108 $this->allModules = (array) $this->civiModules;
109 }
110
111 if ($this->drupalModules !== NULL && $this->civiModules !== NULL) {
112 // both CRM and CMS have bootstrapped, so this is the final list
113 $this->isBuilt = TRUE;
114 }
115 }
116 }
117
118 /**
119 * Gets modules installed on the Drupal site.
120 *
121 * @return array|null
122 * The machine names of the modules installed in Drupal, or NULL if unable
123 * to determine the modules.
124 */
125 protected function getDrupalModules() {
126 if (function_exists('module_list')) {
127 // copied from user_module_invoke
128 return module_list();
129 }
130 }
131
132 }