class MenuSettingsConstraintValidator

Same name in other branches
  1. 9 core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php \Drupal\menu_ui\Plugin\Validation\Constraint\MenuSettingsConstraintValidator
  2. 8.9.x core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php \Drupal\menu_ui\Plugin\Validation\Constraint\MenuSettingsConstraintValidator
  3. 10 core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php \Drupal\menu_ui\Plugin\Validation\Constraint\MenuSettingsConstraintValidator

Constraint validator for changing the menu settings in pending revisions.

Hierarchy

Expanded class hierarchy of MenuSettingsConstraintValidator

File

core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php, line 11

Namespace

Drupal\menu_ui\Plugin\Validation\Constraint
View source
class MenuSettingsConstraintValidator extends ConstraintValidator {
    
    /**
     * {@inheritdoc}
     */
    public function validate($entity, Constraint $constraint) : void {
        if (isset($entity) && !$entity->isNew() && !$entity->isDefaultRevision()) {
            $defaults = menu_ui_get_menu_link_defaults($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();
            }
            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();
                }
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
MenuSettingsConstraintValidator::validate public function

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