I need a list of all content entities, to query them all and check if they have a specific field.
Couldn't find this in the API doku.
This is what you need:
use Drupal\Core\Entity\ContentEntityType;
$content_entity_types = [];
$entity_type_definations = \Drupal::entityTypeManager()->getDefinitions();
/* @var $definition EntityTypeInterface */
foreach ($entity_type_definations as $definition) {
if ($definition instanceof ContentEntityType) {
$content_entity_types[] = $definition;
}
}
But a better approach would be to get the field first and then the bundles which has this field. For example, we are looking for the "field_image" in "node" typed entities:
use Drupal\field\FieldStorageConfig;
$field_image = FieldStorageConfig::load('node.field_image')
$bundles = $field_image->getBundles();
Builds a list of entity type labels suitable for a Form API options list
\Drupal::entityManager()->getEntityTypeLabels(TRUE);
Use in form API
$element['target_type'] = [
'#type' => 'select',
'#title' => t('Type of item to reference'),
'#options' => \Drupal::entityManager()->getEntityTypeLabels(TRUE),
'#default_value' => '',
'#required' => TRUE,
];