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');
}
}
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.
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.
4th line
- Contains \Drupal\yourmodule\Plugin\Block\TestBlock.
You should change "TestBlock" to "YourBlockName" as it is the name of your class
Your code is looking fine i will suggest some check points
- Check the directory structure
- Your module will be inside modules
- This folder should contain youtmodule.info.yml, yourmodule.module, and an src folder
- Yor Block class shoud be inside src\Plugin\Block\< somename >Block.php (in your case TestBlock.php )
- Check the info.yml file and check the space intantation
- Check .module file if you dont have nythink to write he just add content like this but this file is required
<?php
/**
* @file
*/
?>
- Install the plugin
- Clear cache
Check this sample : http://wiki.workassis.com/drupal-8-creating-custom-block-from-scratch/
For future visitors:
For me the problem was solved when I changed the class name to EXACTLY the same as the file name (provided the file name is only small letters or underscore, otherwise first rename the file then match the class name to the file name.)
Example:
WRONG FILENAME & class name:
*/TestBlock.php
<?php
// import & dependancy code
class TestBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return array('#markup' => 'hello world');
}
}
RIGHT FILENAME & class name:
*/test_block.php
<?php
// code
class test_block extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return array('#markup' => 'hello world');
}
}
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:
"[Semantical Error] Couldn't find constant references_block, class Drupal\references\Plugin\Block\referencesBlock."
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:
<?php
/**
* @file
*/
namespace Drupal\references\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'references' Block.
* @Block(
* id = references_block", <--------ERROR HERE! missing this: "
* admin_label = @Translation("References Block"),
* )
*/
class referencesBlock extends BlockBase {
}
FIXED CODE:
<?php
/**
* @file
*/
namespace Drupal\references\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'references' Block.
* @Block(
* id = "references_block", <-------- ERROR FIXED!
* admin_label = @Translation("References Block"),
* )
*/
class referencesBlock extends BlockBase {
// lots of code
}
How to download the error logs from localhost/phpmyadmin:
(If you're in a hurry, just read the bold text)
- Go to: localhost/phpmyadmin
- Find your Drupal database
- At the bottom of the database you should find a table called: watchdog
- Click on the table for watchdog so you see what's inside
- At the top you in the navigation you should see a tab called: SQL, click that
Now you already have a part of the query that you need, so that's easier, it should look like this:
SELECT * FROM `watchdog` WHERE 1
but you still need to change it
I used this query:
SELECT variables FROM `watchdog` ORDER BY wid DESC LIMIT 5
this will generate 5 links that is named something like this:
[BLOB - 8,8 kB]
- Start by downloading the top one, if you can't download them (I couldn't) then choose to open them in Notepad instead of downloading.
- I recommend that you use: Sublime Text 3 to open the files, that program can read almost anything, you can get it here: https://www.sublimetext.com/3
class TestBlock extends BlockBase {
/*
** {@inheritdoc}
*/
public function build() {
return array(
'#markup' => $this->t('Welcome page!'),
);
}
}
Use the following link to create new custom bloc programatically.