function MenuUiHooks::formNodeFormValidate

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

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

Prevents changes to menu settings in pending (non-default) revisions.

See also

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

File

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

Class

MenuUiHooks
Hook implementations for menu_ui.

Namespace

Drupal\menu_ui\Hook

Code

public function formNodeFormValidate(array &$form, FormStateInterface $form_state) : void {
  $node = $form_state->getFormObject()
    ->getEntity();
  if ($node->isNew() || $node->isDefaultRevision()) {
    return;
  }
  $values = $form_state->getValue('menu');
  if (!$values) {
    return;
  }
  $defaults = ($this->menuUiUtility)()
    ->getMenuLinkDefaults($node);
  if (!empty($values['menu_parent'])) {
    [$menu_name, $parent] = explode(':', $values['menu_parent'], 2);
    $values['menu_name'] = $menu_name;
    $values['parent'] = $parent;
  }
  // Handle the case when the menu link is deleted in a pending revision.
  if (empty($values['enabled']) && $defaults['entity_id']) {
    $form_state->setErrorByName('menu][enabled', $this->t('You can only remove the menu link in the <em>published</em> version of this content.'));
  }
  elseif ($defaults['entity_id']) {
    if ($values['menu_name'] != $defaults['menu_name']) {
      $form_state->setErrorByName('menu][menu_parent', $this->t('You can only change the parent menu link for the <em>published</em> version of this content.'));
    }
    elseif (isset($values['parent']) && $values['parent'] != $defaults['parent']) {
      $form_state->setErrorByName('menu][menu_parent', $this->t('You can only change the parent menu link for the <em>published</em> version of this content.'));
    }
    elseif ($values['weight'] != $defaults['weight']) {
      $form_state->setErrorByName('menu][weight', $this->t('You can only change the menu link weight for the <em>published</em> version of this content.'));
    }
  }
}

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