Deprecated code cleanup: Updating EntityManager to EntityFieldManagerInterface

drupal.stackexchange https://drupal.stackexchange.com/questions/293253

  •  24-02-2021
  •  | 
  •  

EntityManager is deprecated so I am trying to update code for Drupal 9. The documentation says it should usually be changed to EntityTypeManager, but in this case the code is using getFieldStorageDefinitions so it needs to be EntityFieldManagerInterface.

I changed this line:

  $fields = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type);

To:

  $fields = \Drupal::EntityFieldManagerInterface()->getFieldStorageDefinitions($entity_type);

Which gives this error when I run behat:

  [error]  Error: Call to undefined method Drupal::EntityFieldManagerInterface() in _drush_behat_get_entity_field_types() (line 95 of /app/drush/Commands/contrib/behat-drush-endpoint/behat.inc) #0 /app/drush/Commands/contrib/behat-drush-endpoint/behat.inc(123): _drush_behat_get_entity_field_types('node')

However, I don't understand this error, because EntityFieldManagerInterface does have getFieldStorageDefinitions.

How can I update entityManager to EntityFieldManagerInterface?

Here's the full function if that's useful:

function _drush_behat_get_entity_field_types($entity_type) {
  $return = array();
  $fields = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type);
  foreach ($fields as $field_name => $field) {
    if (_drush_behat_is_field($entity_type, $field_name)) {
      $return[$field_name] = $field->getType();
    }
  }
  return $return;
}
有帮助吗?

解决方案

EntityFieldManager has no shortcut in \Drupal, so you need to get it through \Drupal::service():

$fields = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type);
许可以下: CC-BY-SA归因
scroll top