My custom block doesn't show up in the block library
Asked Answered
A

3

5

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.

Anglicanism answered 11/2, 2016 at 13:48 Comment(2)
I'm voting for this question to be closed on StackOverflow, and moved to Drupal Answers. I think you will have a better chance on a helpful answer there.Turtleback
check this https://mcmap.net/q/2034756/-custom-blocks-not-working-in-drupal-8Moravian
S
5

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.

Shellyshelman answered 16/2, 2016 at 13:17 Comment(0)
E
1

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

Exegetics answered 6/7, 2017 at 15:18 Comment(0)
C
1

You should respect the Drupal coding standard recommendations: No camelCase naming convention in module name.

OrganizationOverview actually is an error, you should use organization_overview (lowercase/underscore) naming conventions.

Carbonic answered 24/9, 2018 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.