I have created a block type in the drupal 8 UI (structure/block/block-content/types ) that has two fields that are just strings. I then created 4 pieces of content using that block_type. How do I access the content created by the block type in hook_preprocessor_html programmatically?

1 Answer 1


This is a cutdown version of the function to access the block entities.

function _access_content_block_fields_build_arr($bidArr, $fieldRequestArr)

    $return = array();
    $blockElements = array();
    $blocks = \Drupal\block_content\Entity\BlockContent::loadMultiple($bidArr);
    foreach ($blocks as $block) {
        foreach ($fieldRequestArr as $fieldRequest) {
            $blockResult = $block->get($fieldRequest)->getValue();
            $blockElements[$fieldRequest] = $blockResult[0]; 
        $return[] = $blockElements;

    return $return;

purencool.com/accessing-a-block-types-content-programatically is the entire function for image fields url as well

Your Answer

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

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