Custom module development: using hooks

You can use hooks in custom modules to alter the behavior of Drupal core or other modules in your Developer Portal.

Custom modules enable you to extend the functionality of your Developer Portal site. For more information about how to create custom modules, see Custom module development: an introduction to Drupal tools for PHP development, and Custom module development: background and prerequisites.

Hooks are one of the ways that custom modules can use to interact with other modules and with Drupal core subsystems. The following sections provide an overview of hooks, and a list of the IBM® API Connect specific hooks.

Important:
  • You are not permitted to include any IBM API Connect modules within any custom modules that you create. Also, directly editing any API Connect themes, modules, included modules, or Drupal core on the file system is not permitted or supported, as edited versions of these files are overwritten when a fix pack or iFix is installed.
  • All custom development is your responsibility. Although the use of custom modules and themes is supported, IBM API Connect do not provide support in their development or modification.

About hooks

Hooks define functions that alter the behavior of Drupal core. So one way for custom modules to alter these functions, is to use hooks. Hooks are specially-named functions that a module defines (this is known as implementing the hook), these hooks are then discovered and called at specific times to alter or add to the base behavior or data (this is known as invoking the hook). Each hook has a name (for example: hook_batch_alter()), a defined set of parameters, and a defined return value. Your custom modules can implement hooks that are defined by Drupal core, by API Connect, or by other modules that they interact with. Your custom modules can also define their own hooks in order to let other modules interact with them. For more information, see Understanding Hooks on Drupal.org.

Note: If you write a hook implementation function, the function must not throw errors or exceptions, or use alert level log messages. Errors, exceptions, and alert level log messages can break all the processing in the parent code. For example, if you write a hook that calls an external server when an application is updated, the code needs to have error handling in place to handle the server not being found, or not returning the expected result. Otherwise, Drupal stops parsing the function and the application might not get updated correctly. It can cause webhook and snapshot parsing to abort leaving the portal in an inconsistent state.

For a list of all the hooks that are available on Drupal core, and how to implement them in your custom module, see Drupal API Hooks.

For a list of all the API Connect specific hooks, see the following section.

Using hooks in custom modules

To invoke a hook you need to add a function to your .module file in your custom module, and prefix the hook with your custom module name. For example, when using an API Connect hook in a custom module, you must replace the word hook in the hook name with the name of your custom module. For example:
hook_apic_app_create
should be referenced in your custom module as:
moduleName_apic_app_create
See also the following example .module file that shows a custom module called user_field_example:
/**
 * Implementation of hook_form_alter() to alter the Sign up form
 */
function user_field_example_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id === 'user_register_form') {
    // add our validator to the #validate array for the user_register_form
    $form['#validate'][] = 'user_field_example_validate_department_code';
  }
}

/**
 * Validate the Department code field on the Sign up form.
 *
 * Valid entry = DEPnnn
 *   where n = single figure digit.
 */
function user_field_example_validate_department_code($form, &$form_state) {
  if (isset($form_state->getValue('field_department_code')[0]['value'])) {
    $dept_code = $form_state->getValue('field_department_code')[0]['value'];
    $valid = preg_match('/^DEP\d{3}$/', $dept_code);
    if (!$valid) {
      // if the value is not valid then set an inline error on the relevant field
      $form_state->setErrorByName('field_department_code', t('Invalid department code.'));
    }
  }
}

API Connect specific hooks

The following tables list the hooks that are specific to the API Connect Developer Portal. Each section contains a link to the .php file on the API Connect Developer Portal repository on GitHub for those hooks. This file contains examples of how you can use the hooks in your modules.
Hooks about Applications
The following table lists the hooks that relate to Applications in the Developer Portal. For examples of how to use Application hooks, see apic_app/apic_app.api.php on GitHub.
Table 1. Hooks about Applications
Hook name Description
hook_apic_app_create Triggered when an Application is created.
hook_apic_app_update Triggered when an Application is updated.
hook_apic_app_pre_delete Triggered when an Application is deleted, before the node deletion or cascade has happened.
hook_apic_app_post_delete Triggered when an Application is deleted, after the node deletion or cascade has happened.
hook_apic_app_promote Triggered when an Application is promoted.
hook_apic_app_creds_create Triggered when a new set of credentials is created for an Application.
hook_apic_app_creds_update Triggered when a set of credentials is updated for an Application.
hook_apic_app_creds_delete Triggered when a set of credentials is deleted for an Application.
hook_apic_app_subscribe Triggered when a subscription is created.
hook_apic_app_migrate Triggered when a subscription is migrated to a new Plan.
hook_apic_app_unsubscribe Triggered when an Application is unsubscribed from a Plan.
hook_apic_app_image_create Triggered when a custom Application image is created.
hook_apic_app_image_delete Triggered when a custom Application image is deleted.
hook_apic_app_clientid_reset Triggered when a credential client ID is reset.
hook_apic_app_clientsecret_reset Triggered when a credential client secret is reset.
hook_apic_app_modify_getplaceholderimage_alter Alter the Application placeholder image provided in \Drupal\apic_app\Application::getPlaceholderImage(). Can be used to define a specific placeholder image to use when the consumer has not uploaded their own custom image for their Application.
hook_apic_app_modify_getimageforapp_alter Alter the Application placeholder image provided in \Drupal\apic_app\Application::getImageForApp(). Can be used to provide a full path to a specific image to use for an Application that overrides any custom image that might have been uploaded.
hook_apic_app_modify_client_id_reset_alter Alter the client ID provided by API Manager when the ID is reset.
hook_apic_app_modify_client_secret_reset_alter Alter the client secret provided by API Manager when the secret is reset.
hook_apic_app_modify_create_alter Alter the credentials provided by API Manager when a new Application is created.
hook_apic_app_modify_credentials_create_alter Alter the credentials provided by API Manager when new credentials are created.
Hooks about Consumer organizations
The following table lists the hooks that relate to Consumer organizations in the Developer Portal. For examples of how to use Consumer organization hooks, see consumerorg/consumerorg.api.php on GitHub.
Table 2. Hooks about Consumer organizations
Hook name Description
hook_consumerorg_create Triggered when a Consumer organization is created.
hook_consumerorg_update Triggered when a Consumer organization is updated.
hook_consumerorg_pre_delete Triggered when a Consumer organization is deleted, before the node deletion or cascade has happened.
hook_consumerorg_post_delete Triggered when a Consumer organization is deleted, after the node deletion or cascade has happened.
hook_consumerorg_payment_method_create_alter Triggered to allow modification of the payment method creation form.
hook_consumerorg_myorg_tabs_alter Triggered to allow more tabs to be added to the my organization page.
Hooks about APIs
The following table lists the hooks that relate to APIs in the Developer Portal. For examples of how to use API hooks, see apic_api/apic_api.api.php on GitHub.
Table 3. Hooks about APIs
Hook name Description
hook_apic_api_create Triggered when an API is created.
hook_apic_api_update Triggered when an API is updated.
hook_apic_api_delete Triggered when an API is deleted.
Hooks about Products
The following table lists the hooks that relate to Products in the Developer Portal. For examples of how to use Product hooks, see product/product.api.php on GitHub.
Table 4. Hooks about Products
Hook name Description
hook_product_create Triggered when a Product is created.
hook_product_update Triggered when a Product is updated.
hook_product_delete Triggered when a Product is deleted.
Other IBM API Connect Hooks
The following table lists the hooks that can be for other uses.
Table 5. Other APIC Hooks
Hook name Description
hook_ibm_apim_subscription_wizard_summary_alter Triggered to allow modification of the summary page of the subscription wizard.