Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 504 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can you Create your Own Hook in Drupal?

#1
Is it possible to create **your own** hook in a Drupal module for other Drupal modules to consume? If not, is there a mechanism in Drupal for third party developers to provide hooks? If everything's been a no so far, where in the core are the list of hooks implemented?

As I understand things, Drupal modules work on a event like system called <a href="http://api.drupal.org/api/drupal/includes--module.inc/group/hooks/7">hooks</a>. When you create a new module, you create functions that implement a hook. For example, there's a <code>hook_delete</code> hook. If you implement a function in your module

function mymodule_delete($node)
{
}

this function will be called whenever a node is deleted.

What I want to know is, is there a way or me, as a third party module developer, to create **my own** hooks. Say, something like <code>hook_alanskickbutthook</code> so that other module developers could subscribe to this hook.

If this is possible, how do you do it? I've looked around the official docs and haven't found much there, and I still get a little dizzy when I start poking around the Drupal source code (I understand recursion, but don't spend enough time thinking about recursive problems). Full solutions are welcome, but I'm happy to just be pointed in the right direction.
Reply

#2
If i recall...

[To see links please register here]


does ths help? been a while since I messed with Drupal.

To create/offer custom Drupal hook, you must implement in a ways such that calling the hook with module_invoke or module_invoke_all does not make any conflicts with other module hooks. The name of the hook should be unique and it should offer all/specific feature in such a general way that it doesn't require any type of adjustments with code. All the configuration must go on admin pages and should store those configurations in a separate table or any existing tables create by Drupal or modules on which your modules depends. The hook should be easy to implment by other modules and it should not be much complex to implement. When you create custom hooks, your module(s) act(s) as API provider.
Reply

#3
Module_invoke_all() is your ticket to creating your own hooks:

see the API:

[To see links please register here]


and then look at this great writeup:

[To see links please register here]


(edit: was at

[To see links please register here]

but this is now gone)

Once you've made your hook, it can be called in another module using:

/**
* Implementation of hook_myhookname()
*/

function THISMODULENAME_myhookname(args){
//do stuff
}
Reply

#4
For example, say you wanted to create hook_my_custom_goodness() for others to use. Then just place code like this in your module at the point where you want the hook to happen:

$variables['msg'] = 'foo';

// Make sure at least one module implements our hook.
if (sizeof(module_implements('my_custom_goodness')) > 0) {
// Call modules that implement the hook, and let them change $variables.
$variables = module_invoke_all('my_custom_goodness', $variables);
}

drupal_set_message($variables['msg']); // Will display 'bar' instead.

Now, if anybody wanted to use your hook, then they could do so in their own module like this:

/**
* Implements hook_my_custom_goodness().
*/
function SOME_OTHER_MODULE_my_custom_goodness($variables) {
$variables['msg'] = 'bar';
return $variables;
}

There is a more complete explanation here:

[To see links please register here]

Reply

#5
For Drupal 6 & 7, [drupal_alter()](

[To see links please register here]

) is probably the best option.

As stated in [the module_invoke_all() documentation](

[To see links please register here]

),

> All arguments are passed by value. Use drupal_alter() if you need to
> pass arguments by reference.

In Drupal 8, use [ModuleHandler::alter](

[To see links please register here]

).

> Passes alterable variables to specific hook_TYPE_alter()
> implementations.
Reply

#6
You need to implement two hooks 1. hook_token_info() & 2. hook_tokens() in your module file Below i have given code to create my custom token "query-param-all" and used that token in views- Textarea field.....

/**
* Implements hook_token_info().
*/
function mycustom_token_info() {
$type = [
'name' => ('Custom Token'),
'description' => ('Tokens for custom things.'),
];
$node['query-param-all'] = [
'name' => ("Get all URL query string"),
'description' => ('Get all URL query string'),
];
return [
'types' => ['customtoken' => $type],
'tokens' => ['customtoken' => $node],
];
}

/**
* Implements hook_tokens().
*/
function mycustom_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
//print '<pre>'; print_r($data);exit;
$current_path = \Drupal::request()->query->all();
$query_param = '';
if( count($current_path) > 0) {
$amper = '';
$query_param = '?';
foreach($current_path as $key => $value){
$query_param .= $amper.$key.'='.$value;
$amper = '&';
}
}

if ($type == 'customtoken') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'query-param-all':
$replacements[$original] = $query_param;
break;
}
}
}
return $replacements;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through