Skip to content

Instantly share code, notes, and snippets.

@Greg-Boggs
Last active June 24, 2022 02:38
Show Gist options
  • Save Greg-Boggs/279f6f515717d1abaea4837613edf711 to your computer and use it in GitHub Desktop.
Save Greg-Boggs/279f6f515717d1abaea4837613edf711 to your computer and use it in GitHub Desktop.
Get a list of all fields in Drupal 9/10
<?php
// Get all Node types and all fields
$content_types = NodeType::loadMultiple();
foreach ($content_types as $content_type) {
$types_options[$content_type->id()] = $content_type->label();
foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions('node', $content_type->id()) as $field_name => $field_definition) {
if (!empty($field_definition->getTargetBundle())) {
$field_options[$field_definition->getName() . ':' . $field_name] = $field_definition->getLabel();
//display content type names in the drop down
//$field_options[(string) $content_type->label()][$field_definition->getName() . ':' . $field_name] = $field_definition->getLabel();
}
}
}
asort($field_options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment