Problem/Motivation
We would like to get programatically an entity object of block added in Layout Builder, but since `getEntity` method (/core/modules/layout_builder/src/Plugin/Block/InlineBlock.php:233) is protected, we can only get already rendered object, which doesn't fully cover our needs.
It would be great to make `getEntity` method public, so other developers could easily get added block objects if necessary.
Proposed resolution
Change `getEntity` method visibility from protected to public in `ExtraFieldBlock.php`, `FieldBlock.php` and `InlineBlock.php` files.
Issue fork drupal-3223864
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
skdrupal88Comment #4
skdrupal88Comment #5
longwaveComment #6
tim.plunkettCan you expand upon what you're doing with the underlying entity? Would be good to understand that better.
Assigning to @tedbow for his opinion, as he wrote the majority of inline blocks.
Comment #7
skdrupal88@tim.plunkett, we need to calculate the number of article words to send to third-party analytics service, so on node update we grab all data from content blocks text fields and calculate the total number.
Comment #8
tedbowI think there are backwards compatibility concerns with changing the visibility
Given that these classes are @internal and people should not be extending them if they have extended them and overridden
getEntity()then they will get an error because they would have used protected.We could say since they are @internal we shouldn't care if someone has overridden but from the same logic we should not make a new public method on an @internal class for developers to depend on.
I was wondering if you can get the entity for block/component just from context system and I think you can. This rough but I think it works. It would probably need to be cleaned up. This would work on
hook_entity_presaveComment #9
skdrupal88Thanks for the reply @tedbow, I agree with your concerns about the method visibility change, but I also think that we definitely need to provide a simple method to get entity object, and it looks very logical and semantically correct to use method like
->getEntity().I was really confused that I have an access to the rendered block, but don’t have to the entity.
It’s like if we can have access to the rendered body field, but don’t have an access to the raw value when we work with the entity.
According to the Drupal best practices, developers can have an access to values of child entities via references, like we can get an access to referenced node category with
$node->get(‘field_category’)->getValue(). The same way I expect to have an access to the blocks added in layout builder, for example:Comment #13
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
From reading the comments seems as though there is still work/discussion to be add
Did not review MR
Whatever is decided will require test coverage.