0

I am building a custom block. The content of module_name/src/Plugin/Block/MyCustomblockName.php is the following.

    namespace Drupal\module_name\Plugin\Block;

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

    /**
     * Provides a 'Demo' block.
     *
     * @Block(
     *   id = "demo_block",
     *   admin_label = @Translation("Demo block"),
     * )
     */

    class MyCustomblockName extends BlockBase {

      /**
       * {@inheritdoc}
       */
      public function build() {    
        return array(
          '#markup' => $this->t('Hello World!'),
        );
      }

      /**
       * {@inheritdoc}
       */

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

}

It doesn't display the block at all. What could be the reason?

closed as off-topic by kiamlaluno May 5 '16 at 16:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    remove the access method and instead define permissions in permissions.yml – Shreya Shetty May 5 '16 at 15:16
  • i commented out that one inside access method and was not working – Crazyrubixfan May 5 '16 at 15:17
  • 1
    And now go to structure > BlockLayout and click on Place block . You can find your block – Shreya Shetty May 5 '16 at 15:18
  • I see the block when i clicked on place block , but some how its not displaying – Crazyrubixfan May 5 '16 at 15:20
  • 2
    use use Drupal\Core\Block\BlockBase; instead of use Drupal\block\BlockBase; Flush cache and check . – Shreya Shetty May 5 '16 at 15:21
5
  1. From the above code replace:

    use Drupal\block\BlockBase;
    

    with:

    use Drupal\Core\Block\BlockBase;
    

    Then Flush cache and check.

  2. Go to Structure, BlockLayout and click on Place block.

Not the answer you're looking for? Browse other questions tagged or ask your own question.