Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create new content type programmatically usingĀ Configuration API

By editor , 14 April, 2016
Question

How to create a new custom content type (CT) programmatically using Configuration API ?

Module Name = my_module
Content Type = my_new_content

Create content type

Put this two files into my_module/config/install/

# File 1 
# node.type.my_new_content.yml
langcode: en
status: true
dependencies:
  enforced:
    module:
      - my_module
name: 'My New Content'
type: my_new_content
description: 'Content type that can be used to provide <em>Test content</em>'
help: ''
new_revision: false
preview_mode: 1
display_submitted: true

 

Add existing field

# File 2
#field.field.node.my_new_content.body.yml
langcode: en
status: true
dependencies:
  config:
    - field.storage.node.body
    - node.type.my_new_content
  module:
    - text
id: node.my_new_content.body
field_name: body
entity_type: node
bundle: my_new_content
label: 'Message Body'
description: 'Content of the new CT'
required: false
translatable: true
default_value: {  }
default_value_callback: ''
settings:
  display_summary: true
field_type: text_with_summary


 

 

Create a field configuration file

Step 1.    Create the field from the user interface.
Step 2. Export to a file from /admin/config/development/configuration/single/export
        Field Storage (then Field) and Select your field (Here node.field_my_test_text)
        Copy name ftom the bottom of the textarea (Ex: field.storage.node.field_my_test_text.yml)
        Create a config file (Here field.storage.node.field_my_test_text.yml) on /config/install
        Copy the text except 'uudd' line
Step 3. Install or Update your module
        If update, follow update procedure of this tutorial.


Update a Field instance.

Step 1. Update yml file.
Step 2. Create an update in my_module.install , like:

function my_module_update_8001() {
  $config_path = drupal_get_path('module', 'my_module') . '/config/install/field.field.node.my_new_content.body.yml';
  $data = \Symfony\Component\Yaml\Yaml::parse($config_path);
  \Drupal::configFactory()
    ->getEditable('field.field.node.my_new_content.body')
    ->setData($data)
    ->save(TRUE);
  return 'The Field has been updated.';
}

Creating a custom content type in Drupal 8
Tuto : https://www.drupal.org/node/2629550

Creating a content entity type in Drupal 8
https://www.drupal.org/node/2192175

File
my_module.zip (2.97 KB)

Tags

  • Drupal 8
  • Code
  • Module
  • Add new comment

Comments1

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Lauren Rodriguez (not verified)

3 years 6 months ago

I have read your blog on How…

I have read your blog on How can you create custom content types in Drupal.It was very interesting and helpful but I can add some extra points in your article. Here some extra points:
1.Open example.theme
2.Add the code snippet below.
3.Replace example in the function name example_theme_suggestions_page_alter with your actual theme name.
4.Refresh the cache.
These are some extra points to add to your article. Readers if you are confused about your web and App development , you can get a free consultation at Alakmalak technologies.Visit our site for more information.

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer