4

In Drupal 8, field_info_field() has been deprecated.

I tried using \Drupal\field\Field::fieldInfo()->getField('field_address') and \Field::fieldInfo()->getField('field_address'), but I get this error.

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

What am I missing?

3 Answers 3

5

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.

6
  • 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() Jul 29, 2015 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, 2015 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. Jul 30, 2015 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);
    – KTM
    Oct 24, 2016 at 8:45
  • This only works for user-created fields, not for native fields like 'uid'.
    – donquixote
    Aug 13, 2017 at 19:41
0

Use this :

use Drupal\field\Entity\FieldStorageConfig;
$field = FieldStorageConfig::loadByName('node', $field_name);
$allowed_values = options_allowed_values($field);
0

For those looking for a D10 answer:

/** @var \Drupal\field\FieldStorageConfigInterface $field_config */
$field_config = \Drupal::entityTypeManager()
  ->getStorage('field_storage_config')
  ->load(implode('.', [$entity_type, $entity_bundle, $field]));

Your Answer

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

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