Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Alts on
Hello,
I am french so i ll try to explain my problem with simple word.
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 ll add some fields programmatically, and i don't find a core module who are doing that.
if someone can tell me the way for add fields for this new content type i ll be happy :)
Thanks
Best Regards
Kevin
Comments
This covers doing it
This covers doing it programmatically as opposed to doing it with yml.
Take a look at the node module and how it attaches the body fiel
I had to do something similar today, programmatically add an existing field to multiple content types. The node module's node_add_body_field() has a good example of this [starts on line 322 of node.module]:
Hi, below is yaml approach.
Hi, below is yaml approach. To add a custom field, you'll need to create two custom yml files inside the config/install directory. Below is an example of adding a field called Last Name for a content type called "Person":
Yaml File #1: field.field.node.person.last_name.yml
Yaml File #2: field.storage.node.last_name.yml
Also, make sure to add this field in your existing entity_view_display and entity_form_display files. See https://www.drupal.org/node/2629550 for more details.
Once you add these files, go ahead and install the module as you would for any other module. I hope this helps!
Thanks
This worked perfectly and was simple to understand.
For a Third Party Content Type
Thanks for the solution. Just wanted to add that if you want to add a field to a third party entity you won't be able to edit the entity_view_display and entity_form_display yml files, but you can load and edit that configuration in a hook_install assuming the entity type is fieldable.
Its working fine,
Its working fine,
can we have the same for taxonomy too
also please explain where do you find this techniques
Drupal 8 Taxonomy
In Drupal 8 Taxonomy Terms are Entities just like Nodes. So in the code above just replace "node" with "taxonomy_term", as both of those are "Entity Types". If you want to read up on it I would recommend the entity api documentation, but it's a lot of reading.
What if the module is already
What if the module is already installed. How do you add the fields to an existing content type without re-installing the module? Can you run update.php or something?
drush entity-update
drush entity-update
I added my new field yml
I added my new field yml files to config/install and ran drush entity-update. However, drush reported "No entity schema updates required". Do I need to do something else to make drupal recognize the addition to config/install?
After about 2 weeks of
After about 2 weeks of searching I found the command I was looking for:
Hello everyone. This is soo
Hello everyone. This is soo very close to what I need. I hope any of you can help:
Please tell me how you would go about setting up an entity reference field programmatically. I'm just now starting on this migration from D7 to D8 and, while learning a lot, I'm learning a lot about what's missing as well.
Thank you for any assistance.
Much the same
Adding an entity reference field is much the same, the difference is just in the field and field definition settings. You want the type to be 'entityreference'. See https://www.drupal.org/project/entityreference/issues/1264160
Display Setting for Vocabulary Entity Reference.
Hi.. .So I managed to get my reference fields (nodereference) migrated without coding.
Using a backup of the D7 database, https://www.drupal.org/project/entityreference_migration installation and following README instructions worked like a charm.
Unfortunately, the Taxonomy Entity reference "Form Display" settings didn't transfer properly. So I need to figure that part out next. THanks for your suggestion.
I'm not sure how to do it yet - still getting my D8 coding chops together... but I wanna submit a patch to make that happen properly.
Try this module
You can create fields programmatically with this module from custom YAML or JSON files:
https://drupal.org/project/field_create
You can use the provided hook to add your fields to the list:
function mymodule_field_create_alter(&$definitions) {}
And simply run the drush command:
drush field_create