0

I altered node_type_edit_form and added a section to get some settings like below:

Node type form

Now I need to save the configurations and build the default value based on last selection using Configuration API .

The problem is it needs to build the fields selected (checkbox) in every content type form.

  • Do you want the settings for each content type or for all the content types? – Sumit Madan Jun 16 '15 at 4:45
  • I need unique settings for each content type that admin has selected. – Pravin Ajaaz Jun 16 '15 at 7:20
1

Instead of having your own configuration files, you can also use the third party settings concept to add your configuration directly to the node type config entity.

There are multiple advantages in doing that, you don't need to worry about cleaning up when node types are deleted, it's easier to deploy/export node types with their information and it's also easy to access/write when you have a node type. See the relevant change records: https://www.drupal.org/node/2326151 and https://www.drupal.org/node/2419827.

menu_ui.module has a great example for this in core, look for the functions menu_ui_form_node_type_form_alter(), menu_ui_form_node_type_form_builder() (or as an alternative of the builder, define your form structure so that it's saved in the right third party settings key automatically) and the config schema in menu_ui.schema.yml.

  • This is more than what I expected!!. But I cannot exactly understand the use of menu_ui.schema.yml. Can you please add some details about this in your answer berdir. It would be so helpful. – Pravin Ajaaz Jun 17 '15 at 6:33
  • All configuration needs to be defined by a schema, so that the types are known and can be enforced, translations are possible and so on. Just take that as an example and adjust it for your module name, keys and so on. You just need the node.type.*.third_party.menu_ui part, where menu_ui is our module name and then the keys below is what you save in there. – Berdir Jun 17 '15 at 9:48
1

I believe that you have machine name of content type while altering node_type_edit_form. So I assume that $node_type is the variable in which machine name can be accessible.

So as stated in drupal list changes, variable_get and variable_set is replaced by config system in drupal 8.

To save a configuration you need to add this code in your submit function :

\Drupal::configFactory()->getEditable('modulename.settings')
    ->set('checkboxsetting_' . $node_type, $form_state->getValues()['checkboxsettings'])
    ->set('radiosettings' . $node_type, $form_state->getValues()['radiosettings'])
    ->save();

and to get the saved values you need to :

\Drupal::config('modulename.settings')->get('checkboxsetting_' . $node_type);

To set the default configuration from yml file. Create a yml file and content will be :

modulename/config/install/modulename.settings.yml

checkboxsetting_page: whatever
checkboxsetting_article: whatever
radio_page: whatever
radio_article: whatever
  • Thanks sumit but what I need to know is how the YML(schema) for this type of configuration is built. – Pravin Ajaaz Jun 16 '15 at 7:36
  • Means?? Do you need to create a yml too for default configuration? – Sumit Madan Jun 16 '15 at 7:49
  • Sorry for not being clear on this. I need to know how to build the YML schema to store these values. Are YML schemas required only to build default values?? – Pravin Ajaaz Jun 16 '15 at 8:40
  • Updated my answer. – Sumit Madan Jun 16 '15 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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