[generate:entity:content] Add 'view_builder' to config entity annotation template. Fix #3129. #3311
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a patch for #3129.
This allows content entities with bundles to be exposed through the core REST API without throwing errors of the form:
Steps to reproduce & fix manually have been well described by @tahirm in the linked issue, so I won't re-explain them here.
This patch adds a default 'view_builder' to all config entity definitions. It uses the Core
Drupal\Core\Entity\EntityViewBuilder
& doesn't bother defining a custom extension class in the same way as, for example, the 'list_builder':Drupal\{{ module }}\{{ entity_class }}ListBuilder
as I haven't come across a use case for this level of flexibility yet.I checked Core to see where it extends
EntityViewBuilder
rather than using it directly and found of the >30 Drupal Core config entities, only 2 have 'view_builder' defined:\Drupal\tour\Entity\Tour
=>Drupal\tour\TourViewBuilder
\Drupal\block\Entity\Block
=>Drupal\block\BlockViewBuilder
These both appear to be bundle config entities, which fits with the use case being fixed here.
I was concerned that adding 'view_builder' unconditionally to
entity.php.twig
might be too far reaching, as it doesn't look like a config entity needs it defined unless they specify a canonical route. However, it appears that the only usage of this template is for adding content entity's bundle classes insrc/Generate/EntityConfigGenerator
, so I believe this patch will only affect what it should.