module_test.module 4.38 KB
Newer Older
1 2
<?php

3 4
use Drupal\Core\Extension\Extension;

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

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

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

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

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

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

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

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

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