function MenuUiHooks::formNodeFormSubmit

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

Form submission handler for menu item field on the node form.

See also

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

File

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

Class

MenuUiHooks
Hook implementations for menu_ui.

Namespace

Drupal\menu_ui\Hook

Code

public function formNodeFormSubmit(array $form, FormStateInterface $form_state) : void {
  $node = $form_state->getFormObject()
    ->getEntity();
  if (!$form_state->isValueEmpty('menu')) {
    $values = $form_state->getValue('menu');
    if (empty($values['enabled'])) {
      if ($values['entity_id']) {
        $entity = $this->entityRepository
          ->getActive('menu_link_content', $values['entity_id']);
        $entity->delete();
      }
    }
    else {
      // In case the menu title was left empty, fall back to the node title.
      if (empty(trim($values['title']))) {
        $values['title'] = $node->label();
      }
      // Decompose the selected menu parent option into 'menu_name' and
      // 'parent', if the form used the default parent selection widget.
      if (!empty($values['menu_parent'])) {
        [$menu_name, $parent] = explode(':', $values['menu_parent'], 2);
        $values['menu_name'] = $menu_name;
        $values['parent'] = $parent;
      }
      ($this->menuUiUtility)()
        ->menuUiNodeSave($node, $values);
    }
  }
}

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