3

I am trying to call field_info_field_map() in my DefaultForm.php . What is the right way? I am getting the following error:

Error: Class 'Drupal\module_name\Form\Field' not found in Drupal\module_name\Form\DefaultForm->module_name_settings_get_field_options()

The code I am using is as follow:

$fields = Field::fieldInfo()->getFieldMap();

the code i am trying to convert to d8

function mn_settings_get_field_options() {
  static $options = null;
  if (is_array($options)) {
    return $options;
  }
  $options = array();
  $fields = field_info_field_map();
  foreach ($fields as $field_name => $field_data) {
    if (isset($field_data['bundles']['node'])) {
      $node_types = $field_data['bundles']['node'];
      $options[$field_name] = $field_name . ' (In content types: ' . implode(', ', $node_types) . ')';
    }
  }
  asort($options);
  return $options;
}
8
  • Did you specify the class via use at the top of the file under namespace?
    – Kevin
    Commented Jan 20, 2017 at 21:01
  • @Kevin i did specify use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\field\Field; but still getting error . The reference to getFieldMap() is found in this link drupalcontrib.org/api/drupal/… Commented Jan 20, 2017 at 21:06
  • That site is way outdated. Use only the official API docs.
    – Kevin
    Commented Jan 20, 2017 at 21:08
  • @Kevin this seems to do the job <code>$fields = \Drupal::entityManager()->getFieldMap();</code> but the output array for field doesnt tell whether the bundle is node or other entity Commented Jan 20, 2017 at 22:14
  • Not sure what you're talking about, but drupalcontrib.org is not up to date, nor official as far as I know.
    – Kevin
    Commented Jan 20, 2017 at 22:16

2 Answers 2

2

Looks one potential solution is to use is EntityFieldManager::getFieldMap. See the change log for this change.

Also, FYI:

When working directly with an $entity, the field definitions (which include base and configurable fields) can be directly requested from the object with $entity->getFieldDefinitions() or $entity->getFieldDefinition($field_name)

9
  • i am still have problem with name space \Drupal::entityManager()->getFieldMap(); works but if i try entityManager ::getFieldMap(); it doesnt work! same with EntityFieldManager::getFieldMap . I m using name space use Drupal\Core\Entity; Commented Jan 20, 2017 at 22:58
  • i have update the code in the question , this is what i am trying to implement in D8 Commented Jan 20, 2017 at 23:04
  • 3
    Calling EntityFieldManager::getFieldMap() doesn't work because getFieldMap is not a static method (php.net/manual/en/language.oop5.static.php). entityFieldManager is a Drupal service called entity_field.manager and to call the function you should first ask for the service instance \Drupal::service('entity_field.manager')->getFieldMap()
    – Eyal
    Commented Jan 21, 2017 at 9:08
  • @Eyal @kevin how to get the entity type id of the bundle , in D7 field map return array as field[bundles][node][0] => ''content type " . In D8 field map functions return like field[bundle][0]="content type" . I n my case i want to filter the bundle with entity id 'node'. code $fields = field_info_field_map(); foreach ($fields as $field_name => $field_data) { if (isset($field_data['bundles']['node'])) { $node_types = $field_data['bundles']['node']; $options[$field_name] = $field_name . ' (In content types: ' . implode(', ', $node_types) . ')'; } } code Commented Jan 23, 2017 at 17:35
  • Too much code for comments.
    – Kevin
    Commented Jan 23, 2017 at 17:51
1

The following DB query will give me list of all fields attached to node: select name from config where name like 'field.field.node%';

1
  • @eyal finally i got the solution. Commented Mar 16, 2017 at 21:41

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.