Commit b4f1849a authored by alexpott's avatar alexpott

Revert "Issue #2403847 by larowlan, dawehner: Shortcut content entity...

Revert "Issue #2403847 by larowlan, dawehner: Shortcut content entity validation misses form validation logic"

This reverts commit 4a3abdca.
parent 80059557
......@@ -228,7 +228,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Path'))
->setDescription(t('The computed shortcut path.'))
->setComputed(TRUE)
->addConstraint('ShortcutPath', [])
->setCustomStorage(TRUE);
$item_definition = $fields['path']->getItemDefinition();
......
<?php
/**
* @file
* Contains \Drupal\shortcut\Plugin\Validation\Constraint\ShortcutPathConstraint.
*/
namespace Drupal\shortcut\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Validates the path of a shortcut entry
*
* @Plugin(
* id = "ShortcutPath",
* label = @Translation("Shortcut path", context = "Validation"),
* type = { "entity" }
* )
*/
class ShortcutPathConstraint extends Constraint {
/**
* The default violation message.
*
* @var string
*/
public $message = 'The shortcut must correspond to a valid path on the site.';
}
<?php
/**
* @file
* Contains \Drupal\shortcut\Plugin\Validation\Constraint\ShortcutPathConstraintValidator.
*/
namespace Drupal\shortcut\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the shortcut path.
*/
class ShortcutPathConstraintValidator extends ConstraintValidator {
/**
* The path validator.
*
* @var \Drupal\Core\Path\PathValidatorInterface
*/
protected $pathValidator;
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
// We cannot use $items here because this is a computed field, we need to
// fetch the value direct from TypedData.
$list = $this->context->getMetadata()->getTypedData();
if (empty($list)) {
return;
}
$value = $list->value;
if (!$this->pathValidator()->isValid($value)) {
$this->context->addViolation($constraint->message);
}
}
/**
* Gets the path validator.
*
* @return \Drupal\Core\Path\PathValidatorInterface
*/
protected function pathValidator() {
if (!isset($this->pathValidator)) {
$this->pathValidator = \Drupal::pathValidator();
}
return $this->pathValidator;
}
}
......@@ -25,6 +25,34 @@ class ShortcutForm extends ContentEntityForm {
*/
protected $entity;
/**
* The path validator.
*
* @var \Drupal\Core\Path\PathValidatorInterface
*/
protected $pathValidator;
/**
* Constructs a new ShortcutForm instance.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager
* @param \Drupal\Core\Path\PathValidatorInterface $path_validator
* The path validator.
*/
public function __construct(EntityManagerInterface $entity_manager, PathValidatorInterface $path_validator) {
parent::__construct($entity_manager);
$this->pathValidator = $path_validator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity.manager'), $container->get('path.validator'));
}
/**
* {@inheritdoc}
*/
......@@ -60,14 +88,11 @@ public function buildEntity(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
$this->entity->path->value = $form_state->getValue('path');
/** @var \Symfony\Component\Validator\ConstraintViolationListInterface $result */
$result = $this->entity->path->validate();
if ($result->count()) {
$form_state->setErrorByName('path', $result->offsetGet(0)->getMessage());
if (!$this->pathValidator->isValid($form_state->getValue('path'))) {
$form_state->setErrorByName('path', $this->t('The shortcut must correspond to a valid path on the site.'));
}
parent::validate($form, $form_state);
}
/**
......
<?php
/**
* @file
* Contains \Drupal\shortcut\Tests\ShortcutValidationTest.
*/
namespace Drupal\shortcut\Tests;
use Drupal\shortcut\Entity\Shortcut;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
/**
* Tests shortcut validation constraints.
*
* @group shortcut
*/
class ShortcutValidationTest extends EntityUnitTestBase {
/**
* The logged in user.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* Modules to install.
*
* @var array
*/
public static $modules = array('shortcut', 'user', 'system');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['url_alias', 'router']);
// Create and login a user.
$role = Role::create(['id' => 'role_with_access']);
$role->grantPermission('access administration pages');
$role->save();
$this->adminUser = User::create(['roles' => ['role_with_access']]);
$this->adminUser->save();
\Drupal::currentUser()->setAccount($this->adminUser);
\Drupal::service('router.builder')->rebuild();
}
/**
* Tests the shortcut validation constraints.
*/
public function testValidation() {
// Add shortcut.
$shortcut = Shortcut::create(array(
'shortcut_set' => 'default',
'title' => t('Add content'),
'weight' => -20,
'path' => '<front>',
));
$violations = $shortcut->path->validate();
$this->assertEqual(count($violations), 0);
$paths = [
'does/not/exist' => 1,
'user/password' => 0,
'admin' => 0,
'user/' . $this->adminUser->id() => 0,
'http://example.com/' => 0,
];
foreach ($paths as $path => $violation_count) {
$shortcut->set('path', $path);
$violations = $shortcut->path->validate();
$this->assertEqual(count($violations), $violation_count);
if ($violation_count) {
$this->assertEqual($violations[0]->getMessage(), t('The shortcut must correspond to a valid path on the site.'));
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment