2

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 4

4

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

I found the way :-)

$settings = entity_get_form_display('node', 'article', 'default')
  ->getComponent('field_image')['settings'];
1

Widget settings are part of entity form displays, not field settings

1
  • 1
    Correct, but could use some more information/example code :)
    – Berdir
    Nov 16, 2014 at 19:29
0

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'];
1
  • the first line give the view display, shouls be $form_display = \Drupal::service('entity_display.repository')->getFormDisplay($entity_type, $bundle, $form_mode);
    – abelass
    Sep 9, 2022 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.