Custom blocks not working in Drupal 8

2019-08-13 18:10发布

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');
  }

}

7条回答
爷、活的狠高调
2楼-- · 2019-08-13 18:43

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.

查看更多
Evening l夕情丶
3楼-- · 2019-08-13 18:44
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.

查看更多
Root(大扎)
4楼-- · 2019-08-13 18:48

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)

  1. Go to: localhost/phpmyadmin
  2. Find your Drupal database
  3. At the bottom of the database you should find a table called: watchdog
  4. Click on the table for watchdog so you see what's inside
  5. At the top you in the navigation you should see a tab called: SQL, click that
  6. 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

  7. I used this query:

    SELECT variables FROM `watchdog` ORDER BY wid DESC LIMIT 5
    
  8. this will generate 5 links that is named something like this:
    [BLOB - 8,8 kB]

  9. 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.
  10. 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
查看更多
混吃等死
5楼-- · 2019-08-13 18:54

4th line

  • Contains \Drupal\yourmodule\Plugin\Block\TestBlock.

You should change "TestBlock" to "YourBlockName" as it is the name of your class

查看更多
家丑人穷心不美
6楼-- · 2019-08-13 18:55

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.

查看更多
Explosion°爆炸
7楼-- · 2019-08-13 19:03

Your code is looking fine i will suggest some check points

  1. 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 )
  2. Check the info.yml file and check the space intantation
  3. Check .module file if you dont have nythink to write he just add content like this but this file is required

<?php /**
* @file
*/ ?>

  1. Install the plugin
  2. Clear cache

Check this sample : http://wiki.workassis.com/drupal-8-creating-custom-block-from-scratch/

查看更多
登录 后发表回答