Problem/Motivation

Referenced block entities (not content blocks) do not show up when rendered UNLESS they are visible anyway in some region.
This is due to access and visibility check.

Proposed resolution

Add an entityreference formatter option "ignore access checks" that opens a sitebuilder the possibility to solve this.

Remaining tasks

Check if this is feasable.
Hack it.

User interface changes

Additional formatter option.

API changes

TBD

Data model changes

TBD

Original Report

When an entity reference field references a block, and the field formatter is set to render item, the block will only be visible if it is already placed in a region on the theme block administration page. It should be possible to view the block even if the block is not generally visible in a region.

Steps to reproduce:

  1. Create a entity type (node, taxonomy term, block etc.) with an entity reference field
  2. Set target entity type to block
  3. Set field formatter to rendered entity
  4. Create a custom block that is not visible in a theme region
  5. Reference the block from your content type
  6. The referenced block will not be visible

This can be useful in many cases. I need it to create blocks with tabbed content (blocks within a block).

The reason for the problem is that the block module checks for each block's status. If it is not active in the current theme, it will be marked with status = FALSE. Then in the access check of the block module the access to the block is not permitted and the block is not rendered.

It is pretty easy to make a fix for this by implementing hook_ENTITY_TYPE_load() and set status to TRUE. However, I think a better way is to change core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php to set the status to TRUE if the entity has the setStatus method enabled. But this approach does not work as the access to the block has already been checked and cached. So it would be necessary to invalidate the cache.

Can anybody see a nice solution to this?

Comments

cilefen’s picture

Priority: Minor » Normal
alexej_d’s picture

I can confirm this bug. It is a major flaw. The only workaround right now would be to create a region you would not render anywhere and add your blocks to it before you can reference them…

tim.plunkett’s picture

There is a difference between a custom block (block content) and a block entity. Which entity are you referencing?
You should be referencing the custom block, not the block entity.

bjaxelsen’s picture

@tim.plunkett: I am not quite sure I understand the question: In my case I created a block and referenced other blocks (two Views blocks) and only the Views block that was otherwise visible (placed in a region of the theme) was rendered. The other View block was not rendered (it has status = FALSE and will be hidden).

Should I supply some yaml files of the blocks and views, or could any other details clarify the issue?

alexej_d’s picture

@tim.plunkett There are several use cases when you want to reference a views block or a config entity type block supplied by a module (so yes, we are not dealing with content entities). Both types can only by referenced successfully once you add them to a region.

tim.plunkett’s picture

berdir’s picture

Yeah, using entity reference to reference block configs is just not a very good match I think. entity reference checks access and that includes visibility settings and so on.

One possibility would be to write a formatter just for block configs that doesn't care about access. But it's still very weird to have to configure dummy blocks to then use them in a completely different way.

Something that builds on top of plugin field types provided by http://drupal.org/project/plugin might make a lot more sense. Then you'd select a block *plugin* and can configure it on the spot, just like the block config form. That's a lot closer to how e.g. page manager uses blocks as well.

Don't really think this is a bug (that we need to solve in core). Just because an entity reference can reference any entity type doesn't mean that every option needs to actually work. It can't render views either, for example.

alexej_d’s picture

@Berdir so you think it is acceptable that a core feature is not working most of the time? Wouldn't it be better to completely remove the feature to reference config entities since it does not work as expected?
Still, in my opinion it would be a killer feature to reference views and views blocks and it is really something you would expect from the entity reference field. But it is probably to late to get that into core :(

amateescu’s picture

Still, in my opinion it would be a killer feature to reference views and views blocks and it is really something you would expect from the entity reference field.

You can reference views and blocks just fine with the core ER field, the problem is how you display a config entity since most of them are not meant to be "viewed". I also proposed the custom formatter solution in IRC yesterday but.. I'm not so sure about it anymore :)

Edited: see next comment.

amateescu’s picture

On the other hand, disabling the "Rendered entity" formatter if the ER field references a config entity probably makes more sense, since config entities do not have an entity view builder (i.e. they are not meant to be "rendered/viewed").

Edit: I just remembered that the block config entity does have a view builder, which is complete nonsense :(

berdir’s picture

Well, block entities can be viewed :)

And we already do exactly that:

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    // This formatter is only available for entity types that have a view
    // builder.
    $target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
    return \Drupal::entityManager()->getDefinition($target_type)->hasViewBuilderClass();
  }
amateescu’s picture

Issue tags: -blocks, -Entity Reference

Yeah, it's been a while since I looked at the code :/ Anyway, the gist of the problem is that blocks have a view builder and they shouldn't.

xano’s picture

Something that builds on top of plugin field types provided by http://drupal.org/project/plugin might make a lot more sense. Then you'd select a block *plugin* and can configure it on the spot, just like the block config form. That's a lot closer to how e.g. page manager uses blocks as well.

This is indeed possible, but #2532968: Block plugin forms assume they are top-level prevents block plugin configuration from being stored at the moment. We're working on fixing this.

alexej_d’s picture

I think if a config entity has an own view builder (like blocks) it should be renderable using the entity reference formatter, any other behaviour is really just irritating and will lead many new Drupal developers to the conclusion that something is broken (making them loose time trying to fix it).

badrange’s picture

