update.api.php 5.11 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Hooks provided by the Update Manager module.
6 7 8 9 10 11 12
 */

/**
 * @addtogroup hooks
 * @{
 */

13 14 15 16
/**
 * Alter the list of projects before fetching data and comparing versions.
 *
 * Most modules will never need to implement this hook. It is for advanced
17 18 19 20 21 22 23 24
 * interaction with the Update Manager module. The primary use-case for this
 * hook is to add projects to the list; for example, to provide update status
 * data on disabled modules and themes. A contributed module might want to hide
 * projects from the list; for example, if there is a site-specific module that
 * doesn't have any official releases, that module could remove itself from this
 * list to avoid "No available releases found" warnings on the available updates
 * report. In rare cases, a module might want to alter the data associated with
 * a project already in the list.
25 26 27
 *
 * @param $projects
 *   Reference to an array of the projects installed on the system. This
28 29
 *   includes all the metadata documented in the comments below for each project
 *   (either module or theme) that is currently enabled. The array is initially
30 31
 *   populated inside \Drupal\Update\UpdateManager::getProjects() with the help
 *   of \Drupal\Core\Utility\ProjectInfo->processInfoList(), so look there for
32
 *   examples of how to populate the array with real values.
33
 *
34
 * @see \Drupal\Update\UpdateManager::getProjects()
35
 * @see \Drupal\Core\Utility\ProjectInfo::processInfoList()
36 37 38 39 40 41 42
 */
function hook_update_projects_alter(&$projects) {
  // Hide a site-specific module from the list.
  unset($projects['site_specific_module']);

  // Add a disabled module to the list.
  // The key for the array should be the machine-readable project "short name".
43
  $projects['disabled_project_name'] = [
44 45
    // Machine-readable project short name (same as the array key above).
    'name' => 'disabled_project_name',
46
    // Array of values from the main .info.yml file for this project.
47
    'info' => [
48 49
      'name' => 'Some disabled module',
      'description' => 'A module not enabled on the site that you want to see in the available updates report.',
50 51
      'version' => '8.x-1.0',
      'core' => '8.x',
52
      // The maximum file change time (the "ctime" returned by the filectime()
53
      // PHP method) for all of the .info.yml files included in this project.
54
      '_info_file_ctime' => 1243888165,
55
    ],
56 57
    // The date stamp when the project was released, if known. If the disabled
    // project was an officially packaged release from drupal.org, this will
58
    // be included in the .info.yml file as the 'datestamp' field. This only
59 60 61 62 63 64
    // really matters for development snapshot releases that are regenerated,
    // so it can be left undefined or set to 0 in most cases.
    'datestamp' => 1243888185,
    // Any modules (or themes) included in this project. Keyed by machine-
    // readable "short name", value is the human-readable project name printed
    // in the UI.
65
    'includes' => [
66 67 68
      'disabled_project' => 'Disabled module',
      'disabled_project_helper' => 'Disabled module helper module',
      'disabled_project_foo' => 'Disabled module foo add-on module',
69
    ],
70 71 72
    // Does this project contain a 'module', 'theme', 'disabled-module', or
    // 'disabled-theme'?
    'project_type' => 'disabled-module',
73
  ];
74 75
}

76 77 78 79 80 81 82 83 84 85
/**
 * Alter the information about available updates for projects.
 *
 * @param $projects
 *   Reference to an array of information about available updates to each
 *   project installed on the system.
 *
 * @see update_calculate_project_data()
 */
function hook_update_status_alter(&$projects) {
86
  $settings = \Drupal::config('update_advanced.settings')->get('projects');
87 88 89
  foreach ($projects as $project => $project_info) {
    if (isset($settings[$project]) && isset($settings[$project]['check']) &&
        ($settings[$project]['check'] == 'never' ||
90 91
          (isset($project_info['recommended']) &&
            $settings[$project]['check'] === $project_info['recommended']))) {
92 93 94
      $projects[$project]['status'] = UPDATE_NOT_CHECKED;
      $projects[$project]['reason'] = t('Ignored from settings');
      if (!empty($settings[$project]['notes'])) {
95 96
        $projects[$project]['extra'][] = [
          'class' => ['admin-note'],
97 98
          'label' => t('Administrator note'),
          'data' => $settings[$project]['notes'],
99
        ];
100 101 102 103 104
      }
    }
  }
}

105 106 107 108 109 110 111 112 113 114 115
/**
 * Verify an archive after it has been downloaded and extracted.
 *
 * @param string $project
 *   The short name of the project that has been downloaded.
 * @param string $archive_file
 *   The filename of the unextracted archive.
 * @param string $directory
 *   The directory that the archive was extracted into.
 *
 * @return
116 117
 *   If there are any problems, return an array of error messages. If there are
 *   no problems, return an empty array.
118 119
 *
 * @see update_manager_archive_verify()
120
 * @ingroup update_manager_file
121 122
 */
function hook_verify_update_archive($project, $archive_file, $directory) {
123
  $errors = [];
124
  if (!file_exists($directory)) {
125
    $errors[] = t('The %directory does not exist.', ['%directory' => $directory]);
126 127
  }
  // Add other checks on the archive integrity here.
128
  return $errors;
129 130
}

131 132 133
/**
 * @} End of "addtogroup hooks".
 */