backdrop-contrib / examples

Examples for Developers
GNU General Public License v2.0
7 stars 9 forks source link

Node Example #11

Open stpaultim opened 4 years ago

stpaultim commented 4 years ago

I am just experimenting with creating a content type via a custom module, using config files. I notice that the example module here does not do anything with config at all.

Is this example module just out of date and using Drupal methods?

Should this example module be updated to create the content type and fields with config files (similar to what I did here: https://forum.backdropcms.org/comment/1541#comment-1541?

avpaderno commented 1 year ago

Yes, it seems the module is just a port of the equivalent Drupal 7 example module. It should be updated to use config files.

@stpaultim Does the code shown on forum.backdropcms.org/comment/1541#comment-1541 require to be updated, or are there any necessary changes to make it work with latest Backdrop releases?

stpaultim commented 1 year ago

@kiamlaluno - I don't write a lot of code, so I'm not the best person to answer this question.

It seems that I originally thought I could simply add configuration files to a module to create a content type. I learned later, that more is necessary.

The code in that comment works, because I'm just adding an existing field to a new content type. BUT, this is not sufficient to create a new field, which is probably a better example.

argiepiano commented 1 year ago

I think in the case of content types, it may be possible to create one by just copying the json file (or importing the configuration), but I haven't tested this thoroughly.

In the case of fields (which that 2019 post referred to), docwilmot's comment (if I understand it correctly) no longer applies. Importing config files for field info and field instance DOES create the required tables in the database. You can actually test it this way:

  1. Add a text field called my_test to Page content type
  2. Go to files/confix_XXX/ and copy both field.field.field_my_test.json and field.instance.field_my_test.json to another location temporarily
  3. Remove the field from Page. Verify that the two json files do not exist in the config directory anymore, and that the table field_data_field_my_test has also been removed from the database
  4. Head to admin/config/development/configuration/single and import the json code for both, the field and the field instance you copied above
  5. Check the config directory and the database. The field table and json files have been created

That said, I believe there are some contrib modules (OG comes to mind) where there is some additional processing when fields are added. So, the above may not apply to all situations.

avpaderno commented 1 year ago

Thank you for your reply, @stpaultim and @argiepiano.
I know this has been opened as support request. I was thinking to write the example module following a Backdrop approach, if it is possible. Probably, there are more people who needed to create fields when installing a module and looked at the example modules for the code to use. Showing the code that is better to use in Backdrop would help people with the same need.

avpaderno commented 1 year ago

Actually, node_type_example.module shows how a module can define a new content type. It uses the same type of code Backdrop core uses, though. I cannot find in Backdrop core examples of content type created only via configuration files. I take that means it is not currently possible.