Custom blocks not working in Drupal 8
Asked Answered
T

7

2

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

}
Treacy answered 7/4, 2015 at 11:43 Comment(3)
Your class should be at [module-root]/src/Plugin/Block/YourBlockName.php ...is it?Commeasure
Yes, it is. I checked multiple times.Bunion
When you have "silent" errors like that, the first thing to do is look at php's errors logExpeditious
L
9

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.

Linkboy answered 19/9, 2015 at 0:16 Comment(0)
D
2

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.

Disappoint answered 23/6, 2016 at 10:16 Comment(1)
You are a savior!! Drupal 8 should immediately try to rectify this problem.Ardis
T
1

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

}
Toussaint answered 19/4, 2017 at 14:36 Comment(0)
T
1

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
Toussaint answered 5/5, 2017 at 10:39 Comment(0)
E
0

4th line

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

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

Expeditious answered 8/4, 2015 at 8:55 Comment(4)
You may have to flush drupal's cache after this.Expeditious
I've changed the code, but problem still remains. As I've told in my post, I've tried other blocks, and even copying some code from other modules.Bunion
By the way, blocks that generated by contrib modules like "Examples for developers" are working perfectly.Bunion
@file should not be used when your class is namespaced. Ref: drupal.org/docs/develop/coding-standards/… Remove that line. Also, it's still not clear what the filename is. The file within the folder of module internetdevels should be: src/Plugin/Block/TestBlock.php The name of the class must exactly match the filename without .php at the end.Burger
P
0

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/

Prady answered 16/6, 2016 at 10:10 Comment(0)
C
0
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.

Checker answered 6/7, 2017 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.