module_test.module 5.34 KB
Newer Older
1 2 3
<?php

/**
4
 * Implements hook_permission().
5
 */
6
function module_test_permission() {
7 8 9 10
  return array(
    'module_test perm' => t('example perm for module_test module'),
  );
}
11 12 13

/**
 * Implements hook_system_info_alter().
14
 *
15 16 17
 * Manipulate module dependencies to test dependency chains.
 */
function module_test_system_info_alter(&$info, $file, $type) {
18
  if (Drupal::state()->get('module_test.dependency') == 'missing dependency') {
19
    if ($file->name == 'forum') {
20 21
      // Make forum module depend on ban.
      $info['dependencies'][] = 'ban';
22
    }
23 24
    elseif ($file->name == 'ban') {
      // Make ban depend on a made-up module.
25 26 27
      $info['dependencies'][] = 'foo';
    }
  }
28
  elseif (Drupal::state()->get('module_test.dependency') == 'dependency') {
29
    if ($file->name == 'forum') {
30 31
      // Make the forum module depend on ban.
      $info['dependencies'][] = 'ban';
32
    }
33 34
    elseif ($file->name == 'ban') {
      // Make ban depend on php module.
35 36 37
      $info['dependencies'][] = 'php';
    }
  }
38
  elseif (Drupal::state()->get('module_test.dependency') == 'version dependency') {
39
    if ($file->name == 'forum') {
40 41
      // Make the forum module depend on ban.
      $info['dependencies'][] = 'ban';
42
    }
43 44
    elseif ($file->name == 'ban') {
      // Make ban depend on a specific version of php module.
45 46 47 48
      $info['dependencies'][] = 'php (1.x)';
    }
    elseif ($file->name == 'php') {
      // Set php module to a version compatible with the above.
49
      $info['version'] = '8.x-1.0';
50 51
    }
  }
52 53 54
  if ($file->name == 'seven' && $type == 'theme') {
    $info['regions']['test_region'] = t('Test region');
  }
55
}
56 57 58 59 60 61 62 63 64 65

/**
 * Implements hook_hook_info().
 */
function module_test_hook_info() {
  $hooks['test_hook'] = array(
    'group' => 'file',
  );
  return $hooks;
}
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80
/**
 * Implements hook_menu().
 */
function module_test_menu() {
  $items['module-test/hook-dynamic-loading-invoke'] = array(
    'title' => 'Test hook dynamic loading (invoke)',
    'page callback' => 'module_test_hook_dynamic_loading_invoke',
    'access arguments' => array('access content'),
  );
  $items['module-test/hook-dynamic-loading-invoke-all'] = array(
    'title' => 'Test hook dynamic loading (invoke_all)',
    'page callback' => 'module_test_hook_dynamic_loading_invoke_all',
    'access arguments' => array('access content'),
  );
81 82 83 84 85 86
  $items['module-test/hook-dynamic-loading-invoke-all-during-load/%module_test'] = array(
    'title' => 'Test hook dynamic loading (menu item load)',
    'page callback' => 'module_test_hook_dynamic_loading_invoke_all_during_load',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
  );
87 88 89 90 91
  $items['module-test/class-loading'] = array(
    'title' => 'Test loading a class from another module',
    'page callback' => 'module_test_class_loading',
    'access callback' => TRUE,
  );
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  return $items;
}

/**
 * Page callback for 'hook dynamic loading' test.
 *
 * If the hook is dynamically loaded correctly, the menu callback should
 * return 'success!'.
 */
function module_test_hook_dynamic_loading_invoke() {
  $result = module_invoke('module_test', 'test_hook');
  return $result['module_test'];
}

/**
 * Page callback for 'hook dynamic loading' test.
 *
 * If the hook is dynamically loaded correctly, the menu callback should
 * return 'success!'.
 */
function module_test_hook_dynamic_loading_invoke_all() {
113
  $result = Drupal::moduleHandler()->invokeAll('test_hook');
114 115 116
  return $result['module_test'];
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/**
 * Page callback for 'hook dynamic loading' test.
 *
 * If the hook is dynamically loaded correctly, the menu callback should
 * return 'success!'.
 */
function module_test_hook_dynamic_loading_invoke_all_during_load($param) {
  return $param;
}

/**
 * Load function used by module_test_hook_dynamic_loading_invoke_all_during_load().
 *
 * @see module_test_menu().
 */
function module_test_load($param) {
133
  $result = Drupal::moduleHandler()->invokeAll('test_hook');
134 135 136
  return $result[$param];
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/**
 * Page callback for 'class loading' test.
 *
 * This module does not have a dependency on module_autoload_test.module. If
 * that module is enabled, this function should return the string
 * 'Drupal\\module_autoload_test\\SomeClass::testMethod() was invoked.'. If
 * that module is not enabled, this function should return nothing.
 */
function module_test_class_loading() {
  if (class_exists('Drupal\module_autoload_test\SomeClass')) {
    $obj = new Drupal\module_autoload_test\SomeClass();
    return $obj->testMethod();
  }
}

152 153 154 155
/**
 * Implements hook_modules_enabled().
 */
function module_test_modules_enabled($modules) {
156 157
  // Record the ordered list of modules that were passed in to this hook so we
  // can check that the modules were enabled in the correct sequence.
158
  Drupal::state()->set('system_test.module_enable_order', $modules);
159
}
160 161 162 163 164 165 166

/**
 * Implements hook_modules_disabled().
 */
function module_test_modules_disabled($modules) {
  // Record the ordered list of modules that were passed in to this hook so we
  // can check that the modules were disabled in the correct sequence.
167
  Drupal::state()->set('module_test.disable_order', $modules);
168 169 170 171 172 173 174 175
}

/**
 * Implements hook_modules_uninstalled().
 */
function module_test_modules_uninstalled($modules) {
  // Record the ordered list of modules that were passed in to this hook so we
  // can check that the modules were uninstalled in the correct sequence.
176
  Drupal::state()->set('module_test.uninstall_order', $modules);
177
}