I lost time today apparently due to this issue. With Drupal 7 I have at a couple of occasions created a view block, and used the block reference module to add fields where I allow editors to choose which of their favourite blocks are rendered on a node (typically view blocks, can be any block).

Pretty neat solution if you'd like the editors to be able to choose which listings shows up on the front page with a minimum amount of configuration, they can take the news listing, event listing etc.

Block reference has a D8 issue at (https://www.drupal.org/node/2410289) where one of the commenters points out this:

It looks like in Drupal 8, you can create an Entity Reference field and reference either Custom Blocks (content entity) or Plugin Blocks (configuration entity), but not both.

but it doesn't really work, does it - in my case I either get two copies of the block on a page (placed both through the reference AND through blocks layout page, or none.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jojonaloha’s picture

I was just playing with creating an Entity Reference to a View and the only formats available are Label and Entity ID. I think amateescu is right in #9 that it doesn't make sense to render MOST config entities, but there are some that do render. Like Blocks and Views. In those cases I think it also makes sense to allow Contrib to come up with the custom Field Formatters for those things. Ideally those two specifically could be moved into core.

So, my question is, does anybody know of a contrib or sandbox module that has a Entity Reference Field Formatter for Views config entities?

All I keep getting is https://www.drupal.org/project/views_field_formatter and https://www.drupal.org/project/entityreference_views_formatter which don't seem to be what I'm looking for.

jasonawant’s picture

@jojonaloha,

There are few options out there.

Viewfield: https://www.drupal.org/node/2220929
Sandbox project Entity reference view formatter: https://www.drupal.org/sandbox/paulmckibben/2481503
Entity Views Attach: https://www.drupal.org/node/1937618
Core, Add a ViewBuilder for Views: https://www.drupal.org/node/1857422

jojonaloha’s picture

Thank you @jasonawant, those look promising! I'll evaluate those to see which seems most viable for my project.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jibran’s picture

Component: entity_reference.module » entity system
Status: Active » Closed (works as designed)
Issue tags: +DER issue

Closing it as per #7 and #12. Feel free to re-open it you disagree. Meanwhile you can use DsField to add a block from https://www.drupal.org/project/ds.

geek-merlin’s picture

@jibran #21:
> Meanwhile you can use DsField to add a block from https://www.drupal.org/project/ds.

I did not see how you can override access here - can you?

geek-merlin’s picture

Title: Entity reference cannot render blocks » Add entityreference formatter option to "ignore access checks"
Version: 8.2.x-dev » 8.3.x-dev
Category: Bug report » Feature request
Issue summary: View changes
Status: Closed (works as designed) » Active

So the use case is to render a referenced block that is in no visible region.
The conslusion is "not possible because of access checks".

So the most natural thing is to bring back tha D7 entityreference formatter option to "ignore access checks".

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

d.fisher’s picture

I'm not sure if something has changed since the last time I visited this issue.

To fix this issue on a previous project (using Drupal 8.2.4) I created a "reference" region in my theme info file and place a block in it which didn't get rendered in my page.html.twig. I would then create an entity reference field on the node and select the specific block I'd placed in the "reference" region and the block would magically render. It was somewhat messy having to follow these steps but at least it worked.

Now using Drupal 8.2.6 this doesn't seem to work any more. The block doesn't render.

: (

d.fisher’s picture

Ignore my previous comment. I was referencing a block that had anonymous only permissions on it and I was logged in!!!

So to confirm for anyone wanting to resolve this issue for now (just elaborating on comment #2 from @alexej_d).

  1. Add a reference region to your .info file in your theme (reference: 'Reference')
  2. You probably need to rebuild caches at this point
  3. Drop the block you want to reference in to this region in your block layout. Because you haven't rendered the region in your page.html.twig it won't appear anywhere on the page
  4. Now reference the same block in your entity reference field
  5. If you've set the display to 'Rendered entity' you should now see the block rendered inside your field

Hope that helps somebody until this issue makes further progress.

berdir’s picture

There are various projects that offer to select a block plugin directly now, without going through creating a block placement first with some strange hacks. Note that all those things cause overhead because Drupal will prepare such blocks, check access for them and so on on every page.

See https://www.drupal.org/project/block_field for example, which is working quite well for us, it also easily allows to limit access to just a few blocks or just select one by default and then you can use field permissions to hide it for users.

d.fisher’s picture

I didn't know about this module. Looks good. I'll check it out. Certainly feels like a nicer solution than hacking a region which doesn't render.

drupalmonkey’s picture

Block Field module seems to be working well for us as well.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

VishalKumarSahu’s picture

I am experiencing the same issue and not getting any theme_preprocess function to solve the issue.
The way I am trying to render is Darren Fisher's comment [#26].

Other threads with similar issues are-
https://www.drupal.org/project/drupal/issues/2704331
https://www.drupal.org/project/drupal/issues/2730217

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: - +stale-issue-cleanup

Thank you for sharing your idea for improving Drupal.

We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.

Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

Thanks!

berdir’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

This was already closed in 2016. I disagree that the UI should offer an option to bypass access, if someone wants that they can do that in contrib. 10 years later, we also have various alternatives and replacements for this with canvas and layout builder and what not that clearly people haven't been needing this.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.