How to create content type, fields and view while installing custom module in Drupal 9 using configuration manager.

Here we are going to discuss how to move your content type and view in to custom module so that it will create content types , fields and view swhile installing module.

Normally developers creates content types , fields , views and view configurations in Admin UI, SO while sharing same changes to other environment or publishing in drupal.org it will be difficult to manually creating these configurations and fields.

Assuming we have content type Job application, this content type has below fields.

Now go to extend page, in module listing page you can see configuration manager, click on configure.

So in synchronize page you can see below export and import option

In export tab select single item.

Select configuration type as content type and configuration name select Job application.

So you can see below yml code generated.

uuid: 7278cb1f-3c5f-40bb-9828-0a60fc878bad
langcode: en
status: true
dependencies:
  module:
    - menu_ui
third_party_settings:
  menu_ui:
    available_menus:
      - main
    parent: 'main:'
name: 'Job application'
type: job_application
description: ''
help: ''
new_revision: true
preview_mode: 1
display_submitted: true

We are using this yml in our module as mentioned in page Filename: node.type.job_application.yml

Remove uuid from above and create a file node.type.job_application.yml in custom module path dn_careerdemo\config\install

Here dn_careerdemo is the sample module that we have.

Here in order to create new content type we are changing all job_application to job_applications, so file created will be node.type.job_applications.yml

Also content of file will be as below.

langcode: en
status: true
dependencies:
  module:
    - menu_ui
    - dn_careerdemo
  enforced:
    module:
      - dn_careerdemo
third_party_settings:
  menu_ui:
    available_menus:
      - main
    parent: 'main:'
name: 'Job applications'
type: job_applications
description: ''
help: ''
new_revision: true
preview_mode: 1
display_submitted: true

We have included below dependency to our own module in order to remove content type and its fields during uninstall of this module.

Same way for each field, select respective fields as below. Here we are exporting application status filed.

So create file field.field.node.job_applications.field_application_status.yml in path /dn_careerdemo\config\install

Copy below content in file and update content type to job_applications.

langcode: en
status: true
dependencies:
  config:
    - field.storage.node.field_application_status
    - node.type.job_applications
  module:
    - options
    - tmgmt_content
third_party_settings:
  tmgmt_content:
    excluded: false
id: node.job_applications.field_application_status
field_name: field_application_status
entity_type: node
bundle: job_applications
label: 'Application status'
description: ''
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings: {  }
field_type: list_string

Same way create yml file for all fields and place in path.

\config\install\

If you have a view of job_applications , same way you can export view. Here I have exported below view,

 

langcode: en
status: true
dependencies:
  config:
    - node.type.job_applications
  module:
    - node
    - user
id: job_applications
label: 'Job Applications'
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
  default:
    display_plugin: default
    id: default
    display_title: Master
    position: 0
    display_options:
      access:
        type: perm
        options:
          perm: 'access content'
      cache:
        type: tag
        options: {  }
      query:
        type: views_query
        options:
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_comment: ''
          query_tags: {  }
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      pager:
        type: some
        options:
          items_per_page: 5
          offset: 0
      style:
        type: default
      row:
        type: fields
      fields:
        title:
          id: title
          table: node_field_data
          field: title
          settings:
            link_to_entity: true
          plugin_id: field
          relationship: none
          group_type: group
          admin_label: ''
          label: ''
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
      filters:
        status:
          value: '1'
          table: node_field_data
          field: status
          plugin_id: boolean
          entity_type: node
          entity_field: status
          id: status
          expose:
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
          group: 1
        type:
          id: type
          table: node_field_data
          field: type
          value:
            job_application: job_applications
          entity_type: node
          entity_field: type
          plugin_id: bundle
          expose:
            operator_limit_selection: false
            operator_list: {  }
      sorts:
        created:
          id: created
          table: node_field_data
          field: created
          order: DESC
          entity_type: node
          entity_field: created
          plugin_id: date
          relationship: none
          group_type: group
          admin_label: ''
          exposed: false
          expose:
            label: ''
          granularity: second
      title: 'Job Applications'
      header: {  }
      footer: {  }
      empty: {  }
      relationships: {  }
      arguments:
        uid:
          id: uid
          table: node_field_data
          field: uid
          relationship: none
          group_type: group
          admin_label: ''
          default_action: ignore
          exception:
            value: all
            title_enable: false
            title: All
          title_enable: false
          title: ''
          default_argument_type: fixed
          default_argument_options:
            argument: ''
          default_argument_skip_url: false
          summary_options:
            base_path: ''
            count: true
            items_per_page: 25
            override: false
          summary:
            sort_order: asc
            number_of_records: 0
            format: default_summary
          specify_validation: false
          validate:
            type: none
            fail: 'not found'
          validate_options: {  }
          break_phrase: false
          not: false
          entity_type: node
          entity_field: uid
          plugin_id: numeric
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - 'user.node_grants:view'
        - user.permissions
      tags: {  }
  block_1:
    display_plugin: block
    id: block_1
    display_title: Block
    position: 1
    display_options:
      display_extenders:
        metatag_display_extender: {  }
      block_description: 'Job Block'
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - 'user.node_grants:view'
        - user.permissions
      tags: {  }

Place above in in file \config\install\views.view.job_applications

Now you can place your custom module in any Drupal 9 instance, upon installation of the module , job applications content type will be created with all fields and also job application view also will be created with all its configuration.

During unintsall of the same module, all these content type and field will be removed.

Download sample module here.

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...