21

In Drupal 7, to get all fields used in a content type, I use the following code.

$fields = field_info_instances('node', 'article');

What is the equivalent for Drupal 8?

4
  • 1
    Are you working with an entity? If so: $fields = $entity->getFieldDefinitions(); will do it. I'm not sure what the equivalent of field_info_instances() is, FieldStorageConfig doesn't appear to have one
    – Clive
    Jun 9, 2015 at 14:14
  • No I just want to alter the node_type_edit_form and add a list of fields defined by the node type in the form. So the type of the node is the only argument I have. So I thought function like field_info_instances might help. Jun 9, 2015 at 14:34
  • @Clive: can I create an entity object using the entity type name. There is a function NodeType::load('article') which creates object for node types. Jun 9, 2015 at 17:22
  • \Drupal::entityManager()->getFieldDefinitions('node', 'article') did the job clive :) Jun 15, 2015 at 11:35

3 Answers 3

49

EntityManager is deprecated moving forward. The following can be used instead:

$entityFieldManager = \Drupal::service('entity_field.manager');
$fields = $entityFieldManager->getFieldDefinitions($entity_type, $bundle);

Whereas $entity_type can be 'node' and $bundle can be 'article'

2
  • Is there any way to get only cck field list? Jul 18, 2017 at 14:35
  • 3
    This should be the accepted answer now.
    – Bram
    Sep 19, 2017 at 16:34
26

I achieved it using getFieldDefinitions() defined in Class EntityManager. So in order get all fields used in a particular bundle, Here is a way:

$bundle_fields = \Drupal::entityManager()->getFieldDefinitions('node', 'article');

Though the above $bundle_fields also contains nid, uuid, revisions, langcode, etc as fields. So to get an accurate output I did something like this:

  $entity_type_id = 'node';
  $bundle = 'article';
  foreach (\Drupal::entityManager()->getFieldDefinitions($entity_type_id, $bundle) as $field_name => $field_definition) {
    if (!empty($field_definition->getTargetBundle())) {
      $bundleFields[$entity_type_id][$field_name]['type'] = $field_definition->getType();
      $bundleFields[$entity_type_id][$field_name]['label'] = $field_definition->getLabel();
    }
  }
6
  • 4
    This works, however entityManager is deprecated. The comment above the function definition says use entityTypeManager or if the method is not implemented (getFieldDefinitions is not) "see the deprecated \Drupal\Core\Entity\EntityManager to find the correct interface or service." I don't understand this comment. Where can I look to find the correct function to use?
    – Felix Eve
    May 3, 2016 at 22:50
  • 5
    To filter out the base fields (nid, uuid, revisions, langcode, etc) you can do if($field->getFieldStorageDefinition()->isBaseField() == FALSE)
    – leon.nk
    May 18, 2016 at 8:56
  • 1
    Is there a way to get the Field Definitions to all types of content types not just one?
    – Matt
    Aug 26, 2016 at 19:10
  • 1
    @leon.nk yes <code>if($field->getFieldStorageDefinition()->isBaseField() == FALSE)</code>that's more precise than <code>if (!empty($field_definition->getTargetBundle()))</code> to filter out base fields.
    – Hanmant
    Aug 25, 2017 at 8:38
  • In case of a list with key|values how can I get the values from that field?
    – DarkteK
    Mar 3, 2021 at 16:02
10

EntityManager is deprecated and I used the next code. I added to Controller class:

/**
 * The entity field manager.
 *
 * @var \Drupal\Core\Entity\EntityFieldManager
 */
protected $entityFieldManager;
    
/**
 * Constructor.
 *
 * @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
 *   The entity field manager.
 */
public function __construct(EntityFieldManager $entity_field_manager) {
  $this->entityFieldManager = $entity_field_manager;
}
    
/**
 * {@inheritdoc}
 */
public static function create(ContainerInterface $container) {
  return new static(
    $container->get('entity_field.manager')
  );
}

// Here some your functions

/**
 * Build table rows.
 */
protected function buildRows() {
  $entity_type_id = 'node';
  $bundle = 'article';
  $fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle);
  
  foreach ($fields as $field_name => $field_definition) {
    if (!empty($field_definition->getTargetBundle())) {               
      $listFields[$field_name]['type'] = $field_definition->getType();
      $listFields[$field_name]['label'] = $field_definition->getLabel();                  
    }
  }

  $rows = [];
  foreach ($listFields as $field_name => $info) {
    $rows[] = $this->buildRow($info, $field_name);
  }

  return $rows;
}

https://www.drupal.org/node/2549139 is what helped me

2
  • 2
    Some tips: In constructors (and in general) use always interfaces. Most of the drupal service implementations have a well defined interfaces.
    – ssibal
    Jan 9, 2019 at 17:14
  • 1
    In case of a list with key|values how can I get the values from that field?
    – DarkteK
    Mar 3, 2021 at 16:02

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.