-1

I was searching for how to find field info and came across getFieldDefinitions on Drupal 8 (why all these deprecated functions on DP8?) which says to use \Drupal::service('entity_field.manager')->getFieldDefinitions(xxx, xxx);.

How can I tell from EntityFieldManager::getFieldDefinitions() that it needs to be called as a service, and that the service is entity_field.manager?

Why doesn't \Drupal\EntityFieldManager::getFieldDefinitions($etid, $bundle) work?

0
1

How can I tell from EntityFieldManager::getFieldDefinitions() that it needs to be called as a service, and that the service is entity_field.manager?

The online docs are quite effective for this. For example, look at the docs for the EntityFieldManager class, which contain an explicit reference:

1 service uses EntityFieldManager

entity_field.manager in core/core.services.yml

  Drupal\Core\Entity\EntityFieldManager


Why doesn't \Drupal\EntityFieldManager::getFieldDefinitions($etid, $bundle) work?

Because getFieldDefinitions is not a static method; you can't call it statically.

1

From the page describing a method, you cannot know if the method is implemented from a service, but if you look at the page describing the class, you will notice the class implements a service.

screenshot

1 service uses EntityFieldManager doesn't mean the code of a service uses EntityFieldManager, but that EntityFieldManager is the class that defines a service. In this case, the machine name of the service is entity_field.manager, as given from the entity_field.manager in core/core.services.yml line.

As for the second question, that is plain PHP. \Drupal\EntityFieldManager::getFieldDefinitions($etid, $bundle) means call the getFieldDefinitions() static method of the \Drupal\EntityFieldManager class. Since \Drupal\EntityFieldManager::getFieldDefinitions() is not a static method, you cannot call it like that, but you need an object, as in $entity_file_manager->getFieldDefinition().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.