In Drupal 7 they can be found in object that is returned by field_info_instance(). Is there a way to get widget settings for certain field name and bundle in Drupal 8?
4 Answers
I believe the new way, as entity_get_form_display is being deprecated, is this:
$entity_type = 'node';
$bundle = 'page';
$form_mode = 'default';
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load($entity_type . '.' . $bundle . '.' . $form_mode);
$specific_widget_type = $form_display->getComponent('some_specific_field_machine_name');
if($specific_widget_type['type'] == 'some_widget_machine_name';
// ^ OR v
$widget_types = $form_display->getComponents();
foreach ($widget_types as $widget_type) {
if ($widget_type['type'] == "some_widget_machine_name") {
// Do something
}
}
I found the way :-)
$settings = entity_get_form_display('node', 'article', 'default')
->getComponent('field_image')['settings'];
Widget settings are part of entity form displays, not field settings
-
1Correct, but could use some more information/example code :)– BerdirCommented Nov 16, 2014 at 19:29
Just shorter version how to get $form_display comparing to RaisinBranCrunch's
$form_display = \Drupal::service('entity_display.repository')->getViewDisplay($entity_type, $bundle, $form_mode);
$form_display->getComponent('field_name')['settings'];
-
the first line give the view display, shouls be
$form_display = \Drupal::service('entity_display.repository')->getFormDisplay($entity_type, $bundle, $form_mode);
– abelassCommented Sep 9, 2022 at 14:08