简体   繁体   中英

Fields that are imported via config are disabled by default. How to enable them?

In a custom module for Drupal 8 I'm able to add a new content type with all of its fields, but all of the fields are disabled by default. How do I enable them during install of my module?

Edit To be clear, module fields are not being enabled by default under Manage Form Display. How do I enable (or disable) fields using something like hook install?

Edit 2 Along with enabling fields by default in a newly created content type, installing this module also updates fields on an existing content type (Article) and those fields are also disabled.

Edit 3 disabled meaning they are shown as disabled on the form display for the content type and users cannot see the fields when adding content.

There must be code that I can run via hook_install that enabled fields on a content type?

I'm presuming that by "disabled" you mean that the fields aren't displayed when an instance of your content type is made.

Personally I've been using the UI to set up how each of my fields are displayed through the "Manage Display" page provided for each content type. I drag the fields that I want to enable from the disabled section as you would normally.

When I'm happy with how the fields are arranged I then use the brilliant inbuilt export functionality (think features in Drupal 7 but way better) to generate the .yml config files for my content type.

This process is described brilliantly by this article on the Drupal website. In this case, as it's just the config for which fields that we want to display, it should be as simple as exporting the "Entity View Display" config for the content type to a .yml file. (note that you need to name your yml files correctly, this is explained in the article (and I think that the export module provides the auto-generated name anyway)).

Here is what the export looks like for one of my content types: 实体视图显示导出

Once I've generated the config files that I want to apply to Drupal when the module is enabled it really is as simple as making two directories in the root directory of the module:

"./config/install"

and placing the config files into the install directory.

Now, when the module is first installed Drupal will apply the configuration provided by the files in the /config/install directory.

This process is elaborated upon here: https://www.drupal.org/docs/8/creating-custom-modules/include-default-configuration-in-your-drupal-8-module

When you export a field you have to export too:

  • "Field declaration" field.field.[entity_type].[bundle].[field_name].yml file. Don't forget to add re-used fields declaration
  • "Field Storage" field.storage.[entity_type].[field_name].yml file
  • "Form Display" where you have modified the field configuration. core.entity_form_display.[entity_type].[budle].default.yml
  • "Manage Display" where you have modified the field configuration. core.entity_view_display.[entity_type].[budle].[view_mode_id].yml files (If you have Default, Teaser, etc)
  • "Entity", if the entity is new. [entity_type].[type|paragraph_type|vocabulary].[bundle].yml new. [entity_type].[type|paragraph_type|vocabulary].[bundle].yml file

    I recommend you to use " drush cex/cim " commands to import and export configuration.

I don't know too much about Drupal 8 but when i try to create custom content type to use this article.

https://www.drupal.org/docs/8/api/entity-api/creating-a-custom-content-type-in-drupal-8

Then when i enable our custom module then content type is created successfully and the content type fields are also enabled.

Also i have try to create a node in this content type then the node is successfully created.

So please check this article.

It may be helpful for you.

Thanks,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM