0

I created a programmatically block in a custom module for Drupal 8.1. The custom module and the block plugin are created via Drupal Console.

I installed my module and I clear cache via drush cr, but when I go to the admin block page, my block does't appear.

For view an example, I installed the "examples module" and the "block example module". When I do that, there aren't blocks example" in a block admin page too.

I don't understand.

The content of my block class form my custom module is the following.

/**
 * @file
 * Contains \Drupal\nomads_pdf\Plugin\Block\nomads_pdf_block.
 */

namespace Drupal\nomads_pdf\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'nomads_pdf_block' block.
 *
 * @Block(
 *  id = "nomads_pdf_block",
 *  admin_label = @Translation("Nomads_pdf_block"),
 * )
 */
class nomads_pdf_block extends BlockBase {


  /**
   * {@inheritdoc}
   */
  public function build() {
    return array(
      '#type' => 'markup',
      '#markup' => $this->t('Hello, World!'),
    );
  }

}

My custom module structure is the following.

screenshot

5

Did you use the button "Place block"? The block does not appear in the block layout by itself. The block layout is a bit misleading by providing the disabled section, which suggests that all blocks not used are listed there.

The technical background is, that the block layout only lists blocks that have a configuration. When you install a module with a block plugin this block is not registered with a configuration entity yet. That happens when you press the button "Place block" for the first time. You can do this multiple times to get multiple instances of the block. The configured blocks will keep their configuration entity and will stay in the block layout, even when disabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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