Change entity reference field widget based on condition? I am using Drupal 8. In user registration form, I use an entity reference field to a profile. I wanted to display inline entity reference form widget or entity select widget based on role.
For this, I tried to use hook__field_widget_form_alter(), but it didn't work.
Is there any way to solve this problem?
For D7, you use hook_field_widget_properties_ENTITY_TYPE_alter but the problem is that in D8 this hook no longer exists.
Took a while to figure out an answer for D8, but here it is.
Since D8 allows you to create Form Display Modes:
1) Add a form mode

2) Select the entity type, User in this case

3) Add name to it

4) In Manage Form Display, click on Custom Display Settings and checkmark it

5) Click on the alternate view mode and change widget

Then in custom module you change the view mode if it has a certain role
/**
 * Implements hook_entity_form_display_alter()
 */
function mymodule_entity_form_display_alter(\Drupal\Core\Entity\Display\EntityFormDisplayInterface &amp;$form_display, array $context) {
  if ($context['entity_type'] == 'user' &amp;&amp; $context['bundle'] == 'user') {
    $current_user = \Drupal\user\Entity\User::load(\Drupal::currentUser()-&gt;id());
    if ($current_user-&gt;hasRole('administrator')) {
      // change the display mode
      $id = $context['entity_type'] . '.' . $context['bundle'] . '.alternate';
      $storage = \Drupal::entityTypeManager()
        -&gt;getStorage('entity_form_display');
      $alt_display = $storage-&gt;load($id);
      $form_display = $alt_display;
    }
  }
}