1

I make a module in Drupal 8 who create a new content type programmatically. Module name : Annonce

In config/install I add this file:

node.type.annonce_type.yml

type: annonce_type
name: 'Ajouter une annonce'
description: 'Formulaire pour ajouter une annonce'
help: ''
new_revision: false
display_submitted: true
preview_mode: 0
status: true
langcode: fr

All is good, my new content type is create in backoffice. But now I want to add some fields programmatically, and I don't find a core module who are doing that.

1
  • Please don't downvote without adding a comment as to why!
    – Kevin
    Jul 29, 2015 at 11:54

1 Answer 1

2

I answered pretty much the same once already I think, but I can't find that answer anymore.

You add fields and their widget and formatter configuration in the same way. By adding all the relevant config entities.

You can find almost all of them by exporting configuration into the staging folder and then search for files that contain "announce_type" in the file name. That gives you the fields, the form and view displays configurations.

Then you also need the field storages. These are shared across bundles, so they don't have the node type in them (unless you prefixes your fields with your node type/module, which is a good idea if you want to make them part of your module).

Put all those fields in config/install and you're done.

2
  • Ok that work but i have 3 files yml for one field... core.entity_form / core.entity_view and field.field.node
    – Kevin
    Jul 10, 2015 at 8:20
  • 1
    Can you plz provide a sample code for field creation..
    – Abin
    Sep 22, 2015 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.