drupaldrupal-8drupal-blocks

My custom block doesn't show up in the block library


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.


Solution

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

    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.