Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

As a module developer, I'd like to convert some of my Drupal 7 info hooks to YAML discoveries in Drupal 8, but it's not clear to me how to document the schema of a YAML file for consumers. For a hook, of course, I would define hook_whatever() in mymodule.api.php. What do I do for *.whatever.yml? What is core doing?

share|improve this question
    
Do you want to implement new hook exposed by other module or define new hook in your custom module ? –  Anil Sagar Apr 17 at 5:34
    
My custom module already invokes a hook, and I want to replace it with a YAML file. Specifically, I'm replacing hook_shunt_info() with *.shunts.yml. I already have the feature working. My only question is: how do I document it so that module developers who used to implement my hook know how to create a YAML file instead? –  TravisCarden Apr 17 at 13:48

2 Answers 2

  • Create a folder named mymodule inside sites/all/modules or modules
  • Create mymodule.info.yml and mymodule.module files insides your module "mymodule" folder

Code for mymodule.info.yml goes below

name: My Module
description: 'My Module description.'
package: Custom
type: module
version: 1.0
core: 8.x

If you want to invoke a hook you need to use something like below on some event function..

  \Drupal::moduleHandler()->invokeAll('whateven', array($args));

For Example.. User.module does this for user logout event

/**
 * Logs the current user out.
 */
function user_logout() {
  $user = \Drupal::currentUser();

  watchdog('user', 'Session closed for %name.', array('%name' => $user->getUsername()));

  \Drupal::moduleHandler()->invokeAll('user_logout', array($user));

  // Destroy the current session, and reset $user to the anonymous user.
  session_destroy();
}

Regarding api.php files.. They are just used for documentation purposes to help developers..

Regarding .info.yml files they are required to:

  • Notify Drupal about the existence of a module, theme or install profile.
  • Provide information for the Drupal Web UI administration pages.
  • Provide criteria to control module activation and deactivation and Drupal version compatibility.
  • General administrative purposes in other contexts.

Implementing a hook is still same in Drupal 8 .. Defining a function with module name followed by hook name.

share|improve this answer

There is no "documenting" of hooks in D8 except the mymodule.api.php as you've already mentioned(+ impleneting hook_hook_info).

The YAML files are only for:

  • services
  • routes
  • menu local tasks
  • menu local actions
  • libraries
  • module info
  • simple configuration
  • simple configuration schema

Now you've mentioned "info" hooks. In Drupal 8 these are Annotations and their implementations are Plugins. If this is assumption correct then see the documentation in here https://drupal.org/developing/api/8/plugins for plugins - what they are, how they work and how to use them.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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