1

I have a development site (let be site 1) and a site where I check my code (site 2). My goal was to add a new existing field to a paragraphs type (/admin/structure/paragraphs_type), so I did the following:

On site 1:

  • I re-used an existing field in one of the paragraphs type
  • I exported the configuration
  • I deleted uuid and _core.default_config_hash from the configuration files (from those which was relevant for me)
  • I wrote an update hook which will create the same config file (except order), when my module will be updated on other sites, where it was previously installed my module
  • I put the exported 3 files to my module's config/optional dir

Than I moved this module to the site 2 where I run the update hooks. But the newly added field wasn't showed up on the paragraphs type's page under the correct paragraphs type. All the configuration's dependencies are right and enabled. Configuration file is same except the order. If I want to add the field on site 2 manually again, after config import I get this error: There was a problem creating field : 'field_config' entity with ID 'paragraph.text_boxes.field_text' already exists.

Note: before I added the new field, I had a configuration export under git, so after I added a new field and exported the configuration I saw exactly which configs was touched.

What am I doing wrong? How can I add a new existing field to a paragraphs (or a content type)? What's missing?

2 Answers 2

0

if I am not wrong, I believe, Site 1 is your local development environment, and site 2 is your production environment.

I wrote an update hook which will create the same config file (except order), when my module will be updated on other sites, where it was previously installed my module Why would you write an update hook ? You can simply export the configs, and import them on prod. If this does not suit you, I would suggest you to use Features module for this. It will make sure you get a working module which can be installed even without features module in place on Prod.

2
  • I need update hook because the config files are on the module's dir's config/optional folder (these were exported from the site), so after I update the module (or somebody else from drupal org) they don't have the new field Jun 14, 2017 at 8:28
  • In other words, if somebody has my module installed and I push an update with a new field, they don't get it after they update my module. They only get the new field when they reinstall my module, which means they loose all the content. Which is not good at any way. Jun 14, 2017 at 8:34
0

Well finally I found a working solution for me, however I don't know why doesn't show up by default after config save.

First I need to connect my existing field (which I want to re-use) to the selected content type/paragraphs type:

\Drupal\field\Entity\FieldConfig::create([
  'entity_type' => 'node',
  'bundle'      => 'page',
  'field_name'  => 'field_text',
])->save();

This code will add a new field which machine name is field_text (needs to be exist in the system) into page content type which is a node.

Than if I edit the configuration (with configFactory) I can set the field name, view and form settings, etc. However you are able to set field settings with FieldConfig. (but not display and form)

1

Your Answer

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

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