What's the equivalent of field_info_instance() for Drupal 8?
The default field settings are stored as configuration in a file like field.field.ENTITYTYPE.BUNDLE.FIELDNAME.yml, but how do I get their current value?
Using the entity_field.manager service, you are able to get an array of BaseFieldDefinition classes. This service is injectable as well, but for copy-paste working code, you can do the following:
$bundle_fields = \Drupal::getContainer()->get('entity_field.manager')->getFieldDefinitions($entity_type, $bundle); $field_definition = $bundle_fields[$field_name]; $catalog_id = $field_definition->getSetting($setting_name); Using a field instance:
$settings = $field->getFieldDefinition()->getSettings(); For example, in a field widget:
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $element = []; // Get the settings. $settings = $items[$delta]->getFieldDefinition()->getSettings(); // ... build render array return $element; } The most simple way to get field config is to use EntityTypeManager.
$entity_type = 'node';// node, taxonomy_term, taxonomy_vocabulary, ... $bundle = 'article';// the content type machine name $field = 'field_article_tags';// the field machine name $fieldConfig = \Drupal::entityTypeManager() ->getStorage('field_config') ->load($entity_type . '.' . $bundle . '.' . $field); I wanted to get the Type and Label from the field definition. This did not work:
$setting_name = 'label'; $label = $field_definition->getSetting($setting_name); This worked:
$label = $field_definition->getLabel(); $type = $field_definition->getType();