Drupal 8, get programmatically the list of fields of a custom content
Asked Answered
P

4

9

I want create programmatically a custom content (custom content created via the admin UI). But, before the creation, I want check programmatically the types of fields of my custom content

My custom content contains a field "body" (type text), a field "description" (type text), an int field (type int), an attached file field (type fid ?)...

I test several ways with the new api of Drupal 8, my last try..

// I get the entity object "my_custom_content"
$entity_object = NodeType::load("my_custom_content");
dpm($entity_object); //Work perfectly


$test = \Drupal::getContainer()->get("entity_field.manager")->getFieldDefinitions("my_custom_content",$entity_object->bundle())
//The \Drupal::getConta... Return an error : The "my_custom_content" entity type does not exist.

With this $entity_object, how can I get the list of the fields of my custom content ? I see the EntityFieldManager Class, the FieldItemList Class...But I still do not understand how to play with drupal 8 / class / poo ... :/

Thanks !

Pauperize answered 25/11, 2015 at 23:34 Comment(0)
O
12

NodeType is the (config) bundle entity for the Node (content) entity.

The correct call would be:

\Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'my_custom_content');

To get the field definitions of any entity_type use the following structure:

\Drupal::service('entity_field.manager')->getFieldDefinitions(ENTITY_TYPE_ID, BUNDLE_ID);

For example, if you want to get all the field definitions of a paragraph bundle with the id multy_purpose_link then replace ENTITY_TYPE_ID with paragraph and BUNDLE_ID with multy_purpose_link

\Drupal::service('entity_field.manager')->getFieldDefinitions('paragraph', 'multy_purpose_link');
Offer answered 28/11, 2015 at 2:54 Comment(4)
Working fine with node content type, but not work with "Paragarph" entity type.Miquelmiquela
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('paragraphs_type', 'paragraphs_name');Miquelmiquela
Updated the answer with a more general explanation.Offer
To get the field names, you can use array_keys() on a result from this answer, e.g. $fieldNames = array_keys($fieldDefinitions);.Garthgartner
B
3

The given answers are deprecated. You should now load the entity and just use getFieldDefinitions() to fetch the field definitions.

$node = Node::load($slide_id);
$field_defs = $node->getFieldDefinitions();
Biquarterly answered 20/9, 2017 at 10:36 Comment(0)
F
1

Or

$field_defs = \Drupal::service('entity_field.manager')->getFieldDefinitions('taxonomy_term', '<taxonomy machine name here>');

If you want to get the list of field_definitions for a taxonomy vocabulary

Fixity answered 19/5, 2017 at 11:13 Comment(0)
H
1

If the entity type does not have a bundle, for example the user entity.

Try this:

 // All user fields and ones added 
 $user_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('user','user'); 

// Just default 
$default_user_fields  = \Drupal::service('entity_field.manager')->getFieldDefinitions('user', NULL); 
Heaveho answered 27/11, 2019 at 23:36 Comment(1)
Thanks @Taggart Jensen. I was searching the bundle to use for custom user fields. This was the perfect answer.Corinacorine

© 2022 - 2024 — McMap. All rights reserved.