4

In Drupal 8 field_info_field() has been deprecated. The docs say to use this instead:

Field::fieldInfo()->getField($field_name)

I've tried various iterations of it, such as:

$foo = \Drupal\field\Field::fieldInfo()->getField('field_address');
$foo = \Field::fieldInfo()->getField('field_address');

But I keep getting an error similar to the following:

Fatal error: Class 'Drupal\field\Field' not found in /var/www/drupal/modules/custom/foo/src/Controller/FooController.php on line ##

What am I missing?

UPDATE

Thanks to Clive's answer, and especially for pointing me towards change records (which I've now subscribed to via RSS), I was able to generate various commands to get data about entity types, bundles and field "info/instances" in Drupal 8.

The data that was once available via field_info_fields() and field_info_instances(), is now available throughout the EntityManager, FieldConfig, and FieldStorageConfig:

$entity_type = 'node';
$bundle = 'course';
$field_name = 'field_address';
$foo = \Drupal::entityManager()->getFieldMap();
$foo = \Drupal::entityManager()->getAllBundleInfo();
$foo = \Drupal::entityManager()->getBundleInfo($entity_type);
$foo = \Drupal\field\Entity\FieldConfig::loadByName($entity_type, $bundle, $field_name);
$foo = \Drupal::entityManager()->getFieldDefinitions($entity_type, $bundle);
$foo = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type);
$foo = \Drupal::entityManager()->getStorage('field_storage_config')->loadMultiple();
// This one doesn't seem to work for me, see my comment to Clive for a workaround.
$foo = \Drupal\field\Entity\FieldStorageConfig::loadByName($entity_type, $field_name);

Here are some other calls that may be useful:

$foo = \Drupal::entityManager()->getAllViewModes();
$foo = \Drupal::entityManager()->getViewModes($entity_type);
$foo = \Drupal::entityManager()->getAllFormModes();
4

According to the change record:

field_info_field($entity_type, $field_name) -> FieldStorageConfig::loadByName($entity_type, $field_name)

So you should use \Drupal\field\Entity\FieldStorageConfig::loadByName from now on.

It's probably best to use change records and issues on Drupal.org itself for docs at the moment, rather than 3rd party websites - I think they have slightly more chance of being right and kept up to date when something changes.

  • Sadly \Drupal\field\Entity\FieldStorageConfig::loadByName('node', 'field_address') is always empty for me. I dug into how loadByName works, and was able to extract the desired results by making this call: \Drupal::config('field.storage.node.field_address')->get() – tyler.frankenstein Jul 29 '15 at 13:56
  • That's wrong, you shouldn't be accessing config entity values directly through config. Doesn't make sense that loadByName() wouldn't work for you. – Berdir Jul 29 '15 at 21:15
  • @Berdir is my example call to loadByName() in my previous comment correct? I've since removed the address module and then tried with a core field, that returns null as well. – tyler.frankenstein Jul 30 '15 at 3:29
  • How to modify the allowed values of a field using the above function ? am getting the allowed values of a field inside settings array when use \Drupal\field\Entity\FieldStorageConfig::loadByName($entity_type, $field_name); – Crazyrubixfan Oct 24 '16 at 8:45
  • This only works for user-created fields, not for native fields like 'uid'. – donquixote Aug 13 '17 at 19:41

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.