Drupal - How to get the list of field-types?

The first level is an array. The keys are the plugin id's. You can list them:

$defs = Drupal::service('plugin.manager.field.field_type')->getDefinitions();
foreach ( $defs as $key => $value ) {
   echo $key, "\n";
}

This is the result:

comment
datetime
file
image
link
list_string
list_float
list_integer
path
text_with_summary
text
text_long
email
boolean
created
changed
timestamp
string_long
language
decimal
uri
float
password
string
integer
entity_reference
uuid
map
taxonomy_term_reference

You can pick one and dump the plugin definition.

var_dump($defs['comment']);

If you are interested in a special object, you can digg deeper with the help of api.drupal.org. But it will be easier, if you search for the plugin in the core directory.

To access an object, locate it in the var_dump and use a method:

$defs['comment']['description']->render();

In this case it is the object TranslatableMarkup, which has the method render to access the translated string.


Every string that is passed through t() is an object now. You can access them as strings, in fact, you should do so.

There are only very few exceptions where you can't just use such an object as-is, for example array keys, for those, you need to cast them to a string. Anything else should just work.

PS: The official API documentation is http://api.drupal.org/api/drupal/8. Use that for core.

Another good resource are change records: https://www.drupal.org/list-changes/published?keywords_description=field_info_field_types&to_branch=&version=&created_op=%3E%3D&created%5Bvalue%5D=&created%5Bmin%5D=&created%5Bmax%5D=

Edit: Responding to your comment, I don't quite understand what you mean. All the information you need is right there, just use it.

$definitions = Drupal::service('plugin.manager.field.field_type')->getDefinitions();
foreach ( $definitions as $field_type => $definition) {
  print $definition['label'];
  print $definition['description'];
  print $definition['provider'];
}

Again, just ignore that label/description are objects. They have __toString() methods that are automatically called when you print/use them.


Google seems to find this Question when searching for these

If you are using Drupal Console you can also use:

drupal plugin:debug field.field_type

There is also this on Drupal.org which can also help:

Defining and using Content Entity Field definitions

Its not easy finding examples of useable though

Tags:

Entities

8