0

In drupal 7, can we access and read return value of a specific module via another module?

Specifically, I am using the whois module, and would like to access its output from a custom module, with the goal of modifying the output.

How can I do this? What is the suitable hook function? How can I access the output from whois before it gets rendered.

Please guide me.

Thanks a lot.

4
  • 1
    What do you mean by "return value of a specific module"? Functions have return values. Modules do not. Sometimes module's function returns renderable array, or form, or something. You must be more specific about your particular problem, I'm afraid.
    – Mołot
    Commented Dec 4, 2013 at 13:33
  • Imagine that module A define an array with key "module_A_pages" I want to access and read this array inside module B.
    – Mehrdad201
    Commented Dec 4, 2013 at 13:38
  • "You should only ask practical, answerable questions based on actual problems that you face." (see here ). Please either post real names of contrib module and real arrays you want to change, or provide code of your custom modules, both the one that creates, and the one that changes. Without it there is too many possibilities that will or will not work in different situations.
    – Mołot
    Commented Dec 4, 2013 at 13:45
  • Ok . The main module is "whois" module. But I want to program another module. In my module I want to read defined array in whois module (when a user press whois button) and read return data from whois module. After reading, I want to analyze data and add extra information at the end of whois result.
    – Mehrdad201
    Commented Dec 4, 2013 at 13:51

1 Answer 1

1

You can change the output through preprocessing, for example in your theme's template.php file, or in your custom module (see also: https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_preprocess_HOOK/7).

The $variables['content'] variable contains the content of the block to be displayed. The individual elements come from $variables['elements']. If you add:

debug(array_keys($variables['elements']));

to the script, you get the following output:

array (
  0 => 'whois_form',
  1 => 'whois_lookup_data',
  2 => '#block',
  3 => '#weight',
  4 => '#theme_wrappers',
  5 => '#children',
)

When looking at these in detail, you see that $variables['elements']['#children'] is comprised of $variables['elements']['whois_form']['#children'] and $variables['elements']['whois_lookup_data']['#children'].

Ultimately, $variable['content'] is comprised of $variable['elements']['#children'] (see also https://api.drupal.org/api/drupal/modules!block!block.module/function/template_preprocess_block/7)

So, there are two options:

  1. Alter $variables['elements']['whois_lookup_data']['#children'], and recreate $variable['elements']['#children']
  2. Alter $variable['elements']['#children'] directly.

I chose the second option below.

To use the hook_preprocess_block hook, as shown below, the core module "Block" needs to be enabled.

If used in a module, in yourmodule.info:

name = Whois Alter Module
description = A module to change the output of whois.
core = 7.x
package = Drupal 7 Misc
files[] = whois_alter.module
dependencies[] = block
dependencies[] = whois

In whois_alter.module:

<?php
// $Id$
/**
* @file
* A module to demonstrate hook_preprocess_block to alter the output of another module
*
* This module alters the output of the whois module
*/

/**
* Implements hook_help().
*/
function whois_alter_help($path, $arg) {
  if ($path == 'admin/help#whois_alter') {
    return t('A module to alter whois output.');
  }
}


/**
* Implements hook_preprocess_block().
*/

function whois_alter_preprocess_block(&$variables, $hook){

  if (isset($variables['elements']['whois_lookup_data'])) {
     $whois_modified = whois_alter_modify_whois($variables['elements']['#children']);
     $variables['elements']['#children'] = $whois_modified;
     $variables['content'] = $variables['elements']['#children'];
  }
}


function whois_alter_modify_whois($content) {
   // do what you want to do to modify the output
   return $altered_content;
}

For Theme preprocessing, just use the functional part of the module in template.php:

function yourtheme_preprocess_block(&$variables, $hook){

  if (isset($variables['elements']['whois_lookup_data'])) {
     $whois_modified = yourtheme_modify_whois($variables['elements']['#children']);
     $variables['elements']['#children'] = $whois_modified;
     $variables['content'] = $variables['elements']['#children'];
  }
}


function yourtheme_modify_whois($content) {
   // do what you want to do to modify the output
   return $altered_content;
}
6
  • it seems there is no hook "module_preprocess_block" are you sure about that ?
    – Mehrdad201
    Commented Dec 4, 2013 at 22:10
  • You need to enable the "block" module - a core module. Edited my answer accordingly.
    – Ursula
    Commented Dec 4, 2013 at 23:42
  • In my drupal, block is enabled and I am using it. I am creating a new module base on your codes Ursula. Thanks a lot for your help. I will inform your about the result.
    – Mehrdad201
    Commented Dec 5, 2013 at 9:25
  • Dear Ursula, I checked your module. But this line $variables['content'] = $variables['elements']['whois_lookup_data']['#children']; it removes "whois form". When I comment this line "whois search form" is appeared but when I active it whois form is hide. Can you tell what does this line do?
    – Mehrdad201
    Commented Dec 5, 2013 at 9:36
  • I found that when I set dependency in info file I am able to use defined functions in source module in my custom module. So I set dependencies[] = whois in my custom module and then I used "whois_get_whois" function (this function is defined in whois module) in my custom module. this functions loads whois info of specific domain name into an array. Because cached is activated, it is very quick and there is no twice request from whois server. Now I am able to work base on result array keys and values. Now what is your idea about my theory ?
    – Mehrdad201
    Commented Dec 5, 2013 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.