function MenuUiHooks::formNodeTypeFormValidate

Same name and namespace in other branches
  1. 11.x core/modules/menu_ui/src/Hook/MenuUiHooks.php \Drupal\menu_ui\Hook\MenuUiHooks::formNodeTypeFormValidate()

Validate handler for forms with menu options.

See also

\Drupal\menu_ui\Hook\MenuUiHooks::formNodeTypeFormAlter()

File

core/modules/menu_ui/src/Hook/MenuUiHooks.php, line 323

Class

MenuUiHooks
Hook implementations for menu_ui.

Namespace

Drupal\menu_ui\Hook

Code

public function formNodeTypeFormValidate(array &$form, FormStateInterface $form_state) : void {
  $available_menus = array_filter($form_state->getValue('menu_options'));
  // If there is at least one menu allowed, the selected item should be in
  // one of them.
  if (count($available_menus)) {
    $menu_item_id_parts = explode(':', $form_state->getValue('menu_parent'));
    if (!in_array($menu_item_id_parts[0], $available_menus)) {
      $form_state->setErrorByName('menu_parent', $this->t('The selected menu link is not under one of the selected menus.'));
    }
  }
  else {
    $form_state->setValue('menu_parent', '');
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.