Drupal 8 : Get all available fields by a field type as an array.

The following code will return all the available fields as an array by a field type. You can pass the field type that you want to load within getFieldMapByFieldType() function.

Sample code:

$fieldsArray = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('boolean');
$fields = $fieldsArray['node'];

Note:

“boolean” is a field type here. The returned array will have the field name as a key, field type, and the list of bundles in which this field gets mapped.

Sample result:

Array
(
    [tag_codeexpertz] => Array
        (
            [type] => boolean
            [bundles] => Array
                (
                    [article] => article
                    [page] => page
                )
 
        )
 
    [promote] => Array
        (
            [type] => boolean
            [bundles] => Array
                (
                    [article] => article
                    [page] => page
                )
 
        )
 
)

 

Category: