function menu_ui_form_node_type_form_alter

menu_ui_form_node_type_form_alter(&$form, FormStateInterface $form_state)

Implements hook_form_FORM_ID_alter() for \Drupal\node\NodeTypeForm.

Adds menu options to the node type form.

See also

NodeTypeForm::form()

menu_ui_form_node_type_form_submit()

File

core/modules/menu_ui/menu_ui.module, line 395
Allows administrators to customize the site's navigation menus.

Code

function menu_ui_form_node_type_form_alter(&$form, FormStateInterface $form_state) {
  /** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */
  $menu_parent_selector = \Drupal::service('menu.parent_form_selector');
  $menu_options = menu_ui_get_menus();
  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = $form_state->getFormObject()->getEntity();
  $form['menu'] = array(
    '#type' => 'details',
    '#title' => t('Menu settings'),
    '#attached' => array(
      'library' => array('menu_ui/drupal.menu_ui.admin'),
    ),
    '#group' => 'additional_settings',
  );
  $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.'),
  );
  // @todo See if we can avoid pre-loading all options by changing the form or
  //   using a #process callback. https://www.drupal.org/node/2310319
  //   To avoid an 'illegal option' error after saving the form we have to load
  //   all available menu parents. Otherwise, it is not possible to dynamically
  //   add options to the list using ajax.
  $options_cacheability = new CacheableMetadata();
  $options = $menu_parent_selector->getParentSelectOptions('', NULL, $options_cacheability);
  $form['menu']['menu_parent'] = array(
    '#type' => 'select',
    '#title' => t('Default parent item'),
    '#default_value' => $type->getThirdPartySetting('menu_ui', 'parent', 'main:'),
    '#options' => $options,
    '#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'),
    '#attributes' => array('class' => array('menu-title-select')),
  );
  $options_cacheability->applyTo($form['menu']['menu_parent']);

  $form['#validate'][] = 'menu_ui_form_node_type_form_validate';
  $form['#entity_builders'][] = 'menu_ui_form_node_type_form_builder';
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!menu_ui!menu_ui.module/function/menu_ui_form_node_type_form_alter/8.1.x