How to cancel installation of my module if requirements are not met

Solutions

Sounds like you're looking for hook_requirements(), which will abort the installation if the requirements are not met.

So if your module was named mymodule, you'd create a function called mymodule_requirements() inside the file mymodule.install.

You can do this in hook_requirements, from the documentation:

During the 'install' phase, modules can for example assert that library or server versions are available or sufficient. Note that the installation of a module can happen during installation of Drupal itself (by install.php) with an installation profile or later by hand. As a consequence, install-time requirements must be checked without access to the full Drupal API, because it is not available during install.php. For localization you should for example use $t = get_t() to retrieve the appropriate localization function name (t() or st()). If a requirement has a severity of REQUIREMENT_ERROR, install.php will abort or at least the module will not install. Other severity levels have no effect on the installation. Module dependencies do not belong to these installation requirements, but should be defined in the module's .info file.

After your check, you return array (see docs again) with REQUIREMENT_ERROR.

function example_requirements($phase) {
  $requirements = array();
  // Ensure translations do not break at install time
  $t = get_t();

  $requirements['example'] = array(
    'title' => $t('Example Library'),
  );

  $libraries = libraries_get_libraries();
  if (isset($libraries['example'])) {
    $requirements['example']['value'] = $t('Installed');
    $requirements['example']['severity'] = REQUIREMENT_OK;
  }
  else {
    $requirements['example']['value'] = $t('Not Installed');
    $requirements['example']['severity'] = REQUIREMENT_ERROR;
    $requirements['example']['description'] = $t('Please install the example library %url.', array('%url' => 'http://example.com'));
  }

  return $requirements;
}

You can check this code example here.

Similar questions

Is it possible to add css class to the page's body tag with rules when certain conditions are met?
I seem to have read that there is a way of making Rules add a css class to the body tag, but I can't find out how to do it. Any ideas?
replace template with other when conditions are met
I want to add a record do theme_hook_suggestions variable but only when specific conditions are met, especially when system is going to use a specific template in theme. In effect I want to replace one template with other. I.e. - when user sees node with id = 50 and view mode == teaser and user has set some specific parameters system should use tem...
Rewrite output for view field only when certain condition is met?
How to make a condition that if e.g. a checkbox is selected in the node saying something like e.g. 'Don't link this to the original content, redirect it somewhere else instead', then overwrite the default path of that node title to something custom? I found a module https://www.drupal.org/project/views_conditional does it do what I'm trying to achi...
site slogan in h6 following h2 site name not matching WCAG 2.0 requirements
Configuration : Drupal 7.26 / Omega 3 / Delta module An accessibility check made with achecker.ca on a website gives me one result : Success Criteria 2.4.6 Headings and Labels (AA) Check 38: Header nesting - header following h2 is incorrect. Repair: Modify the header levels so only an h3 or any header less than h3 follows h2. Error Line 112, Column...
"Did not meet requirements; missing migrations" error
In a custom D6->D8 migration module, how does one selectively migrate specific content-types, vocabs and blocks, etc, without receiving the error: 'XXX did not meet requirements. Missing migrations XXX' I can already selective limit via migrate_prepare_row(), but anytime I do so content related to anything not migrated makes MI throw this error and...
Can't update core with composer: "Your requirements could not be resolved to an installable set of packages"
I am not able to perform a security upgrade of Drupal core with composer. I get "requirements could not be resolved." Here is my composer.json: https://pastebin.com/KxEQdPkt Here is the response:

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.