1

I have a module called hello_world.

I have a hello_world.info.yml file in drupal/web/modules/custom/hello_world whose content is the following.

name: Hello World
description: Hello World module
type: module
core: 8.x
package: Custom

This works perfectly ; the module is in the extension list.

I tried to make a help hook so I made in the same folder a hello_world.module file with the following code.

<?php

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */

function hello_world_help($route_name, RouteMatchInterface $route_match) {
    switch ($route_name) {
        case 'help.page.hello_world':
            $output = '';
            $output .= '<h3>' . t('About') . '</h3>';
            $output .= '<p>' . t('This is an example module.') . '</p>';
            return $output;
            break;
    }
}

This does not work at all. The help page is not shown.

I also tried to make a hello world page using this controller in drupal/web/modules/custom/hello_world/src/HelloWOrldCOntroller.php.

<?php

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Controller for the salutation message.
 */
class HelloWorldController extends ControllerBase
{
    /**
     * Hello World.
     *
     * @return string
     */
    public function helloWorld()
    {
        return [
            '#markup' => $this->t('Hello World')
        ];
    }
}

I added hello_world.routing.yml to drupal/web/modules/custom/hello_world.

hello_world.hello:
path: '/hello'
defaults:
_controller:
'\Drupal\hello_world\Controller\HelloWorldController::helloWorld'
_title: 'Our first route'
requirements:
_permission: 'access content'

This does not work either, even after clearing the cache. As I said, the hello_world.info.yml works perfectly fine, but the help hook and the controller/routing does not. The rest of the core modules work. I am using Vagrant if it matters.

4
  • 2
    Did you edited the module code after installing it?
    – avpaderno
    Commented Apr 11, 2018 at 7:45
  • @kiamlaluno I did not install it, just created the files in drupal following some tutorial.
    – prgrm
    Commented Apr 11, 2018 at 7:46
  • 1
    If you don't install a module, it doesn't work.
    – avpaderno
    Commented Apr 11, 2018 at 7:47
  • @kiamlaluno RIght. Solved.
    – prgrm
    Commented Apr 11, 2018 at 7:50

1 Answer 1

3

Creating the files is not enough: The module needs also to be installed. /admin/modules gives a list of modules and allows to install them.

After a module is installed, all its hooks are invoked from Drupal.

As side note, YAML files are required to use the correct indentation. The content of the hello_world.routing.yml file should be the following one.

hello_world.hello:
  path: '/hello'
  defaults:
    _controller: '\Drupal\hello_world\Controller\HelloWorldController::helloWorld'
    _title: 'Our first route'
  requirements:
    _permission: 'access content'

See the example give in Routing API, Registering simple routes.

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.