Change record status: 
Introduced in branch: 
8.0.x
Description: 

In Drupal 7, modules could implement hook_form_node_type_form_alter(), add form elements and they were automatically saved as variables with a $node_type->type suffix.

In Drupal 8, they can use ThirdPartySettingsInterface to store settings, but need to provide a #entity_builders callback to control which values are saved.

7.x

function menu_form_node_type_form_alter(&$form, $form_state) {
  $type = $form['#node_type'];
  $form['menu']['menu_options'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Available menus'),
    '#default_value' => variable_get('menu_options_' . $type->type, array('main-menu')),
    '#options' => $menu_options,
    '#description' => t('The menus available to place links in for this content type.'),
  );
}

// Then the setting can be used as:
$menu_options = variable_get('menu_options_' . $type->type, array('main-menu'));

8.x

function menu_ui_form_node_type_form_alter(&$form, FormStateInterface $form_state) {
  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = $form_state->getFormObject()->getEntity();
  $form['menu']['menu_options'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Available menus'),
    '#default_value' => $type->getThirdPartySetting('menu_ui', 'available_menus', array('main')),
    '#options' => $menu_options,
    '#description' => t('The menus available to place links in for this content type.'),
  );
 $form['#entity_builders'][] = 'menu_ui_form_node_type_form_builder';
}

function menu_ui_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
  $type->setThirdPartySetting('menu_ui', 'available_menus', array_values(array_filter($form_state->getValue('menu_options'))));
  $type->setThirdPartySetting('menu_ui', 'parent', $form_state->getValue('menu_parent'));
}

// Then the setting can be used as 
$menu_options = $type->getThirdPartySetting('menu_ui', 'available_menus', array('main'));

Additionally, modules need to provide the config schema for their settings:

node_type.third_party.menu_ui:
  type: mapping
  label: 'Per-content type menu settings'
  mapping:
    available_menus:
      type: sequence
      label: 'Available menus'
      sequence:
       - type: string
         label: 'Menu machine name'
    parent:
      type: string
      label: 'Parent'
Impacts: 
Site builders, administrators, editors
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

pfrenssen’s picture

For the generic mechanism that allow to use third party settings for configuration entities, see New ThirdPartySettingsInterface and ThirdPartySettingsTrait for configuration entities to store third party settings.

balagan’s picture

In the D7 example the $menu_options variable is undefined in the function. (I just realized, that might be intentional...) -> Go to sleep now...