Score:1

Hide field programmatically in "manage display"

hm flag

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?

Score:2
cn flag

You can use EntityDisplayBase::removeComponent():

$entity_view_display->removeComponent('field_myfield')->save();
Vanish avatar
hm flag
It works, thanks!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.