0

I have a module that requires some settings per content type. The module is to also provide one admin page where all the settings can be applied across content types at once.

Essentially I'm trying to share configuration information between a custom Admin Settings form and the node type edit form. I had this all working with Drupal 8 Beta 10 or something like that, but I'm trying to get it working with the latest stable release (8.0.4).

I originally used the method described here for my custom Admin form using the following code:

$config = $this->config('mymodule.settings');
'#default_value' => $config->get('my_setting'),

Similarly, for the node edit form alter function, I used:

$config =  \Drupal::config('mymodule.settings'); 
'#default_value' => $config->get('my_setting'),

But the above stopped working for the node type settings form at some point, so I started looking at using Third Party Settings:

$node_type = $form_state->getFormObject()->getEntity();
'#default_value' => $node_type->getThirdPartySetting('mymodule', 'setting', 'default_value'),

This works great for the node type edit form, but I don't know of a way to pull the third-party settings into my custom Admin form partially because getEntity() method isn't available.

So my question is, what is the best practice for sharing configuration settings between a custom admin form and the content type edit form?

1 Answer 1

0

So the bottom line appears to be that Third Party Settings is a great way to handle content type settings without really caring which content type you're dealing with, but there doesn't seem to be a way to get this working with a custom admin settings form for the reason described in the question.

If I didn't want to have a separate configuration form that handled the content type settings for all content types on one form, I would definitely have used Third Party Settings. However, since I had this extra requirement, I wound up fixing whatever broke along the journey from D8 beta to stable release.

Now the node type gets included with each of the desired settings using the following code:

function mymodule_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
  $node_type = $type->get('type');
  $config = \Drupal::getContainer()->get('config.factory')->getEditable('mymodule.settings');
  $config->set('my_setting_'.$node_type, $form_state->getValue('my_setting'));
  $config->save();
}

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.