16

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

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

What is the equivalent for Drupal 8?

  • 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 '15 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. – Pravin Ajaaz Jun 9 '15 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. – Pravin Ajaaz Jun 9 '15 at 17:22
  • \Drupal::entityManager()->getFieldDefinitions('node', 'article') did the job clive :) – Pravin Ajaaz Jun 15 '15 at 11:35
29

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

$entityFieldManager = \Drupal::service('entity_field.manager');
$fields = $entityFieldManager->getFieldDefinitions($entity_type, $bundle);
  • Is there any way to get only cck field list? – Rajesh Vishwakarma Jul 18 '17 at 14:35
  • 2
    This should be the accepted answer now. – Bram Sep 19 '17 at 16:34
23

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();
    }
  }
  • 2
    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 '16 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 '16 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 '16 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 '17 at 8:38
5

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 it was helped me

  • Some tips: In constructors (and in general) use always interfaces. Most of the drupal service implementations have a well defined interfaces. – ssibal Jan 9 at 17:14

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.