0

This question already has an answer here:

I made a sub-theme of the admin theme "Seven" and called it Seven2 Because I wanted to customize the node-edit-form.twig.html file. That works.

My next step, I want to add a block I made in a custom module, to that node-edit-form.twig.html. There are several ways of doing that, and one I chose is preprocess page (or node for that matter), where I want to load my block.

The preprocess functions can either be in Seven2 .theme file or the custommodule.module file.

When I execute:

$block = \Drupal\block\Entity\Block::load('checkmark_block');

The $block is NULL when I do kint($block). Its like it can't find the damned block!! I have spent like 4 hours trying to make this thing work, in different ways, but no matter what, I can't load that block in my preprocess function. I don't know why but I think it's because it's an admin theme? Or... please help.

One of the blocks located in custommodule/src/Plugin/Block:

<?php
namespace Drupal\custommodule\Plugin\Block;
use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'CheckMark' Block.
 *
 * @Block(
 *   id = "checkmark_block",
 *   admin_label = @Translation("CheckMark block"),
 *   category = @Translation("CheckMarks"),
 * )
 */
class CheckMark extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $markup = 'Test test';

    $build = array(
      '#type' => 'markup',
      '#markup' => $markup,
      '#cache' => array('max-age' => 0),
    );

    return $build;
  }

}

marked as duplicate by Kevin, kiamlaluno Sep 26 '17 at 6:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Ok, I made it work by the answer from this post: How can I programmatically display a block?

And rendered it by adding this in hook_form_alter:

if ($form_id == 'node_article_edit_form') {
  ... code from the other post here ... 
  $block = $plugin_block->build();
  $form['checkmark_block'] = $block;
}

And then in the twig file:

{{ form.checkmark_block }}

I find it way to difficult for a small task like this, and annoying to spend so much time on it, but one hopefully learns by it.

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