update.api.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Update Manager module.
  5. */
  6. use Drupal\update\UpdateFetcherInterface;
  7. /**
  8. * @addtogroup hooks
  9. * @{
  10. */
  11. /**
  12. * Alter the list of projects before fetching data and comparing versions.
  13. *
  14. * Most modules will never need to implement this hook. It is for advanced
  15. * interaction with the Update Manager module. The primary use-case for this
  16. * hook is to add projects to the list; for example, to provide update status
  17. * data on disabled modules and themes. A contributed module might want to hide
  18. * projects from the list; for example, if there is a site-specific module that
  19. * doesn't have any official releases, that module could remove itself from this
  20. * list to avoid "No available releases found" warnings on the available updates
  21. * report. In rare cases, a module might want to alter the data associated with
  22. * a project already in the list.
  23. *
  24. * @param $projects
  25. * Reference to an array of the projects installed on the system. This
  26. * includes all the metadata documented in the comments below for each project
  27. * (either module or theme) that is currently enabled. The array is initially
  28. * populated inside \Drupal\update\UpdateManager::getProjects() with the help
  29. * of \Drupal\Core\Utility\ProjectInfo->processInfoList(), so look there for
  30. * examples of how to populate the array with real values.
  31. *
  32. * @see \Drupal\update\UpdateManager::getProjects()
  33. * @see \Drupal\Core\Utility\ProjectInfo::processInfoList()
  34. */
  35. function hook_update_projects_alter(&$projects) {
  36. // Hide a site-specific module from the list.
  37. unset($projects['site_specific_module']);
  38. // Add a disabled module to the list.
  39. // The key for the array should be the machine-readable project "short name".
  40. $projects['disabled_project_name'] = [
  41. // Machine-readable project short name (same as the array key above).
  42. 'name' => 'disabled_project_name',
  43. // Array of values from the main .info.yml file for this project.
  44. 'info' => [
  45. 'name' => 'Some disabled module',
  46. 'description' => 'A module not enabled on the site that you want to see in the available updates report.',
  47. 'version' => '8.x-1.0',
  48. 'core' => '8.x',
  49. // The maximum file change time (the "ctime" returned by the filectime()
  50. // PHP method) for all of the .info.yml files included in this project.
  51. '_info_file_ctime' => 1243888165,
  52. ],
  53. // The date stamp when the project was released, if known. If the disabled
  54. // project was an officially packaged release from drupal.org, this will
  55. // be included in the .info.yml file as the 'datestamp' field. This only
  56. // really matters for development snapshot releases that are regenerated,
  57. // so it can be left undefined or set to 0 in most cases.
  58. 'datestamp' => 1243888185,
  59. // Any modules (or themes) included in this project. Keyed by machine-
  60. // readable "short name", value is the human-readable project name printed
  61. // in the UI.
  62. 'includes' => [
  63. 'disabled_project' => 'Disabled module',
  64. 'disabled_project_helper' => 'Disabled module helper module',
  65. 'disabled_project_foo' => 'Disabled module foo add-on module',
  66. ],
  67. // Does this project contain a 'module', 'theme', 'disabled-module', or
  68. // 'disabled-theme'?
  69. 'project_type' => 'disabled-module',
  70. ];
  71. }
  72. /**
  73. * Alter the information about available updates for projects.
  74. *
  75. * @param $projects
  76. * Reference to an array of information about available updates to each
  77. * project installed on the system.
  78. *
  79. * @see update_calculate_project_data()
  80. */
  81. function hook_update_status_alter(&$projects) {
  82. $settings = \Drupal::config('update_advanced.settings')->get('projects');
  83. foreach ($projects as $project => $project_info) {
  84. if (isset($settings[$project]) && isset($settings[$project]['check']) &&
  85. ($settings[$project]['check'] == 'never' ||
  86. (isset($project_info['recommended']) &&
  87. $settings[$project]['check'] === $project_info['recommended']))) {
  88. $projects[$project]['status'] = UpdateFetcherInterface::NOT_CHECKED;
  89. $projects[$project]['reason'] = t('Ignored from settings');
  90. if (!empty($settings[$project]['notes'])) {
  91. $projects[$project]['extra'][] = [
  92. 'class' => ['admin-note'],
  93. 'label' => t('Administrator note'),
  94. 'data' => $settings[$project]['notes'],
  95. ];
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * Verify an archive after it has been downloaded and extracted.
  102. *
  103. * @param string $project
  104. * The short name of the project that has been downloaded.
  105. * @param string $archive_file
  106. * The filename of the unextracted archive.
  107. * @param string $directory
  108. * The directory that the archive was extracted into.
  109. *
  110. * @return
  111. * If there are any problems, return an array of error messages. If there are
  112. * no problems, return an empty array.
  113. *
  114. * @see update_manager_archive_verify()
  115. * @ingroup update_manager_file
  116. */
  117. function hook_verify_update_archive($project, $archive_file, $directory) {
  118. $errors = [];
  119. if (!file_exists($directory)) {
  120. $errors[] = t('The %directory does not exist.', ['%directory' => $directory]);
  121. }
  122. // Add other checks on the archive integrity here.
  123. return $errors;
  124. }
  125. /**
  126. * @} End of "addtogroup hooks".
  127. */