module_test.module 4.24 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
 * Manipulate module dependencies to test dependency chains.
 */
17
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
    elseif ($file->name == 'ban') {
24
      // 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
    elseif ($file->name == 'ban') {
34 35
      // Make ban depend on xmlrpc module.
      $info['dependencies'][] = 'xmlrpc';
36 37
    }
  }
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
    elseif ($file->name == 'ban') {
44 45
      // Make ban depend on a specific version of xmlrpc module.
      $info['dependencies'][] = 'xmlrpc (1.x)';
46
    }
47
    elseif ($file->name == 'xmlrpc') {
48
      // Set xmlrpc module to a version compatible with the above.
49
      $info['version'] = '8.x-1.0';
50 51
    }
  }
52
  if ($file->name == 'seven' && $type == 'theme') {
53 54
    $info['regions']['test_region'] = t('Test region');
  }
55
  if ($file->name == 'module_test' && \Drupal::state()->get('module_test.hook_system_info_alter')) {
56 57 58
    $info['required'] = TRUE;
    $info['explanation'] = 'Testing hook_system_info_alter()';
  }
59
}
60 61 62 63 64 65 66 67 68 69

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

71 72 73 74 75
/**
 * Page callback for 'hook dynamic loading' test.
 *
 * If the hook is dynamically loaded correctly, the menu callback should
 * return 'success!'.
76 77
 *
 * @deprecated \Drupal\module_test\Controller\ModuleTestController::hookDynamicLoadingInvoke()
78 79
 */
function module_test_hook_dynamic_loading_invoke() {
80
  $result = \Drupal::moduleHandler()->invoke('module_test', 'test_hook');
81 82 83 84 85 86 87 88
  return $result['module_test'];
}

/**
 * Page callback for 'hook dynamic loading' test.
 *
 * If the hook is dynamically loaded correctly, the menu callback should
 * return 'success!'.
89 90
 *
 * @deprecated \Drupal\module_test\Controller\ModuleTestController::hookDynamicLoadingInvokeAll()
91 92
 */
function module_test_hook_dynamic_loading_invoke_all() {
93
  $result = \Drupal::moduleHandler()->invokeAll('test_hook');
94 95 96
  return $result['module_test'];
}

97 98 99 100 101 102
/**
 * Load function used by module_test_hook_dynamic_loading_invoke_all_during_load().
 *
 * @see module_test_menu().
 */
function module_test_load($param) {
103
  $result = \Drupal::moduleHandler()->invokeAll('test_hook');
104 105 106
  return $result[$param];
}

107 108 109 110 111 112 113
/**
 * 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.
114 115
 *
 * @deprecated \Drupal\module_test\Controller\ModuleTestController::testClassLoading()
116 117 118 119 120 121 122 123
 */
function module_test_class_loading() {
  if (class_exists('Drupal\module_autoload_test\SomeClass')) {
    $obj = new Drupal\module_autoload_test\SomeClass();
    return $obj->testMethod();
  }
}

124
/**
125
 * Implements hook_modules_installed().
126
 */
127
function module_test_modules_installed($modules) {
128 129
  // 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.
130
  \Drupal::state()->set('module_test.install_order', $modules);
131 132 133 134 135 136 137 138
}

/**
 * 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.
139
  \Drupal::state()->set('module_test.uninstall_order', $modules);
140
}