Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Extension/module.api.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Hooks related to module and update systems. | |
6 */ | |
7 | |
8 use Drupal\Core\Database\Database; | |
9 use Drupal\Core\Url; | |
10 use Drupal\Core\Utility\UpdateException; | |
11 | |
12 /** | |
13 * @defgroup update_api Update API | |
14 * @{ | |
15 * Updating minor versions of modules | |
16 * | |
17 * When you update code in a module, you may need to update stored data so that | |
18 * the stored data is compatible with the new code. If this update is between | |
19 * two minor versions of your module within the same major version of Drupal, | |
20 * you can use the Update API to update the data. This API is described in brief | |
21 * here; for more details, see https://www.drupal.org/node/2535316. If you are | |
22 * updating your module for a major version of Drupal (for instance, Drupal 7 to | |
23 * Drupal 8), updates will not run and you will need to use the | |
24 * @link migrate Migrate API @endlink instead. | |
25 * | |
26 * @section sec_when When to write update code | |
27 * You need to provide code that performs an update to stored data whenever your | |
28 * module makes a change to its data model. A data model change is any change | |
29 * that makes stored data on an existing site incompatible with that site's | |
30 * updated codebase. Examples: | |
31 * - Configuration changes: adding/removing/renaming a config key, changing the | |
32 * expected data type or value structure, changing dependencies, schema | |
33 * changes, etc. | |
34 * - Database schema changes: adding, changing, or removing a database table or | |
35 * field; moving stored data to different fields or tables; changing the | |
36 * format of stored data. | |
37 * - Content entity or field changes: adding, changing, or removing a field | |
38 * definition, entity definition, or any of their properties. | |
39 * | |
40 * @section sec_how How to write update code | |
41 * Update code for a module is put into an implementation of hook_update_N(), | |
42 * which goes into file mymodule.install (if your module's machine name is | |
43 * mymodule). See the documentation of hook_update_N() and | |
44 * https://www.drupal.org/node/2535316 for details and examples. | |
45 * | |
46 * @section sec_test Testing update code | |
47 * Update code should be tested both manually and by writing an automated test. | |
48 * Automated tests for update code extend | |
49 * \Drupal\system\Tests\Update\UpdatePathTestBase -- see that class for details, | |
50 * and find classes that extend it for examples. | |
51 * | |
52 * @see migration | |
53 * @} | |
54 */ | |
55 | |
56 /** | |
57 * @addtogroup hooks | |
58 * @{ | |
59 */ | |
60 | |
61 /** | |
62 * Defines one or more hooks that are exposed by a module. | |
63 * | |
64 * Normally hooks do not need to be explicitly defined. However, by declaring a | |
65 * hook explicitly, a module may define a "group" for it. Modules that implement | |
66 * a hook may then place their implementation in either $module.module or in | |
67 * $module.$group.inc. If the hook is located in $module.$group.inc, then that | |
68 * file will be automatically loaded when needed. | |
69 * In general, hooks that are rarely invoked and/or are very large should be | |
70 * placed in a separate include file, while hooks that are very short or very | |
71 * frequently called should be left in the main module file so that they are | |
72 * always available. | |
73 * | |
74 * See system_hook_info() for all hook groups defined by Drupal core. | |
75 * | |
76 * @return | |
77 * An associative array whose keys are hook names and whose values are an | |
78 * associative array containing: | |
79 * - group: A string defining the group to which the hook belongs. The module | |
80 * system will determine whether a file with the name $module.$group.inc | |
81 * exists, and automatically load it when required. | |
82 * | |
83 * @see hook_hook_info_alter() | |
84 */ | |
85 function hook_hook_info() { | |
86 $hooks['token_info'] = [ | |
87 'group' => 'tokens', | |
88 ]; | |
89 $hooks['tokens'] = [ | |
90 'group' => 'tokens', | |
91 ]; | |
92 return $hooks; | |
93 } | |
94 | |
95 /** | |
96 * Alter the registry of modules implementing a hook. | |
97 * | |
98 * This hook is invoked during \Drupal::moduleHandler()->getImplementations(). | |
99 * A module may implement this hook in order to reorder the implementing | |
100 * modules, which are otherwise ordered by the module's system weight. | |
101 * | |
102 * Note that hooks invoked using \Drupal::moduleHandler->alter() can have | |
103 * multiple variations(such as hook_form_alter() and hook_form_FORM_ID_alter()). | |
104 * \Drupal::moduleHandler->alter() will call all such variants defined by a | |
105 * single module in turn. For the purposes of hook_module_implements_alter(), | |
106 * these variants are treated as a single hook. Thus, to ensure that your | |
107 * implementation of hook_form_FORM_ID_alter() is called at the right time, | |
108 * you will have to change the order of hook_form_alter() implementation in | |
109 * hook_module_implements_alter(). | |
110 * | |
111 * @param $implementations | |
112 * An array keyed by the module's name. The value of each item corresponds | |
113 * to a $group, which is usually FALSE, unless the implementation is in a | |
114 * file named $module.$group.inc. | |
115 * @param $hook | |
116 * The name of the module hook being implemented. | |
117 */ | |
118 function hook_module_implements_alter(&$implementations, $hook) { | |
119 if ($hook == 'form_alter') { | |
120 // Move my_module_form_alter() to the end of the list. | |
121 // \Drupal::moduleHandler()->getImplementations() | |
122 // iterates through $implementations with a foreach loop which PHP iterates | |
123 // in the order that the items were added, so to move an item to the end of | |
124 // the array, we remove it and then add it. | |
125 $group = $implementations['my_module']; | |
126 unset($implementations['my_module']); | |
127 $implementations['my_module'] = $group; | |
128 } | |
129 } | |
130 | |
131 /** | |
132 * Alter the information parsed from module and theme .info.yml files. | |
133 * | |
134 * This hook is invoked in _system_rebuild_module_data() and in | |
135 * \Drupal\Core\Extension\ThemeHandlerInterface::rebuildThemeData(). A module | |
136 * may implement this hook in order to add to or alter the data generated by | |
137 * reading the .info.yml file with \Drupal\Core\Extension\InfoParser. | |
138 * | |
139 * Using implementations of this hook to make modules required by setting the | |
140 * $info['required'] key is discouraged. Doing so will slow down the module | |
141 * installation and uninstallation process. Instead, use | |
142 * \Drupal\Core\Extension\ModuleUninstallValidatorInterface. | |
143 * | |
144 * @param array $info | |
145 * The .info.yml file contents, passed by reference so that it can be altered. | |
146 * @param \Drupal\Core\Extension\Extension $file | |
147 * Full information about the module or theme. | |
148 * @param string $type | |
149 * Either 'module' or 'theme', depending on the type of .info.yml file that | |
150 * was passed. | |
151 * | |
152 * @see \Drupal\Core\Extension\ModuleUninstallValidatorInterface | |
153 */ | |
154 function hook_system_info_alter(array &$info, \Drupal\Core\Extension\Extension $file, $type) { | |
155 // Only fill this in if the .info.yml file does not define a 'datestamp'. | |
156 if (empty($info['datestamp'])) { | |
157 $info['datestamp'] = $file->getMTime(); | |
158 } | |
159 } | |
160 | |
161 /** | |
162 * Perform necessary actions before a module is installed. | |
163 * | |
164 * @param string $module | |
165 * The name of the module about to be installed. | |
166 */ | |
167 function hook_module_preinstall($module) { | |
168 mymodule_cache_clear(); | |
169 } | |
170 | |
171 /** | |
172 * Perform necessary actions after modules are installed. | |
173 * | |
174 * This function differs from hook_install() in that it gives all other modules | |
175 * a chance to perform actions when a module is installed, whereas | |
176 * hook_install() is only called on the module actually being installed. See | |
177 * \Drupal\Core\Extension\ModuleInstaller::install() for a detailed description of | |
178 * the order in which install hooks are invoked. | |
179 * | |
180 * This hook should be implemented in a .module file, not in an .install file. | |
181 * | |
182 * @param $modules | |
183 * An array of the modules that were installed. | |
184 * | |
185 * @see \Drupal\Core\Extension\ModuleInstaller::install() | |
186 * @see hook_install() | |
187 */ | |
188 function hook_modules_installed($modules) { | |
189 if (in_array('lousy_module', $modules)) { | |
190 \Drupal::state()->set('mymodule.lousy_module_compatibility', TRUE); | |
191 } | |
192 } | |
193 | |
194 /** | |
195 * Perform setup tasks when the module is installed. | |
196 * | |
197 * If the module implements hook_schema(), the database tables will | |
198 * be created before this hook is fired. | |
199 * | |
200 * If the module provides a MODULE.routing.yml or alters routing information | |
201 * these changes will not be available when this hook is fired. If up-to-date | |
202 * router information is required, for example to use \Drupal\Core\Url, then | |
203 * (preferably) use hook_modules_installed() or rebuild the router in the | |
204 * hook_install() implementation. | |
205 * | |
206 * Implementations of this hook are by convention declared in the module's | |
207 * .install file. The implementation can rely on the .module file being loaded. | |
208 * The hook will only be called when a module is installed. The module's schema | |
209 * version will be set to the module's greatest numbered update hook. Because of | |
210 * this, any time a hook_update_N() is added to the module, this function needs | |
211 * to be updated to reflect the current version of the database schema. | |
212 * | |
213 * See the @link https://www.drupal.org/node/146843 Schema API documentation | |
214 * @endlink for details on hook_schema and how database tables are defined. | |
215 * | |
216 * Note that since this function is called from a full bootstrap, all functions | |
217 * (including those in modules enabled by the current page request) are | |
218 * available when this hook is called. Use cases could be displaying a user | |
219 * message, or calling a module function necessary for initial setup, etc. | |
220 * | |
221 * Please be sure that anything added or modified in this function that can | |
222 * be removed during uninstall should be removed with hook_uninstall(). | |
223 * | |
224 * @see hook_schema() | |
225 * @see \Drupal\Core\Extension\ModuleInstaller::install() | |
226 * @see hook_uninstall() | |
227 * @see hook_modules_installed() | |
228 */ | |
229 function hook_install() { | |
230 // Create the styles directory and ensure it's writable. | |
231 $directory = file_default_scheme() . '://styles'; | |
232 file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); | |
233 } | |
234 | |
235 /** | |
236 * Perform necessary actions before a module is uninstalled. | |
237 * | |
238 * @param string $module | |
239 * The name of the module about to be uninstalled. | |
240 */ | |
241 function hook_module_preuninstall($module) { | |
242 mymodule_cache_clear(); | |
243 } | |
244 | |
245 /** | |
246 * Perform necessary actions after modules are uninstalled. | |
247 * | |
248 * This function differs from hook_uninstall() in that it gives all other | |
249 * modules a chance to perform actions when a module is uninstalled, whereas | |
250 * hook_uninstall() is only called on the module actually being uninstalled. | |
251 * | |
252 * It is recommended that you implement this hook if your module stores | |
253 * data that may have been set by other modules. | |
254 * | |
255 * @param $modules | |
256 * An array of the modules that were uninstalled. | |
257 * | |
258 * @see hook_uninstall() | |
259 */ | |
260 function hook_modules_uninstalled($modules) { | |
261 if (in_array('lousy_module', $modules)) { | |
262 \Drupal::state()->delete('mymodule.lousy_module_compatibility'); | |
263 } | |
264 mymodule_cache_rebuild(); | |
265 } | |
266 | |
267 /** | |
268 * Remove any information that the module sets. | |
269 * | |
270 * The information that the module should remove includes: | |
271 * - state that the module has set using \Drupal::state() | |
272 * - modifications to existing tables | |
273 * | |
274 * The module should not remove its entry from the module configuration. | |
275 * Database tables defined by hook_schema() will be removed automatically. | |
276 * | |
277 * The uninstall hook must be implemented in the module's .install file. It | |
278 * will fire when the module gets uninstalled but before the module's database | |
279 * tables are removed, allowing your module to query its own tables during | |
280 * this routine. | |
281 * | |
282 * @see hook_install() | |
283 * @see hook_schema() | |
284 * @see hook_modules_uninstalled() | |
285 */ | |
286 function hook_uninstall() { | |
287 // Remove the styles directory and generated images. | |
288 file_unmanaged_delete_recursive(file_default_scheme() . '://styles'); | |
289 } | |
290 | |
291 /** | |
292 * Return an array of tasks to be performed by an installation profile. | |
293 * | |
294 * Any tasks you define here will be run, in order, after the installer has | |
295 * finished the site configuration step but before it has moved on to the | |
296 * final import of languages and the end of the installation. This is invoked | |
297 * by install_tasks(). You can have any number of custom tasks to perform | |
298 * during this phase. | |
299 * | |
300 * Each task you define here corresponds to a callback function which you must | |
301 * separately define and which is called when your task is run. This function | |
302 * will receive the global installation state variable, $install_state, as | |
303 * input, and has the opportunity to access or modify any of its settings. See | |
304 * the install_state_defaults() function in the installer for the list of | |
305 * $install_state settings used by Drupal core. | |
306 * | |
307 * At the end of your task function, you can indicate that you want the | |
308 * installer to pause and display a page to the user by returning any themed | |
309 * output that should be displayed on that page (but see below for tasks that | |
310 * use the form API or batch API; the return values of these task functions are | |
311 * handled differently). You should also use #title within the task | |
312 * callback function to set a custom page title. For some tasks, however, you | |
313 * may want to simply do some processing and pass control to the next task | |
314 * without ending the page request; to indicate this, simply do not send back | |
315 * a return value from your task function at all. This can be used, for | |
316 * example, by installation profiles that need to configure certain site | |
317 * settings in the database without obtaining any input from the user. | |
318 * | |
319 * The task function is treated specially if it defines a form or requires | |
320 * batch processing; in that case, you should return either the form API | |
321 * definition or batch API array, as appropriate. See below for more | |
322 * information on the 'type' key that you must define in the task definition | |
323 * to inform the installer that your task falls into one of those two | |
324 * categories. It is important to use these APIs directly, since the installer | |
325 * may be run non-interactively (for example, via a command line script), all | |
326 * in one page request; in that case, the installer will automatically take | |
327 * care of submitting forms and processing batches correctly for both types of | |
328 * installations. You can inspect the $install_state['interactive'] boolean to | |
329 * see whether or not the current installation is interactive, if you need | |
330 * access to this information. | |
331 * | |
332 * Remember that a user installing Drupal interactively will be able to reload | |
333 * an installation page multiple times, so you should use \Drupal::state() to | |
334 * store any data that you may need later in the installation process. Any | |
335 * temporary state must be removed using \Drupal::state()->delete() before | |
336 * your last task has completed and control is handed back to the installer. | |
337 * | |
338 * @param array $install_state | |
339 * An array of information about the current installation state. | |
340 * | |
341 * @return array | |
342 * A keyed array of tasks the profile will perform during the final stage of | |
343 * the installation. Each key represents the name of a function (usually a | |
344 * function defined by this profile, although that is not strictly required) | |
345 * that is called when that task is run. The values are associative arrays | |
346 * containing the following key-value pairs (all of which are optional): | |
347 * - display_name: The human-readable name of the task. This will be | |
348 * displayed to the user while the installer is running, along with a list | |
349 * of other tasks that are being run. Leave this unset to prevent the task | |
350 * from appearing in the list. | |
351 * - display: This is a boolean which can be used to provide finer-grained | |
352 * control over whether or not the task will display. This is mostly useful | |
353 * for tasks that are intended to display only under certain conditions; | |
354 * for these tasks, you can set 'display_name' to the name that you want to | |
355 * display, but then use this boolean to hide the task only when certain | |
356 * conditions apply. | |
357 * - type: A string representing the type of task. This parameter has three | |
358 * possible values: | |
359 * - normal: (default) This indicates that the task will be treated as a | |
360 * regular callback function, which does its processing and optionally | |
361 * returns HTML output. | |
362 * - batch: This indicates that the task function will return a batch API | |
363 * definition suitable for batch_set() or an array of batch definitions | |
364 * suitable for consecutive batch_set() calls. The installer will then | |
365 * take care of automatically running the task via batch processing. | |
366 * - form: This indicates that the task function will return a standard | |
367 * form API definition (and separately define validation and submit | |
368 * handlers, as appropriate). The installer will then take care of | |
369 * automatically directing the user through the form submission process. | |
370 * - run: A constant representing the manner in which the task will be run. | |
371 * This parameter has three possible values: | |
372 * - INSTALL_TASK_RUN_IF_NOT_COMPLETED: (default) This indicates that the | |
373 * task will run once during the installation of the profile. | |
374 * - INSTALL_TASK_SKIP: This indicates that the task will not run during | |
375 * the current installation page request. It can be used to skip running | |
376 * an installation task when certain conditions are met, even though the | |
377 * task may still show on the list of installation tasks presented to the | |
378 * user. | |
379 * - INSTALL_TASK_RUN_IF_REACHED: This indicates that the task will run on | |
380 * each installation page request that reaches it. This is rarely | |
381 * necessary for an installation profile to use; it is primarily used by | |
382 * the Drupal installer for bootstrap-related tasks. | |
383 * - function: Normally this does not need to be set, but it can be used to | |
384 * force the installer to call a different function when the task is run | |
385 * (rather than the function whose name is given by the array key). This | |
386 * could be used, for example, to allow the same function to be called by | |
387 * two different tasks. | |
388 * | |
389 * @see install_state_defaults() | |
390 * @see batch_set() | |
391 * @see hook_install_tasks_alter() | |
392 * @see install_tasks() | |
393 */ | |
394 function hook_install_tasks(&$install_state) { | |
395 // Here, we define a variable to allow tasks to indicate that a particular, | |
396 // processor-intensive batch process needs to be triggered later on in the | |
397 // installation. | |
398 $myprofile_needs_batch_processing = \Drupal::state()->get('myprofile.needs_batch_processing', FALSE); | |
399 $tasks = [ | |
400 // This is an example of a task that defines a form which the user who is | |
401 // installing the site will be asked to fill out. To implement this task, | |
402 // your profile would define a function named myprofile_data_import_form() | |
403 // as a normal form API callback function, with associated validation and | |
404 // submit handlers. In the submit handler, in addition to saving whatever | |
405 // other data you have collected from the user, you might also call | |
406 // \Drupal::state()->set('myprofile.needs_batch_processing', TRUE) if the | |
407 // user has entered data which requires that batch processing will need to | |
408 // occur later on. | |
409 'myprofile_data_import_form' => [ | |
410 'display_name' => t('Data import options'), | |
411 'type' => 'form', | |
412 ], | |
413 // Similarly, to implement this task, your profile would define a function | |
414 // named myprofile_settings_form() with associated validation and submit | |
415 // handlers. This form might be used to collect and save additional | |
416 // information from the user that your profile needs. There are no extra | |
417 // steps required for your profile to act as an "installation wizard"; you | |
418 // can simply define as many tasks of type 'form' as you wish to execute, | |
419 // and the forms will be presented to the user, one after another. | |
420 'myprofile_settings_form' => [ | |
421 'display_name' => t('Additional options'), | |
422 'type' => 'form', | |
423 ], | |
424 // This is an example of a task that performs batch operations. To | |
425 // implement this task, your profile would define a function named | |
426 // myprofile_batch_processing() which returns a batch API array definition | |
427 // that the installer will use to execute your batch operations. Due to the | |
428 // 'myprofile.needs_batch_processing' variable used here, this task will be | |
429 // hidden and skipped unless your profile set it to TRUE in one of the | |
430 // previous tasks. | |
431 'myprofile_batch_processing' => [ | |
432 'display_name' => t('Import additional data'), | |
433 'display' => $myprofile_needs_batch_processing, | |
434 'type' => 'batch', | |
435 'run' => $myprofile_needs_batch_processing ? INSTALL_TASK_RUN_IF_NOT_COMPLETED : INSTALL_TASK_SKIP, | |
436 ], | |
437 // This is an example of a task that will not be displayed in the list that | |
438 // the user sees. To implement this task, your profile would define a | |
439 // function named myprofile_final_site_setup(), in which additional, | |
440 // automated site setup operations would be performed. Since this is the | |
441 // last task defined by your profile, you should also use this function to | |
442 // call \Drupal::state()->delete('myprofile.needs_batch_processing') and | |
443 // clean up the state that was used above. If you want the user to pass | |
444 // to the final Drupal installation tasks uninterrupted, return no output | |
445 // from this function. Otherwise, return themed output that the user will | |
446 // see (for example, a confirmation page explaining that your profile's | |
447 // tasks are complete, with a link to reload the current page and therefore | |
448 // pass on to the final Drupal installation tasks when the user is ready to | |
449 // do so). | |
450 'myprofile_final_site_setup' => [], | |
451 ]; | |
452 return $tasks; | |
453 } | |
454 | |
455 /** | |
456 * Alter the full list of installation tasks. | |
457 * | |
458 * You can use this hook to change or replace any part of the Drupal | |
459 * installation process that occurs after the installation profile is selected. | |
460 * | |
461 * This hook is invoked on the install profile in install_tasks(). | |
462 * | |
463 * @param $tasks | |
464 * An array of all available installation tasks, including those provided by | |
465 * Drupal core. You can modify this array to change or replace individual | |
466 * steps within the installation process. | |
467 * @param $install_state | |
468 * An array of information about the current installation state. | |
469 * | |
470 * @see hook_install_tasks() | |
471 * @see install_tasks() | |
472 */ | |
473 function hook_install_tasks_alter(&$tasks, $install_state) { | |
474 // Replace the entire site configuration form provided by Drupal core | |
475 // with a custom callback function defined by this installation profile. | |
476 $tasks['install_configure_form']['function'] = 'myprofile_install_configure_form'; | |
477 } | |
478 | |
479 /** | |
480 * Perform a single update between minor versions. | |
481 * | |
482 * hook_update_N() can only be used to update between minor versions of a | |
483 * module. To upgrade between major versions of Drupal (for example, between | |
484 * Drupal 7 and 8), use the @link migrate Migrate API @endlink instead. | |
485 * | |
486 * @section sec_naming Naming and documenting your function | |
487 * For each change in a module that requires one or more actions to be performed | |
488 * when updating a site, add a new implementation of hook_update_N() to your | |
489 * mymodule.install file (assuming mymodule is the machine name of your module). | |
490 * Implementations of hook_update_N() are named (module name)_update_(number). | |
491 * The numbers are normally composed of three parts: | |
492 * - 1 or 2 digits for Drupal core compatibility (Drupal 8, 9, 10, etc.). This | |
493 * convention must be followed. | |
494 * - 1 digit for your module's major release version; for example, for 8.x-1.* | |
495 * use 1, for 8.x-2.* use 2, for Core 8.0.x use 0, and for Core 8.1.x use 1. | |
496 * This convention is optional but suggested for clarity. | |
497 * - 2 digits for sequential counting, starting with 01. Note that the x000 | |
498 * number can never be used: the lowest update number that will be recognized | |
499 * and run for major version x is x001. | |
500 * Examples: | |
501 * - node_update_8001(): The first update for the Drupal 8.0.x version of the | |
502 * Drupal Core node module. | |
503 * - mymodule_update_8101(): The first update for your custom or contributed | |
504 * module's 8.x-1.x versions. | |
505 * - mymodule_update_8201(): The first update for the 8.x-2.x versions. | |
506 * | |
507 * Never renumber update functions. The numeric part of the hook implementation | |
508 * function is stored in the database to keep track of which updates have run, | |
509 * so it is important to maintain this information consistently. | |
510 * | |
511 * The documentation block preceding this function is stripped of newlines and | |
512 * used as the description for the update on the pending updates task list, | |
513 * which users will see when they run the update.php script. | |
514 * | |
515 * @section sec_notes Notes about the function body | |
516 * Writing hook_update_N() functions is tricky. There are several reasons why | |
517 * this is the case: | |
518 * - You do not know when updates will be run: someone could be keeping up with | |
519 * every update and run them when the database and code are in the same state | |
520 * as when you wrote your update function, or they could have waited until a | |
521 * few more updates have come out, and run several at the same time. | |
522 * - You do not know the state of other modules' updates either. | |
523 * - Other modules can use hook_update_dependencies() to run updates between | |
524 * your module's updates, so you also cannot count on your functions running | |
525 * right after one another. | |
526 * - You do not know what environment your update will run in (which modules | |
527 * are installed, whether certain hooks are implemented or not, whether | |
528 * services are overridden, etc.). | |
529 * | |
530 * Because of these reasons, you'll need to use care in writing your update | |
531 * function. Some things to think about: | |
532 * - Never assume that the database schema is the same when the update will run | |
533 * as it is when you wrote the update function. So, when updating a database | |
534 * table or field, put the schema information you want to update to directly | |
535 * into your function instead of calling your hook_schema() function to | |
536 * retrieve it (this is one case where the right thing to do is copy and paste | |
537 * the code). | |
538 * - Never assume that the configuration schema is the same when the update will | |
539 * run as it is when you wrote the update function. So, when saving | |
540 * configuration, use the $has_trusted_data = TRUE parameter so that schema is | |
541 * ignored, and make sure that the configuration data you are saving matches | |
542 * the configuration schema at the time when you write the update function | |
543 * (later updates may change it again to match new schema changes). | |
544 * - Never assume your field or entity type definitions are the same when the | |
545 * update will run as they are when you wrote the update function. Always | |
546 * retrieve the correct version via | |
547 * \Drupal::entityDefinitionUpdateManager()::getEntityType() or | |
548 * \Drupal::entityDefinitionUpdateManager()::getFieldStorageDefinition(). When | |
549 * adding a new definition always replicate it in the update function body as | |
550 * you would do with a schema definition. | |
551 * - Never call \Drupal::entityDefinitionUpdateManager()::applyUpdates() in an | |
552 * update function, as it will apply updates for any module not only yours, | |
553 * which will lead to unpredictable results. | |
554 * - Be careful about API functions and especially CRUD operations that you use | |
555 * in your update function. If they invoke hooks or use services, they may | |
556 * not behave as expected, and it may actually not be appropriate to use the | |
557 * normal API functions that invoke all the hooks, use the database schema, | |
558 * and/or use services in an update function -- you may need to switch to | |
559 * using a more direct method (database query, etc.). | |
560 * - In particular, loading, saving, or performing any other CRUD operation on | |
561 * an entity is never safe to do (they always involve hooks and services). | |
562 * - Never rebuild the router during an update function. | |
563 * | |
564 * The following actions are examples of things that are safe to do during | |
565 * updates: | |
566 * - Cache invalidation. | |
567 * - Using \Drupal::configFactory()->getEditable() and \Drupal::config(), as | |
568 * long as you make sure that your update data matches the schema, and you | |
569 * use the $has_trusted_data argument in the save operation. | |
570 * - Marking a container for rebuild. | |
571 * - Using the API provided by \Drupal::entityDefinitionUpdateManager() to | |
572 * update the entity schema based on changes in entity type or field | |
573 * definitions provided by your module. | |
574 * | |
575 * See https://www.drupal.org/node/2535316 for more on writing update functions. | |
576 * | |
577 * @section sec_bulk Batch updates | |
578 * If running your update all at once could possibly cause PHP to time out, use | |
579 * the $sandbox parameter to indicate that the Batch API should be used for your | |
580 * update. In this case, your update function acts as an implementation of | |
581 * callback_batch_operation(), and $sandbox acts as the batch context | |
582 * parameter. In your function, read the state information from the previous | |
583 * run from $sandbox (or initialize), run a chunk of updates, save the state in | |
584 * $sandbox, and set $sandbox['#finished'] to a value between 0 and 1 to | |
585 * indicate the percent completed, or 1 if it is finished (you need to do this | |
586 * explicitly in each pass). | |
587 * | |
588 * See the @link batch Batch operations topic @endlink for more information on | |
589 * how to use the Batch API. | |
590 * | |
591 * @param array $sandbox | |
592 * Stores information for batch updates. See above for more information. | |
593 * | |
594 * @return string|null | |
595 * Optionally, update hooks may return a translated string that will be | |
596 * displayed to the user after the update has completed. If no message is | |
597 * returned, no message will be presented to the user. | |
598 * | |
599 * @throws \Drupal\Core\Utility\UpdateException|PDOException | |
600 * In case of error, update hooks should throw an instance of | |
601 * Drupal\Core\Utility\UpdateException with a meaningful message for the user. | |
602 * If a database query fails for whatever reason, it will throw a | |
603 * PDOException. | |
604 * | |
605 * @ingroup update_api | |
606 * | |
607 * @see batch | |
608 * @see schemaapi | |
609 * @see hook_update_last_removed() | |
610 * @see update_get_update_list() | |
611 * @see \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface | |
612 * @see node_update_8001 | |
613 * @see system_update_8004 | |
614 * @see https://www.drupal.org/node/2535316 | |
615 */ | |
616 function hook_update_N(&$sandbox) { | |
617 // For non-batch updates, the signature can simply be: | |
618 // function hook_update_N() { | |
619 | |
620 // Example function body for adding a field to a database table, which does | |
621 // not require a batch operation: | |
622 $spec = [ | |
623 'type' => 'varchar', | |
624 'description' => "New Col", | |
625 'length' => 20, | |
626 'not null' => FALSE, | |
627 ]; | |
628 $schema = Database::getConnection()->schema(); | |
629 $schema->addField('mytable1', 'newcol', $spec); | |
630 | |
631 // Example of what to do if there is an error during your update. | |
632 if ($some_error_condition_met) { | |
633 throw new UpdateException('Something went wrong; here is what you should do.'); | |
634 } | |
635 | |
636 // Example function body for a batch update. In this example, the values in | |
637 // a database field are updated. | |
638 if (!isset($sandbox['progress'])) { | |
639 // This must be the first run. Initialize the sandbox. | |
640 $sandbox['progress'] = 0; | |
641 $sandbox['current_pk'] = 0; | |
642 $sandbox['max'] = Database::getConnection()->query('SELECT COUNT(myprimarykey) FROM {mytable1}')->fetchField() - 1; | |
643 } | |
644 | |
645 // Update in chunks of 20. | |
646 $records = Database::getConnection()->select('mytable1', 'm') | |
647 ->fields('m', ['myprimarykey', 'otherfield']) | |
648 ->condition('myprimarykey', $sandbox['current_pk'], '>') | |
649 ->range(0, 20) | |
650 ->orderBy('myprimarykey', 'ASC') | |
651 ->execute(); | |
652 foreach ($records as $record) { | |
653 // Here, you would make an update something related to this record. In this | |
654 // example, some text is added to the other field. | |
655 Database::getConnection()->update('mytable1') | |
656 ->fields(['otherfield' => $record->otherfield . '-suffix']) | |
657 ->condition('myprimarykey', $record->myprimarykey) | |
658 ->execute(); | |
659 | |
660 $sandbox['progress']++; | |
661 $sandbox['current_pk'] = $record->myprimarykey; | |
662 } | |
663 | |
664 $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); | |
665 | |
666 // To display a message to the user when the update is completed, return it. | |
667 // If you do not want to display a completion message, return nothing. | |
668 return t('All foo bars were updated with the new suffix'); | |
669 } | |
670 | |
671 /** | |
672 * Executes an update which is intended to update data, like entities. | |
673 * | |
674 * These implementations have to be placed in a MODULE.post_update.php file. | |
675 * | |
676 * These updates are executed after all hook_update_N() implementations. At this | |
677 * stage Drupal is already fully repaired so you can use any API as you wish. | |
678 * | |
679 * NAME can be arbitrary machine names. In contrast to hook_update_N() the order | |
680 * of functions in the file is the only thing which ensures the execution order | |
681 * of those functions. | |
682 * | |
683 * Drupal also ensures to not execute the same hook_post_update_NAME() function | |
684 * twice. | |
685 * | |
686 * @param array $sandbox | |
687 * Stores information for batch updates. See above for more information. | |
688 * | |
689 * @return string|null | |
690 * Optionally, hook_post_update_NAME() hooks may return a translated string | |
691 * that will be displayed to the user after the update has completed. If no | |
692 * message is returned, no message will be presented to the user. | |
693 * | |
694 * @throws \Drupal\Core\Utility\UpdateException|PDOException | |
695 * In case of error, update hooks should throw an instance of | |
696 * \Drupal\Core\Utility\UpdateException with a meaningful message for the | |
697 * user. If a database query fails for whatever reason, it will throw a | |
698 * PDOException. | |
699 * | |
700 * @ingroup update_api | |
701 * | |
702 * @see hook_update_N() | |
703 */ | |
704 function hook_post_update_NAME(&$sandbox) { | |
705 // Example of updating some content. | |
706 $node = \Drupal\node\Entity\Node::load(123); | |
707 $node->setTitle('foo'); | |
708 $node->save(); | |
709 | |
710 $result = t('Node %nid saved', ['%nid' => $node->id()]); | |
711 | |
712 // Example of disabling blocks with missing condition contexts. Note: The | |
713 // block itself is in a state which is valid at that point. | |
714 // @see block_update_8001() | |
715 // @see block_post_update_disable_blocks_with_missing_contexts() | |
716 $block_update_8001 = \Drupal::keyValue('update_backup')->get('block_update_8001', []); | |
717 | |
718 $block_ids = array_keys($block_update_8001); | |
719 $block_storage = \Drupal::entityManager()->getStorage('block'); | |
720 $blocks = $block_storage->loadMultiple($block_ids); | |
721 /** @var $blocks \Drupal\block\BlockInterface[] */ | |
722 foreach ($blocks as $block) { | |
723 // This block has had conditions removed due to an inability to resolve | |
724 // contexts in block_update_8001() so disable it. | |
725 | |
726 // Disable currently enabled blocks. | |
727 if ($block_update_8001[$block->id()]['status']) { | |
728 $block->setStatus(FALSE); | |
729 $block->save(); | |
730 } | |
731 } | |
732 | |
733 return $result; | |
734 } | |
735 | |
736 /** | |
737 * Return an array of information about module update dependencies. | |
738 * | |
739 * This can be used to indicate update functions from other modules that your | |
740 * module's update functions depend on, or vice versa. It is used by the update | |
741 * system to determine the appropriate order in which updates should be run, as | |
742 * well as to search for missing dependencies. | |
743 * | |
744 * Implementations of this hook should be placed in a mymodule.install file in | |
745 * the same directory as mymodule.module. | |
746 * | |
747 * @return | |
748 * A multidimensional array containing information about the module update | |
749 * dependencies. The first two levels of keys represent the module and update | |
750 * number (respectively) for which information is being returned, and the | |
751 * value is an array of information about that update's dependencies. Within | |
752 * this array, each key represents a module, and each value represents the | |
753 * number of an update function within that module. In the event that your | |
754 * update function depends on more than one update from a particular module, | |
755 * you should always list the highest numbered one here (since updates within | |
756 * a given module always run in numerical order). | |
757 * | |
758 * @ingroup update_api | |
759 * | |
760 * @see update_resolve_dependencies() | |
761 * @see hook_update_N() | |
762 */ | |
763 function hook_update_dependencies() { | |
764 // Indicate that the mymodule_update_8001() function provided by this module | |
765 // must run after the another_module_update_8003() function provided by the | |
766 // 'another_module' module. | |
767 $dependencies['mymodule'][8001] = [ | |
768 'another_module' => 8003, | |
769 ]; | |
770 // Indicate that the mymodule_update_8002() function provided by this module | |
771 // must run before the yet_another_module_update_8005() function provided by | |
772 // the 'yet_another_module' module. (Note that declaring dependencies in this | |
773 // direction should be done only in rare situations, since it can lead to the | |
774 // following problem: If a site has already run the yet_another_module | |
775 // module's database updates before it updates its codebase to pick up the | |
776 // newest mymodule code, then the dependency declared here will be ignored.) | |
777 $dependencies['yet_another_module'][8005] = [ | |
778 'mymodule' => 8002, | |
779 ]; | |
780 return $dependencies; | |
781 } | |
782 | |
783 /** | |
784 * Return a number which is no longer available as hook_update_N(). | |
785 * | |
786 * If you remove some update functions from your mymodule.install file, you | |
787 * should notify Drupal of those missing functions. This way, Drupal can | |
788 * ensure that no update is accidentally skipped. | |
789 * | |
790 * Implementations of this hook should be placed in a mymodule.install file in | |
791 * the same directory as mymodule.module. | |
792 * | |
793 * @return | |
794 * An integer, corresponding to hook_update_N() which has been removed from | |
795 * mymodule.install. | |
796 * | |
797 * @ingroup update_api | |
798 * | |
799 * @see hook_update_N() | |
800 */ | |
801 function hook_update_last_removed() { | |
802 // We've removed the 8.x-1.x version of mymodule, including database updates. | |
803 // The next update function is mymodule_update_8200(). | |
804 return 8103; | |
805 } | |
806 | |
807 /** | |
808 * Provide information on Updaters (classes that can update Drupal). | |
809 * | |
810 * Drupal\Core\Updater\Updater is a class that knows how to update various parts | |
811 * of the Drupal file system, for example to update modules that have newer | |
812 * releases, or to install a new theme. | |
813 * | |
814 * @return | |
815 * An associative array of information about the updater(s) being provided. | |
816 * This array is keyed by a unique identifier for each updater, and the | |
817 * values are subarrays that can contain the following keys: | |
818 * - class: The name of the PHP class which implements this updater. | |
819 * - name: Human-readable name of this updater. | |
820 * - weight: Controls what order the Updater classes are consulted to decide | |
821 * which one should handle a given task. When an update task is being run, | |
822 * the system will loop through all the Updater classes defined in this | |
823 * registry in weight order and let each class respond to the task and | |
824 * decide if each Updater wants to handle the task. In general, this | |
825 * doesn't matter, but if you need to override an existing Updater, make | |
826 * sure your Updater has a lighter weight so that it comes first. | |
827 * | |
828 * @ingroup update_api | |
829 * | |
830 * @see drupal_get_updaters() | |
831 * @see hook_updater_info_alter() | |
832 */ | |
833 function hook_updater_info() { | |
834 return [ | |
835 'module' => [ | |
836 'class' => 'Drupal\Core\Updater\Module', | |
837 'name' => t('Update modules'), | |
838 'weight' => 0, | |
839 ], | |
840 'theme' => [ | |
841 'class' => 'Drupal\Core\Updater\Theme', | |
842 'name' => t('Update themes'), | |
843 'weight' => 0, | |
844 ], | |
845 ]; | |
846 } | |
847 | |
848 /** | |
849 * Alter the Updater information array. | |
850 * | |
851 * An Updater is a class that knows how to update various parts of the Drupal | |
852 * file system, for example to update modules that have newer releases, or to | |
853 * install a new theme. | |
854 * | |
855 * @param array $updaters | |
856 * Associative array of updaters as defined through hook_updater_info(). | |
857 * Alter this array directly. | |
858 * | |
859 * @ingroup update_api | |
860 * | |
861 * @see drupal_get_updaters() | |
862 * @see hook_updater_info() | |
863 */ | |
864 function hook_updater_info_alter(&$updaters) { | |
865 // Adjust weight so that the theme Updater gets a chance to handle a given | |
866 // update task before module updaters. | |
867 $updaters['theme']['weight'] = -1; | |
868 } | |
869 | |
870 /** | |
871 * Check installation requirements and do status reporting. | |
872 * | |
873 * This hook has three closely related uses, determined by the $phase argument: | |
874 * - Checking installation requirements ($phase == 'install'). | |
875 * - Checking update requirements ($phase == 'update'). | |
876 * - Status reporting ($phase == 'runtime'). | |
877 * | |
878 * Note that this hook, like all others dealing with installation and updates, | |
879 * must reside in a module_name.install file, or it will not properly abort | |
880 * the installation of the module if a critical requirement is missing. | |
881 * | |
882 * During the 'install' phase, modules can for example assert that | |
883 * library or server versions are available or sufficient. | |
884 * Note that the installation of a module can happen during installation of | |
885 * Drupal itself (by install.php) with an installation profile or later by hand. | |
886 * As a consequence, install-time requirements must be checked without access | |
887 * to the full Drupal API, because it is not available during install.php. | |
888 * If a requirement has a severity of REQUIREMENT_ERROR, install.php will abort | |
889 * or at least the module will not install. | |
890 * Other severity levels have no effect on the installation. | |
891 * Module dependencies do not belong to these installation requirements, | |
892 * but should be defined in the module's .info.yml file. | |
893 * | |
894 * During installation (when $phase == 'install'), if you need to load a class | |
895 * from your module, you'll need to include the class file directly. | |
896 * | |
897 * The 'runtime' phase is not limited to pure installation requirements | |
898 * but can also be used for more general status information like maintenance | |
899 * tasks and security issues. | |
900 * The returned 'requirements' will be listed on the status report in the | |
901 * administration section, with indication of the severity level. | |
902 * Moreover, any requirement with a severity of REQUIREMENT_ERROR severity will | |
903 * result in a notice on the administration configuration page. | |
904 * | |
905 * @param $phase | |
906 * The phase in which requirements are checked: | |
907 * - install: The module is being installed. | |
908 * - update: The module is enabled and update.php is run. | |
909 * - runtime: The runtime requirements are being checked and shown on the | |
910 * status report page. | |
911 * | |
912 * @return | |
913 * An associative array where the keys are arbitrary but must be unique (it | |
914 * is suggested to use the module short name as a prefix) and the values are | |
915 * themselves associative arrays with the following elements: | |
916 * - title: The name of the requirement. | |
917 * - value: The current value (e.g., version, time, level, etc). During | |
918 * install phase, this should only be used for version numbers, do not set | |
919 * it if not applicable. | |
920 * - description: The description of the requirement/status. | |
921 * - severity: The requirement's result/severity level, one of: | |
922 * - REQUIREMENT_INFO: For info only. | |
923 * - REQUIREMENT_OK: The requirement is satisfied. | |
924 * - REQUIREMENT_WARNING: The requirement failed with a warning. | |
925 * - REQUIREMENT_ERROR: The requirement failed with an error. | |
926 */ | |
927 function hook_requirements($phase) { | |
928 $requirements = []; | |
929 | |
930 // Report Drupal version | |
931 if ($phase == 'runtime') { | |
932 $requirements['drupal'] = [ | |
933 'title' => t('Drupal'), | |
934 'value' => \Drupal::VERSION, | |
935 'severity' => REQUIREMENT_INFO | |
936 ]; | |
937 } | |
938 | |
939 // Test PHP version | |
940 $requirements['php'] = [ | |
941 'title' => t('PHP'), | |
942 'value' => ($phase == 'runtime') ? \Drupal::l(phpversion(), new Url('system.php')) : phpversion(), | |
943 ]; | |
944 if (version_compare(phpversion(), DRUPAL_MINIMUM_PHP) < 0) { | |
945 $requirements['php']['description'] = t('Your PHP installation is too old. Drupal requires at least PHP %version.', ['%version' => DRUPAL_MINIMUM_PHP]); | |
946 $requirements['php']['severity'] = REQUIREMENT_ERROR; | |
947 } | |
948 | |
949 // Report cron status | |
950 if ($phase == 'runtime') { | |
951 $cron_last = \Drupal::state()->get('system.cron_last'); | |
952 | |
953 if (is_numeric($cron_last)) { | |
954 $requirements['cron']['value'] = t('Last run @time ago', ['@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($cron_last)]); | |
955 } | |
956 else { | |
957 $requirements['cron'] = [ | |
958 'description' => t('Cron has not run. It appears cron jobs have not been setup on your system. Check the help pages for <a href=":url">configuring cron jobs</a>.', [':url' => 'https://www.drupal.org/cron']), | |
959 'severity' => REQUIREMENT_ERROR, | |
960 'value' => t('Never run'), | |
961 ]; | |
962 } | |
963 | |
964 $requirements['cron']['description'] .= ' ' . t('You can <a href=":cron">run cron manually</a>.', [':cron' => \Drupal::url('system.run_cron')]); | |
965 | |
966 $requirements['cron']['title'] = t('Cron maintenance tasks'); | |
967 } | |
968 | |
969 return $requirements; | |
970 } | |
971 | |
972 /** | |
973 * @} End of "addtogroup hooks". | |
974 */ |