public function UniqueEntityTitleValidator::validate in Unique entity title 8
File
- src/
Plugin/ Validation/ Constraint/ UniqueEntityTitleValidator.php, line 16
Class
- UniqueEntityTitleValidator
- Validates the UniqueEntityTitle constraint.
Namespace
Drupal\unique_entity_title\Plugin\Validation\ConstraintCode
public function validate($item, Constraint $constraint) {
$value = $item
->getValue()[0]['value'];
$entity = $item
->getEntity();
$entity_type_id = $entity
->getEntityTypeId();
switch ($entity_type_id) {
case 'node':
$entity_bundle = $entity
->getType();
$unique_entity_title_enabled = !empty($entity->type->entity
->getThirdPartySetting('unique_entity_title', 'enabled')) ? TRUE : FALSE;
$unique_entity_title_label = \Drupal::config('core.base_field_override.node.' . $entity_bundle . '.title')
->get('label') ?: 'Title';
$unique_field = 'title';
$bundle_field = 'type';
$id_field = 'nid';
break;
case 'taxonomy_term':
$entity_bundle = $entity
->bundle();
$unique_entity_title_enabled = !empty(\Drupal::config('unique_entity_title.settings')
->get($entity_bundle . '_taxonomy_unique')) ? TRUE : FALSE;
$unique_entity_title_label = 'Name';
$unique_field = 'name';
$bundle_field = 'vid';
$id_field = 'tid';
break;
default:
break;
}
if ($unique_entity_title_enabled && $this
->isNotUnique($unique_field, $value, $entity_type_id, $bundle_field, $entity_bundle, $id_field)) {
$this->context
->addViolation($constraint->notUnique, [
'%label' => $unique_entity_title_label,
'%value' => $value,
]);
}
}