I wanted to create a module that fetch the entire data from my custom content type to generate a json file. So I used the function

$values = \Drupal\Core\Entity\ContentEntityBase::get('field_name');

to read the field and its values of a content type. Any way to dynamically fetch all the fields and its values. Any solution? Thanks in advance..:D

1 Answer 1


You can get all the field definitions for a content type using getFieldDefinitions from the Entity Field Manager service. The definitions are indexed by field name, so you can iterate on the keys of the returned array for any of the nodes in your site.

An example would be the following:

  // Get the definitions
  $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'my_custom_content');

  // Load a node for which you want to get the field values
  $my_node = \Drupal\node\Entity\Node::load($my_nid);

  // Iterate through the definitions
  foreach (array_keys($definitions) as $field_name) {
    // Get the values for your node
    // Use getValue() if you want to get an array instead of text.
    $values[$field_name] = $my_node->get($field_name)->value;
  • Fatal error: Call to undefined method Drupal\wms_tojson\Controller\TojsonController::getTranslatedField() in /opt/lampp/htdocs/d8test1/core/lib/Drupal/Core/Entity/ContentEntityBase.php on line 455. Any idea please? Feb 10, 2016 at 10:36
  • also getFieldDefinitions() cause a communication link error 1153 Feb 10, 2016 at 10:41
  • 1
    @VishnuJayan, there was a mistake in my code. I have just edited the reply with tested code. Btw, I did not get any error when testing getFieldDefinitions on my D8 site...
    – dinopmi
    Feb 10, 2016 at 11:56

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.