1

I develop a contrib module. I added a new field to "user" entity. I need to hide this field from display in "manage display"

I run this code

$entity_view_display = \Drupal::entityTypeManager()
  ->getStorage('entity_view_display')
  ->load('user.user.default');
if ($entity_view_display) {
  $entity_view_display->setComponent('field_myfield', [
    'region' => 'hidden',
  ])->save();
}

In "manage display" page (/admin/config/people/accounts/display) I see that my field moved to "hidden" region, but when I view user profile I see my field.

If I press "save" button in "manage display" page my field is gone when I view user profile.

How to resave "manage display" programmatically? Is it possible?

1 Answer 1

2

You can use EntityDisplayBase::removeComponent():

$entity_view_display->removeComponent('field_myfield')->save();
1
  • It works, thanks!
    – Vanish
    Aug 7, 2023 at 13:24

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.