1

So I was hoping to have my theme create special blocks for different parts of the front page so that they can be moved around easily later. I added the files to mytheme/src/Plugin/Block/BlockName.php but they never show up anywhere on the site or the block manager even after clearing cache.

Here's the code for one of my blocks (MediaPanel.php):

<?php
/**
 * @file
 * Contains \Drupal\mytheme\Plugin\Block\MediaPanel.
 */

namespace Drupal\mytheme\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides my custom block.
 *
 * @Block(
 *   id = "media_panel",
 *   admin_label = @Translation("Media Panel"),
 *   category = @Translation("Blocks")
 * )
 */
class MediaPanel extends BlockBase {

/**
   * {@inheritdoc}
   */
  public function build() {
    return array('#markup' => 'hello world');
  }
}

?>

Do I have to use a module for creating Blocks? It would be inconvenient to have to make a module to create blocks that will be specific to my theme anyway.

1
  • If it's just markup/content, you could potentially save custom block content on install.
    – mradcliffe
    Commented Mar 7, 2016 at 23:15

1 Answer 1

3

Yes. Themes can't provide plugins.

3
  • Really? I know the Drupal 8 Bootstrap theme comes with an src/Plugin directory full of PHP class files. I was hoping it would be possible since I don't want to unpack the blocks' markup from the theme by having it go in the modules folder.
    – hanyuxuan
    Commented Mar 8, 2016 at 17:04
  • bootstrap does its own weird thing, it has special plugin types with special discovery that it implements itself. Those things actually only work with themes. Normal plugins do not.
    – Berdir
    Commented Mar 8, 2016 at 18:12
  • Is there any work around to create a block problematically only for my theme?
    – siddiq
    Commented Dec 8, 2017 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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