5

I created a new custom block type from /admin/structure/block/block-content/types having x and y fields with field types image and textfield respectively. Then created a block with the same block type and uploaded image in x and some text in y. Here my problem is: how to load this block via code?

More than that how to get values of these block fields x and y by loading the block?

I tried these:

$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$render = \Drupal::entityManager()->getViewBuilder('block_content')->view($block);

but it didn't work.

5

Place the block in the disabled section at admin/structure/block, this creates a block instance with your block_content type, give it a nice easy to remember machine name.

place block in disabled section

By placing it in the disabled section, it won't show up, until you load it programatically.

Then using your machine name:

$machine_name = 'myblock';

$block = \Drupal
  ->entityTypeManager()
  ->getStorage('block')
  ->load($machine_name);
if (!empty($block)) {
  $block_content = \Drupal::entityTypeManager()
    ->getViewBuilder('block')
    ->view($block);

  $pre_render = $block_content;
}

If you really want to get the rendered fields, you can do:

$render = \Drupal::entityTypeManager()
  ->getViewBuilder('block')
  ->build($pre_render);

But it is more standard practice to use Drupal's formatters and such and perhaps a custom template, rather than building the block and extracting elements from the rendered render array.

0

There are two questions. How to load and render a configured block is answered perfectly by @oknate.

The second question: More than that how to get values of these block fields 'x' and 'y' by loading the block?

That's easy, because you've already loaded the block content entity, where the fields are stored in the database.

$block = \Drupal\block_content\Entity\BlockContent::load($bid);

You can access the value from the text field

$text = $block->field_y->value;

and get the url from the image field.

$image_url = file_create_url($block->field_x->entity->uri->value);
  • Please explain how to get this $bid? – user21513 Dec 21 '16 at 10:11
  • $bid is the block content id. You see this in the url: /block/[bid]. This is the same as $nid is for a node: /node/[nid] – 4k4 Dec 21 '16 at 10:32
  • Suppose you are placing the block in , contact/form page . I need to get this bid dynamically , How to get? hook_page_preprocess dynamically able to get the machine id . with $block = \Drupal\block_content\Entity\BlockContent::load($machine_bid); Its returning null – user21513 Dec 21 '16 at 10:35
  • See drupal.stackexchange.com/questions/216672/… – 4k4 Dec 21 '16 at 10:55
0

My case was different: I am loading a custom block in the .theme file. I was not able to get the exact "Block ID" in a digit.

I got the block machine name. In this case, use the following code.

$block_config = \Drupal\block\Entity\Block::load($machine_blockid);
$uuid = $block_config->getPlugin()->getDerivativeId();
$block_content = \Drupal::service('entity.repository')->loadEntityByUuid('block_content', $uuid);
if ($block_content) {
  $img = file_create_url($block_content->field_X_image->entity->uri->value);
  $color = $block_content->field_Y_textfield->value;
}

Otherwise, use the following code. $bid is the block content id. You see this in /block/[bid].

$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$x_img_url = file_create_url($block_content->field_X_image->entity->uri->value);;
$y_textfield_value = $block_content->field_Y_textfield->value;
0

Try this code by including EntityInterface:

// Block 
use Drupal\Core\Entity\EntityInterface;
// the last part of the parameter is the block id
$block = \Drupal\block\Entity\Block::load('yourblockid');
$block_content = \Drupal::entityManager()
  ->getViewBuilder('block')
  ->view($block);
\Drupal::service('renderer')->renderRoot($block_content);
0
$block = \Drupal::service('entity.repository')->loadEntityByUuid('block_content', $uuid);
      if (!empty($block)) {
        $recArticleAdView = \Drupal::entityTypeManager()
          ->getViewBuilder('block_content')
          ->view($block);
        $rendered = \Drupal::service('renderer')->render($recArticleAdView);
        $response = new CacheableResponse($rendered);
        $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($recArticleAdView));
        $variables['content']['sponsored_content'] = Markup::create(trim(strip_tags($response->getContent(), '<div>')));
      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy