You are here

function xmlrpc_example_xmlrpc_alter in xmlrpc 8

Implements hook_xmlrpc_alter().

Check to see if xmlrpc_example.add and xmlrpc_example.subtract methods are defined and replace their callbacks with custom code.

The alteration part of the module starts here. hook_xmlrpc_alter() is useful when you want to extend, limit or alter methods defined by other modules. This part is not required to have an XML-RPC server or client working, but is useful to understand what can we do using current xmlrpc API provided by drupal.

This code can be defined in other module to alter the methods exposed by this xmlrpc demonstration server, or can be used to alter methods defined by other modules implementing hook_xmlrpc()

As with the rest of the example module, an user interface is not required to make use of this hook. A configuration form is included to enable/disable this functionality, but this part is optional if you want to implement hook_xmlrpc_alter()

This is the XML-RPC code for the alteration part. It will check if an option to enable the functionality is enabled and then alter it. We alter the 'xmlrpc_example.add' and 'xmlrpc_example.subtract' methods, changing the associated callback with custom functions. The modified methods (with new callbacks associated) will perform the addition or subtraction of the integer inputs, but will never check for limits nor return errors.

See also

hook_xmlrpc_alter()

Related topics

File

xmlrpc_example/xmlrpc_example.module, line 220
Module file for xmlrpc_example module.

Code

function xmlrpc_example_xmlrpc_alter(&$methods) {
  $config = \Drupal::config('xmlrpc_example.server');

  // Only perform alterations if instructed to do so.
  if (!$config
    ->get('alter_enabled')) {
    return;
  }

  // Loop all defined methods (other modules may include additional methods).
  foreach ($methods as $index => $method) {

    // First element in the method array is the method name.
    if ($method[0] == 'xmlrpc_example.add') {

      // Replace current callback with custom callback
      // (second argument of the array).
      $methods[$index][1] = '_xmlrpc_example_alter_add';
    }

    // Do the same for the subtraction method.
    if ($method[0] == 'xmlrpc_example.subtract') {
      $methods[$index][1] = '_xmlrpc_example_alter_subtract';
    }
  }
}