How to add custom fields and display settings to built-in content types via custom module install? I'm pretty new to Drupal, I come from Wordpress development and I'm trying to "translate" some of my plugins to modules in Drupal 8.

I'm following the documentation trying to add a new field, say "price" to a node.
All the documentation I found is related to add a field to a custom node, but in my case I want to add those fields to normal predefined nodes.

First question that comes to my mind is, I can create the custom field with YML configuration files or code right?

I created the field with YML and it is recognised by drupal; the problem is that they don't appear on the nodes (I have to manually activate it).

I created on my module /config/install/:


core.entity_form_display.node.article.default
core.entity_view_display.node.article.default


I add my field, but of course when I try to activate the module it does not like it since they are already defined by Drupal.

Is there a way to "extend" those core files? is that possible with YML or code? 

I followed https://www.drupal.org/docs/8/api/entity-api/programming-custom-fields-into-your-content-type. I created the YML files on /config/install, and indeed the field is created, the problem is I don't know how to "extend" view and form display entities as they are not related to a custom node type, otherwise they should be linked to pre-built node types and I cannot redefine those files (core.entity_form_display.node.article.default core.entity_view_display.node.article.default) since they already exist on the core.

Following this comment I've created the following files:

/config/install/field.field.node.article.field_price.yml

langcode: en
status: true
dependencies:
  config:
    - field.storage.node.field_price
    - node.type.article
id: node.article.field_price
field_name: field_price
entity_type: node
bundle: article
label: Price
description: 'Premium Price'
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings:
  min: 1
  max: 9999
  prefix: ''
  suffix: ''
field_type: integer


/config/install/field.storage.node.field_price.yml

langcode: en
status: true
dependencies:
  module:
    - node
id: node.field_price
field_name: field_price
entity_type: node
type: integer
settings:
  unsigned: false
  size: normal
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false


Also I created FieldFormatter and FieldWidget. (I am not sure if properly.)

What I found out is the module is creating the field Price.
If I go to /admin/structure/types/manage/article/display, I see my price there, but it is under Disabled.


  


I feel I'm so close, but at the same time so lost.

What am I missing in order to make that field go active?
For a beginner I'ld recommend to use Drupal's convenient user interface in the back-end. Create an admin user, log in, start adding fields, change display, reorder – all done by clicking through the interface.

For example you can add custom fields to the existing Article content type at https://example.com/admin/structure/types/manage/article/fields. There's is a button Add fields. Click it and explore. There is no difference in adding fields to a built-in content type.

As others said before YML are for configuration export and import, when deploying a site. As you would do as an advanced developer: You'ld have that page locally on your computer, add some fields by clicking through the back-end, export their config, push that to a Git repo, and then on your live page, pull from your repository, import the config files and get the same fields there without the need to click anything again.



Adding fields from a custom module can be achieved by providing certain YML in your modules /config/install folder. There as well is no difference between built-in content types or custom content types. It is described pretty good on DO at https://www.drupal.org/docs/8/api/entity-api/programming-custom-fields-into-your-content-type. Except that you would have to change the naming and especially change the value of the bundle: car_brand (content type) you want your fields to get added to.

Apart from that, you may need to install and uninstall this module pretty often during development. Get used to drush commands, for example drush dre module_name to do that in Terminal/Console (you need to have installed the Devel module to use drush dre). For your own good, I hope you already work with a UNIX based OS (Linux or OSX). Maybe you always have a fresh database backup available you can re-import with drush sql-cli &lt; database.sql when you want to start over during development.



Just for reference I paste the deleted answer code here OP originally had put in his question. To finally make the fields available in the content type OP came up with this. I strongly guess he had put this into the module's *.install file calling hook_install() or hook_update_N(). Possibly deprecated functions, but I think you'll get the idea.

$entity_type         = 'node';
$bundle              = 'article';
$field['field_name'] = 'field_price'; // your field id

entity_get_form_display($entity_type, $bundle, 'default')
  -&gt;setComponent($field['field_name'], array())
  -&gt;save();

entity_get_display($entity_type, $bundle, 'default')
  -&gt;setComponent($field['field_name'], array())
  -&gt;save();