Hi, 

Please let me know how can I set the field widget property programmatically. Here is the screenshot of the Manage Form Display of my website, I am trying to set the widget of the field Positional Index to Simple Hierarchical Select programmatically..

https://imgur.com/a/n8PLaLQ

Please let me know, thanks.

Comments

wombatbuddy’s picture

Here is the example of setting this widget for the 'field_tags' of the 'Article' content type.
(the entity is 'node', the bundle is 'Article', the form mode is 'default): 

/**
 * Implements hook_install().
 */
function my_module_install() {
  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
  $form_display = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('node.article.default');

  $form_display->setComponent('field_tags', [
    'type' => 'options_shs',
  ]);

  $form_display->save();
}

The links that may be helpful:
1 https://api.drupal.org/api/drupal/core%21includes%21entity.inc/function/...
2 https://www.drupal.org/docs/8/creating-custom-modules/include-default-co...
3 https://hechoendrupal.gitbooks.io/drupal-console/content/en/commands/con...

Saptaparnee’s picture

Thank you so much. It worked. But could you please let me know how you selected the type => 'options_shs', I mean I could not figure out how to find out the name like "options_shs", which I assume is some machine name of the Simple Hierarchical Select module that I enabled.

wombatbuddy’s picture

First way:
-------------
1. Select the 'Simple Hierarchical Select' widget on 'Manage form display' page.

2. Export configuration of your content type using the following Drupal Console command:

drupal config:export:content:type article \
--module="my_module"

3. Explore the content of the 'core.entity_form_display.node.article.default.yml' file which was generated.

Second way:
-----------------
1. Select the 'Simple Hierarchical Select' widget on 'Manage form display' page.

2. Visit
/admin/config/development/configuration/single/export

3. Export 'Entity form display' you need.
See the screenshot:
https://cdn1.imggmi.com/uploads/2019/10/29/cdcd0fbb73412be05209f490040e6...

4. Find in the code of the configuretion your field and get properties your need.
The screenshot of the example:
https://cdn1.imggmi.com/uploads/2019/10/29/60ee998738dda63adbaf38abdd181...

Third way:
--------------
You can watch the annotation of the widget's plugin.
1. Visit
modules/contrib/shs/src/Plugin/Field/FieldWidget

2. Read the source code of the 'OptionsShsWidget.php' file.
See the screenshot:
https://cdn1.imggmi.com/uploads/2019/10/29/0fb6650bd092d83c36a93274d62f7...

Saptaparnee’s picture

Thank you so much :)

mourad-zitouni’s picture

Thanks very much wombatbuddy, you saved my day!

anybody’s picture

Also see this core issue and help to get things forward to implement field change helpers for better DX:

https://www.drupal.org/project/drupal/issues/937442

Also this documentation page contains very helpful snippets: https://www.drupal.org/docs/drupal-apis/update-api/updating-entities-and...

http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes