44
55use Drupal \Component \Plugin \Derivative \DeriverBase ;
66use Drupal \Core \Entity \ContentEntityTypeInterface ;
7+ use Drupal \Core \Entity \EntityDisplayRepositoryInterface ;
78use Drupal \Core \Entity \EntityTypeManagerInterface ;
89use Drupal \Core \Plugin \Discovery \ContainerDeriverInterface ;
910use Drupal \Core \StringTranslation \StringTranslationTrait ;
@@ -20,29 +21,42 @@ class EntityRenderedDeriver extends DeriverBase implements ContainerDeriverInter
2021 */
2122 protected $ entityTypeManager ;
2223
24+ /**
25+ * The entity display repository service.
26+ *
27+ * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
28+ */
29+ protected $ entityDisplayRepository ;
30+
2331 /**
2432 * {@inheritdoc}
2533 */
2634 public static function create (ContainerInterface $ container , $ basePluginId ) {
27- return new static ($ container ->get ('entity_type.manager ' ));
35+ return new static (
36+ $ container ->get ('entity_type.manager ' ),
37+ $ container ->get ('entity_display.repository ' )
38+ );
2839 }
2940
3041 /**
3142 * EntityRenderedDeriver constructor.
3243 *
3344 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
3445 * The entity type manager service.
46+ * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository
47+ * The entity display repository service.
3548 */
36- public function __construct (EntityTypeManagerInterface $ entityTypeManager ) {
49+ public function __construct (EntityTypeManagerInterface $ entityTypeManager, EntityDisplayRepositoryInterface $ entityDisplayRepository ) {
3750 $ this ->entityTypeManager = $ entityTypeManager ;
51+ $ this ->entityDisplayRepository = $ entityDisplayRepository ;
3852 }
3953
4054 /**
4155 * {@inheritdoc}
4256 */
4357 public function getDerivativeDefinitions ($ basePluginDefinition ) {
4458 foreach ($ this ->entityTypeManager ->getDefinitions () as $ id => $ type ) {
45- if ($ type instanceof ContentEntityTypeInterface) {
59+ if ($ type instanceof ContentEntityTypeInterface && ! empty ( $ this -> entityDisplayRepository -> getViewModes ( $ id )) ) {
4660 $ derivative = [
4761 'parents ' => [StringHelper::camelCase ($ id )],
4862 'description ' => $ this ->t ("Renders '@type' entities in the given view mode. " , ['@type ' => $ type ->getLabel ()]),
0 commit comments