Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My Custom Block in Drupal 8 does not show up in Block Layout

I am developing a custom module in Drupal 8. It shows data regarding some organizations that make use of our service. For this I have created a Controller that shows data from the database, which is put there by another module. From the scarce information and tutorials available on Drupal 8 developement I've been able to create the following. In the .routing.yml file I have created a path to this overview table like so (it doesn't properly copy here but the indents are okay):

   OrganizationOverview.world:
     path: '/world'
     defaults:
       _controller:      'Drupal\OrganizationOverview\Controller\OrganizationOverviewController::overview'
    _title: 'World'
  requirements:
    _role: 'administrator'
    _permission: 'access content'

So now the overview is accessible with the URL site.com/world. But what we want is to show it on the frontpage or show it anywhere else on the site. For this it needs to be a Block. For this I have created an OrganizationOverviewBlock class in OrganizationOverview/src/Plugin/Block/OrganizationOverviewBlock.php which is the proper way according to the PSR-4 standard. The class looks like this:

<?php

namespace Drupal\OrganizationOverview\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;

/**
 * Provides a 'OrganizationOverviewBlock' block.
 *
 * @Block(
 *  id = "organization_overview_block",
 *  admin_label = @Translation("OrganizationOverviewBlock"),
 *  category = @Translation("Custom")
 * )
 */
class OrganizationOverviewBlock extends BlockBase 
{


    public function build()
    {
        return array(
            '#markup' => 'Hello World',
        );
    }

    public function blockAccess(AccountInterface $account)
    {
        return $account->hasPermission('access content');
    }

}

So now it should show up in the Blocks Layout page (after flushing cache, which I do consistently) at site.com/admin/structure/block/ as "Organization Overview Block" where I should enable it, according to plenty sources (Create custom Block, Block API Drupal 8). But it doesn't show up there. I've tried implementing ContainerFactoryPluginInterface with some of those methods but that changes nothing. It does not show up. I've tried making a new test module with a block with the same code but a simpler name and it does not show up. I've copied the code to another platform (the production site) but it also doesn't show up there. What am I doing wrong? Can someone help me? I know Drupal 8 is new but this module really needs to be published soon.

like image 906
Jacques Avatar asked Feb 11 '16 13:02

Jacques


People also ask

How can you view the block layout of your website in Drupal?

If you click "Configure Block" you can go ahead and edit the contents of the block, deal with the visibility settings and even change the placement of where it is on your theme. Blocks are placed in regions via the Block Admin page Administer > Structure > Block Layout. Your site's theme defines the regions available.

How do I create a custom block in Drupal 8?

Go to admin -> structure -> block layout -> custom block library. Click 'block types' tab. Once here, click on the 'Add custom block type' button. Enter block label and description.

How do I override a block in Drupal 8?

Site block configuration can be found at: Administration > Configuration > System > Block List OverrideSettings ( admin/config/block_list_override/settings ).


2 Answers

You'll find a working example of building custom block in the Drupal Examples Project. So:

  • Get the Drupal 8 examples project
  • Enable the Block Example Module
  • Double check the working code

With that, you should get your block available in your own module

You can also take advantage of what explained here, where a single php file do the all job. Check files and folders path also.

like image 81
augusto Avatar answered Oct 23 '22 23:10

augusto


Not require routing file for custom block.

<pre>
class TestBlock extends BlockBase {

/*
** {@inheritdoc}
*/

public function build() {
   return array(
     '#markup' => $this->t('Welcome page!'),
   );
   }
}
</pre>

http://drupalasia.com/article/drupal-8-how-create-custom-block-programatically

like image 1
user8264199 Avatar answered Oct 23 '22 23:10

user8264199