Can't get custom block module to appear in “Place blocks” screen in Drupal 8

Refresh

April 2019

Views

290 time

1

I am trying to write a custom block module in drupal 8 that will allow me to place this block into my drupal pages using the block layout screen.

I'm very new to drupal - so this is very much a "hello world" project (initially) to figure out how this works. To this end, I have been following youtube video instructions as well as reading through whatever doco I can find on the net [and also a couple of books], but to no avail [yet].

What I have done, is created a directory "walker" in the modules folder in which I have created Walker.info.yml which looks like this:

name: Walker
description: A filewalker
core: 8.x
package: Custom
type: module

In the same directory I have created Walker.module which looks like this:

<?php
/**
 * @file
 * Code for the walker module.
 */

I have then created a directory:

module/walker/src/Plugin/Block/

and created a file called WalkerBlock.php with the following content:

<?php
/**
 * @file
 * Code for the walker block.
 */

namespace Drupal\walker\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a walker block
 * 
 * @Block(
 *   id = "walker",
 *   admin_label = @Translation("The walker block"),
 *   category = @Translation("Blocks")
 * )
 */

class WalkerBlock extends BlockBase
{
    public function build()
    {
        return array('#markup' => 'Walker block');
    }
}

If I run the command "find . -type f | grep Walker" from my root Drupal directory, I get the following output:

./modules/Walker/Walker.module
./modules/Walker/src/Plugin/Block/WalkerBlock.php
./modules/Walker/Walker.info.yml

Yet, though the documentation and videos I have read suggest that this should be all I require, the Walker block does not appear in the block layout screen as an option that I can place in one of the block regions.

Can anyone see what I'm doing wrong? I have been going around and around in circles for hours now.

Note also, that I have gone to the drupal8/admin/config/development/performance screen and clicked the "Clear all cache's" button

Thanks heaps for any assistance,

David Buddrige :-)

1 answers

0

Check the module where you defined you block plugin is installed or not. If the module is not installed, go to the extend page of your site search for your module name and enable it. If you are using drush then you can simply enable it by

drush en moduleName

After that, I think you will get the block name in place block of your block layout section.