Verified Commit 8cd4b02a authored by godotislate's avatar godotislate
Browse files

Revert "task: #3050199 Remove entity level constraint MenuSettingsConstraint...

Revert "task: #3050199 Remove entity level constraint MenuSettingsConstraint that validates property only added in a form"

This reverts commit 214876bb.
parent b6b707f4
Loading
Loading
Loading
Loading
Loading
+3 −46
Changes for core/modules/menu_ui/src/Hook/MenuUiHooks.php: 3 added lines, 46 removed lines.
Original line number Diff line number Diff line
@@ -86,6 +86,9 @@ public function help($route_name, RouteMatchInterface $route_match): ?string {
  public function entityTypeBuild(array &$entity_types): void {
    /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
    $entity_types['menu']->setFormClass('add', 'Drupal\menu_ui\MenuForm')->setFormClass('edit', 'Drupal\menu_ui\MenuForm')->setFormClass('delete', 'Drupal\menu_ui\Form\MenuDeleteForm')->setListBuilderClass('Drupal\menu_ui\MenuListBuilder')->setLinkTemplate('add-form', '/admin/structure/menu/add')->setLinkTemplate('delete-form', '/admin/structure/menu/manage/{menu}/delete')->setLinkTemplate('edit-form', '/admin/structure/menu/manage/{menu}')->setLinkTemplate('add-link-form', '/admin/structure/menu/manage/{menu}/add')->setLinkTemplate('collection', '/admin/structure/menu');
    if (isset($entity_types['node'])) {
      $entity_types['node']->addConstraint('MenuSettings', []);
    }
  }

  /**
@@ -227,7 +230,6 @@ public function formNodeFormAlter(&$form, FormStateInterface $form_state) : void
        $form['actions'][$action]['#submit'][] = static::class . ':formNodeFormSubmit';
      }
    }
    $form['#validate'][] = static::class . ':formNodeFormValidate';
    $form['#entity_builders'][] = static::class . ':nodeBuilder';
  }

@@ -238,51 +240,6 @@ public function nodeBuilder(string $entity_type, NodeInterface $entity, array &$
    $entity->menu = $form_state->getValue('menu');
  }

  /**
   * Form validation handler for menu item field on the node form.
   *
   * Prevents changes to menu settings in pending (non-default) revisions.
   *
   * @see \Drupal\menu_ui\Hook\MenuUiHooks::formNodeFormAlter()
   */
  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.'));
    }
    // Handle all the other non-revisionable menu link changes in a pending
    // revision.
    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.'));
      }
    }
  }

  /**
   * Form submission handler for menu item field on the node form.
   *
−6
Changes for core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraint.php: 0 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -8,12 +8,6 @@

/**
 * Validation constraint for changing the menu settings in pending revisions.
 *
 * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Menu
 *   settings validation is now handled as a form validation callback in
 *   \Drupal\menu_ui\Hook\MenuUiHooks::formNodeFormValidate().
 *
 * @see https://www.drupal.org/node/3606616
 */
#[Constraint(
  id: 'MenuSettings',
+46 −7
Changes for core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php: 46 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -10,12 +10,6 @@

/**
 * Constraint validator for changing the menu settings in pending revisions.
 *
 * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Menu
 *   settings validation is now handled as a form validation callback in
 *   \Drupal\menu_ui\Hook\MenuUiHooks::formNodeFormValidate().
 *
 * @see https://www.drupal.org/node/3606616
 */
class MenuSettingsConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

@@ -35,7 +29,52 @@ public function __construct(
   * {@inheritdoc}
   */
  public function validate($entity, Constraint $constraint): void {
    @trigger_error('MenuSettingsConstraintValidator is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Menu settings validation is now handled as a form validation callback in \Drupal\menu_ui\Hook\MenuUiHooks::formNodeFormValidate(). See https://www.drupal.org/node/3606616', E_USER_DEPRECATED);
    if (isset($entity) && !$entity->isNew() && !$entity->isDefaultRevision()) {
      $defaults = $this->menuUiUtility->getMenuLinkDefaults($entity);

      // If the menu UI entity builder is not present and the menu property has
      // not been set, do not attempt to validate the menu settings since they
      // are not being modified.
      if (!$values = $entity->menu) {
        return;
      }

      if (trim($values['title']) && !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']) {
        $this->context->buildViolation($constraint->messageRemove)
          ->atPath('menu')
          ->setInvalidValue($entity)
          ->addViolation();
      }
      // Handle all the other non-revisionable menu link changes in a pending
      // revision.
      elseif ($defaults['entity_id']) {
        if ($defaults['entity_id'] && ($values['menu_name'] != $defaults['menu_name'])) {
          $this->context->buildViolation($constraint->messageParent)
            ->atPath('menu.menu_parent')
            ->setInvalidValue($entity)
            ->addViolation();
        }
        elseif (isset($values['parent']) && ($values['parent'] != $defaults['parent'])) {
          $this->context->buildViolation($constraint->messageParent)
            ->atPath('menu.menu_parent')
            ->setInvalidValue($entity)
            ->addViolation();
        }
        elseif (($values['weight'] != $defaults['weight'])) {
          $this->context->buildViolation($constraint->messageWeight)
            ->atPath('menu.weight')
            ->setInvalidValue($entity)
            ->addViolation();
        }
      }
    }
  }

}