Score:0

How to hide the fields in manage display when layout module is installed and display when module is uninstalled programmatically?

in flag

In the below config I want to remove the body field from content and add it in hidden as body: true when I uninstall the layout module. Basically when I install the layout module the fields in manage display should get disabled/hidden and when I uninstall the module the fields should be displayed. How can I achieve this programatically?

This config code is of entity_view_display.node.article.default

  uuid: eedc35e4-0592-4f1a-bdc6-47dbf60fa929    
    langcode: en    
    status: true    
    dependencies:   
      config:   
        - field.field.node.article.body 
        - field.field.node.article.comment  
        - field.field.node.article.field_image  
        - field.field.node.article.field_media  
        - field.field.node.article.field_sample_text    
        - field.field.node.article.field_tags   
        - node.type.article 
      module:   
        - layout_builder    
        - text  
        - user  
    third_party_settings:   
      layout_builder:   
        enabled: false  
        allow_custom: false 
    id: node.article.default    
    targetEntityType: node  
    bundle: article 
    mode: default   
    content:    
      body: 
        type: text_default  
        label: above    
        settings: {  }  
        third_party_settings: {  }  
        weight: 0   
        region: content 
    hidden: 
      comment: true 
      field_image: true 
      field_media: true 
      field_sample_text: true   
      field_tags: true  
      langcode: true    
      links: true   
      search_api_excerpt: true
4uk4 avatar
cn flag
Are you looking for a hook like hook_modules_installed/uninstalled or setComponent /removeComponent for the display mode? See https://drupal.stackexchange.com/questions/224686/8-how-do-i-programmatically-enable-a-user-field-under-manage-form-display-and
Libbna Mathew avatar
in flag
in hook_install() I want to hide fields in manage display. Like when I enable/install the layout builder module the fields in manage display should get disabled.
4uk4 avatar
cn flag
You mean the core layout builder module? Then you need the hook I've mentioned. hook_install() runs only for the module where the hook is implemented.
Libbna Mathew avatar
in flag
No no, I have created a custom layout module. And I am working on D9. I just want the content to get emptied in above mentioned config and body field in the content should be be hidden:true. And this functionality should be done in hook_install(). Hope I am able to explain it well!
Libbna Mathew avatar
in flag
I want to disable the field in manage display when I install my custom module.
Score:1
in flag

I have solved the issue. Instead of \Drupal::entityTypeManager() I used

$articleDefaultLayout = LayoutBuilderEntityViewDisplay::load(
    'node.article.default'
);
$articleDefaultLayout->setComponent('body');
Libbna Mathew avatar
in flag
@4uk4 thanks your help.
4uk4 avatar
cn flag
This doesn't matter whether you use entityTypeManager() or the static load(). Both have the same result.
Libbna Mathew avatar
in flag
But when I used entityTypeManager(), removeComponent() was giving error. "Method not found"
4uk4 avatar
cn flag
Then there is a different error. BTW your code example has the disadvantage that it throws an error when the Layout Builder is not installed. It would be a better idea to load the original class or load the entity by entity type id via the entityTypeManager(). Then you get always the currently installed class for this entity type.
Libbna Mathew avatar
in flag
I have got your point. But why it is showing this error then? I did exactly what you mentioned in the code in hook_install(). It is failing to found the removeComponent() method. !!
Score:0
cn flag

To disable the body field when you install the custom module:

mymodule.install

/**
 * Implements hook_install().
 */
function mymodule_install() {
  \Drupal::entityTypeManager()
    ->getStorage('entity_view_display')
    ->load('node.article.default')
    ->removeComponent('body')
    ->save();
}

How to enable the field see 8 - How do i programmatically enable a user field under manage form display and manage display?

Libbna Mathew avatar
in flag
I am getting error in removeComponent() method. And If I want to delete the field using field id so instead of 'body' we can write field id, right?
Libbna Mathew avatar
in flag
I am working on Drupal 9
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.