0

Searched all over the place but cannot find anything similar. Here's the issue...

The setup:

  • 'Page' content type
  • 'Block Content' paragraph type

The 'Page' content type has a field for referencing the 'Block Content' paragraph, called "Components" (set to unlimited). Now we can use paragraphs within our page. Simple.

The 'Block Content' paragraph type has a field for referencing blocks (using the module Block Field). It also has two other fields for title and subtitle. These fields need to be displayed on the referenced block template for this paragraph instance. This is the issue.

I have included some images to show the setup.

I am trying to use theme_preprocess_block to inject these fields (title and subtitle) into the correct block using $vars['plugin_id'] from the block preprocess but the issue is that the block does not know its paragraph parent. And if I don't know the parent, I can't retrieve these fields that I need to inject into the block.

This answer suggests using the following to get the parent:

$parent   = $variables['elements']['#node']->_referringItem->getEntity();

But in my case I could not find anything equivalent in my block preprocess function.

Update 1:

I tried (from within theme_preprocess_block):

dpm($vars['elements']['content']['#block_content']->_referringItem->getEntity());

This give the following error:

Error: Call to a member function getEntity() on null in limit_preprocess_block__banner_block()

Update 2:

I just dumped the $vars from the preprocess function and noticed that there seems to be the paragraph id in there. But I don't know how to access it from the $vars variable. Is it possible? (see dump image below)

enter image description here

IMAGE 1:

enter image description here


IMAGE 2:

enter image description here


IMAGE 3:

enter image description here


IMAGE 4:

enter image description here


IMAGE 5:

enter image description here

7
  • This is a problem with the data model you are using. Your setup now is referencing configurable blocks and the two fields belong to the block configuration. If this is about referencing block content entities use a standard entity reference field and render all paragraph fields including the block content in the paragraph type. – 4k4 May 4 at 7:13
  • @4k4, thanks for the reply. But we like the usability experience of this setup. Using this dropdown to select the block and limiting the type of blocks is perfect. And we want to avoid using autocomplete for selecting blocks. So I need a work around to get this setup to work. – hga77 May 4 at 7:25
  • @4k4, I just dumped the $vars from the preprocess and there is a parent id. Can I access this? I updated the question with this info. – hga77 May 4 at 7:34
  • If you want to keep the data model you have to add the two additional fields to the block settings, then they are available in preprocess. See for example git.drupalcode.org/project/block_class/-/blob/8.x-1.x/… – 4k4 May 4 at 9:39
  • @4k4, These fields are available in preprocess by loading the hosting node and gaining access to them from the node object. Thats not the problem. I need to somehow get access to the parent paragraph instance. I know its available but I don't know where to look. – hga77 May 4 at 9:49
0

The linked answer was using this inside a node hook.

In case of hook_preprocess_block(&$variables) you can try $variables['elements']['content']['#block_content'] instead. This should return the content entity, similar to the linked answer.

Notice: $whateverEntity->_referringItem is not cache-safe, so if you are referencing the same block on multiple paragraphs, this will not work reliably.

1
  • Thanks. I tried that but it gives an error. I updated my question. – hga77 May 4 at 7:20

Your Answer

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

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