Drupal - Get a field instance's settings

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;
}

Tags:

Entities

8