📜 ⬆️ ⬇️

Drupal PostInstall - a module that allows other modules to "be configured" after installation

As you know, some modules (such as Google Analytics, for example) need some initial configuration before they can at least function. In addition, sometimes you have to deal with a situation where even when installing a module you need to know some information from the user. Of course, Drupal allows you to do it all on a case-by-case basis using a hook mechanism. However, I decided that it would be better to have a unified way, an interface for solving this problem. For these purposes, and was written by a small service module called PostInstall.

What?


The essence of the module is as follows. It allows other modules to display their configuration pages immediately after installation. The module copes with the installation of several modules at once and displays the configuration pages strictly in accordance with the dependencies (the dependent module is configured before the dependent one). In addition, the module works correctly even if it installs itself with modules that require configuration. If at some step the post-configuration was interrupted, the user will be returned to it the next time they visit the modules page (admin / modules). Setup to proceed from the moment of completion.

How does it work?


It works to disgrace simply. After installing the modules, PostInstall checks if there are modules in the list of installed modules that require post-configuration. If there are any, after the installation is completed, the user is transferred to the setup wizard. The configuration form for each module represents a separate stage of the wizard. The user is shown at what stage he is now (which module is being configured at the moment) and how many stages are total. After the configuration is completed, the user returns to the admin / modules page.

How to implement?


In order to allow your module to be configured after installation, you must:
  1. Add it to the postinstall module (add the line “dependencies [] = postinstall” to the modulename.info file).
  2. Implement in the hook_postinstall () module.

Below is a Russified version of the postinstall.api.php file with a Russified description and an example of how to implement hook_postinstall ().
/** *        *     . * * @param $action *     : * - form:         drupal_get_form(). *          submit.  *     . * - validate:         $values *      $error_messages,   . * $error_messages -      : * - name:    (  form_set_error()). * - message:   ,    . * - submit: Submit callback  .     -  $values. * @param $values *      ( $action == 'validate')   * ( $action == 'submit'). */ function hook_postinstall($action, $values = array()) { switch ($action) { case 'form': $form = array(); $form['test'] = array( '#type' => 'textfield', '#title' => 'Test textfield' ); return $form; break; case 'validate': $error_messages = array(); if (!is_numeric($values['test'])) { $error_messages[] = array( 'name' => 'test', 'message' => t('   ') ); } return $error_messages; break; case 'submit': drupal_set_message($values['test']); break; } } 

')
The official project page on Drupal.Org: drupal.org/project/postinstall
GitHub repository: github.com/numesmat/drupal-postinstall (I can suddenly start forgetting to update)

Source: https://habr.com/ru/post/143864/


All Articles