Skip to content
Snippets Groups Projects

Issue #3501685 by a.dmitriiev: Error: Call to a member function getTargetEntityTypeId() on null

Merged Issue #3501685 by a.dmitriiev: Error: Call to a member function getTargetEntityTypeId() on null
1 file
+ 15
13
Compare changes
  • Side-by-side
  • Inline
@@ -2,8 +2,6 @@
namespace Drupal\entity_reference_display\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
@@ -15,23 +13,27 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
trait EntityReferenceDisplayFormatterTrait {
/**
* Constructs a new MyEntityReferenceDisplayFormatter.
* The entity repository service.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* The entity repository service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
* The entity field manager service.
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
public function __construct(protected EntityRepositoryInterface $entityRepository, protected EntityFieldManagerInterface $entityFieldManager) {}
protected $entityRepository;
/**
* The entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$container->get('entity.repository'),
$container->get('entity_field.manager')
);
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->entityRepository = $container->get('entity.repository');
$instance->entityFieldManager = $container->get('entity_field.manager');
return $instance;
}
/**
@@ -115,7 +117,7 @@ trait EntityReferenceDisplayFormatterTrait {
$entity_bundle = $field_definition->getTargetBundle();
if (!empty($entity_bundle)) {
$entity_fields = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type, $entity_bundle);
->getFieldDefinitions($entity_type, $entity_bundle);
/** @var \Drupal\Core\Field\FieldDefinitionInterface $field */
foreach ($entity_fields as $field) {
// Formatter is only available for entity types with display mode field.
Loading