I'm building a module with custom block in Drupal 8 beta 9. When I create a block in my module I'm placing it in src\Plugin\Block directory. The block show in 'Block structure' list, but when I press link to add it, nothing happens. I reviewed my code 10 times, tried to put my files into core module's directories (only for test obviously), tried to put some core block files to my directory, to copy-paste code. None of this works. Here is a code of my block file:
<?php
/**
* @file
* Contains \Drupal\internetdevels\Plugin\Block\TestBlock.
*/
namespace Drupal\internetdevels\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides 'my custom' block.
*
* @Block(
* id = "my_custom_block",
* admin_label = @Translation("My Custom Block"),
* category = @Translation("System"),
* )
*/
class TestBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return array('#markup' => 'hello world');
}
}
I had the same issue. Non of the recepies I found on the Internet would work to create a block in drupal 8 for me. Finaly I worked it out.
I used some caps for my module name like myBlockModule. This was the problem (on my windows system). Changing the module name to myblockmodule and all files where this name is referenced resolved the issue for me.
Use the following link to create new custom bloc programatically.
I had a similar problem, where I had copied the working code from a previous block and only changed the relevant names, and when I enabled this new module... BOOOOM!!! everything crashes on any page everywhere in the entire Drupal.
After downloading the error logs from localhost/phpmyadmin I could see this:
where: references_block is the meta-data name for my block. it turns out that one of the quotation marks was missing.
Here is that part of the code:
ERROR CODE:
FIXED CODE:
How to download the error logs from localhost/phpmyadmin:
(If you're in a hurry, just read the bold text)
Now you already have a part of the query that you need, so that's easier, it should look like this:
but you still need to change it
I used this query:
this will generate 5 links that is named something like this:
[BLOB - 8,8 kB]
4th line
You should change "TestBlock" to "YourBlockName" as it is the name of your class
In D8, disabled blocks no longer automatically appear under Disabled on admin/structure/block. First, you have to click the 'Place block' button for the Disabled region. Then you can select the block from your custom module.
Your code is looking fine i will suggest some check points
Check this sample : http://wiki.workassis.com/drupal-8-creating-custom-block-from-scratch/