0

I don't understand why they are so much functions deprecated on Drupal 8. From the basics (node load, node delete,...) to more complicated (getFieldDefinitions,...).

And their is no explanation for using the new way of implementation in the official documentation.

For example, I want to get the definitions of all my fields for a content type. The old way was :

Drupal::service('entity_field.manager')->getFieldDefinitions(xxx, xxx);

For somes reasons, its now deprecated. Now, we have to use this. But how use it... that's the question. Their absolutely no example or help. I would use the getFieldDefinitions function from this class, but I have to create a instance from it, the function is not static.

How do I do :( ?

2
  • I'm stuck to create a instance of EntityFieldManager, they're 8 parameters for the consctructor. But most of all, I tought DP8 was using service and dependencies injection. For me, the EntityFieldManager is already instantiated, but how to get it ? Drupal::service('entity_field.manager') was working that way.
    – Gaius
    Commented Jun 8, 2016 at 12:55
  • Some cross-posts happening there, my fault - the answer should help but let me know if it's still not clear
    – Clive
    Commented Jun 8, 2016 at 12:59

2 Answers 2

10
Drupal::service('entity_field.manager')->getFieldDefinitions(xxx, xxx);

Is the current way to get field definitions.

It's the EntityManager service that has been deprecated, not EntityFieldManager::getFieldDefinitions. A deprecation notice on a method doesn't apply to the code used within the method, just the use of the method itself.

The former currently proxies straight through to the latter, so it isn't really useful any more.

As a general rule, functions are deprecated when they're no longer useful, or when they break convention with the rest of the codebase. Don't fear it, embrace it. Progress is a good thing :)

3
  • All right. So, Drupal::service('entity_field.manager')->getFieldDefinitions; is calling EntityFieldManager::getFieldDefinitions. The definition of the service is still the same, but it work differently by using EntityFieldManager::getFieldDefinitions instead of EntityManager::getFieldDefinitions, right ?
    – Gaius
    Commented Jun 8, 2016 at 12:59
  • @Gaius No, they are identical. Both the version in the Drupal static service wrapper and EntityManager::getFieldDefinitions() end up calling EntityFieldManager::getFieldDefinitions() via the container.
    – mpdonadio
    Commented Jun 8, 2016 at 13:02
  • Well, no, unless I'm misunderstanding you... EntityManager::getFieldDefinitions contains one line of code, which grabs a reference to the entity field manager service, and returns the result of invoking getFieldDefinitions on that class. So the return value from EntityManager::getFieldDefinitions is always identical to the return from EntityFieldManager::getFieldDefinitions. The only real thing to take away form this is to use the non-deprecated method, not the deprecated one. They both do the same thing. Does that help?
    – Clive
    Commented Jun 8, 2016 at 13:03
3

The development cycle for Drupal 8 took a long time, much longer than anyone anticipated. Because of this, early decisions that were made were refined and changed, as the system evolved. This is why some things are marked as @deprecated.

Some things, mainly the global functions like format_date() still exist to aid in the transition to Drupal 8. Others, like EntityManager have outgrown their initial purpose.

For the specific example you give, you can read the change record (all @deprecated classes / methods / functions should have a CR (except for some really early ones). Essentially, EntityManager grew in scope so it was decided to split out into individual services to allow code (and core maintenance) to be more manageable. The current version of EntityManager actually gets all of the sub-services injected into it, and the old methods call out to the methods on the sub-service.

Deprecated code will not go away in Drupal 8. Usage in core will go away.

Your last part about "I would use the getFieldDefinitions function from this class, but I have to create a instance from it, the function is not static." sound like a different question that needs to be fleshed out more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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