function FieldStorageConfigEditForm::validateCardinality
Same name in other branches
- 9 core/modules/field_ui/src/Form/FieldStorageConfigEditForm.php \Drupal\field_ui\Form\FieldStorageConfigEditForm::validateCardinality()
- 8.9.x core/modules/field_ui/src/Form/FieldStorageConfigEditForm.php \Drupal\field_ui\Form\FieldStorageConfigEditForm::validateCardinality()
- 10 core/modules/field_ui/src/Form/FieldStorageConfigEditForm.php \Drupal\field_ui\Form\FieldStorageConfigEditForm::validateCardinality()
Validates the cardinality.
Parameters
array $element: The cardinality form render array.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
File
-
core/
modules/ field_ui/ src/ Form/ FieldStorageConfigEditForm.php, line 206
Class
- FieldStorageConfigEditForm
- Provides a form for the "field storage" edit page.
Namespace
Drupal\field_ui\FormCode
public function validateCardinality(array &$element, FormStateInterface $form_state) {
$field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($this->entity
->getTargetEntityTypeId());
$cardinality = $form_state->getValue([
$element['#parents'],
'cardinality',
]);
$cardinality_number = $form_state->getValue([
$element['#parents'],
'cardinality_number',
]);
// Validate field cardinality.
if ($cardinality === 'number' && !$cardinality_number) {
$form_state->setError($element['cardinality_number'], $this->t('Number of values is required.'));
}
elseif (!$this->entity
->isNew() && isset($field_storage_definitions[$this->entity
->getName()]) && $cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
// Get a count of entities that have a value in a delta higher than the
// one selected. Deltas start with 0, so the selected value does not
// need to be incremented.
$entities_with_higher_delta = \Drupal::entityQuery($this->entity
->getTargetEntityTypeId())
->accessCheck(FALSE)
->condition($this->entity
->getName() . '.%delta', $cardinality_number)
->count()
->execute();
if ($entities_with_higher_delta) {
$form_state->setError($element['cardinality_number'], $this->formatPlural($entities_with_higher_delta, 'There is @count entity with @delta or more values in this field, so the allowed number of values cannot be set to @allowed.', 'There are @count entities with @delta or more values in this field, so the allowed number of values cannot be set to @allowed.', [
'@delta' => $cardinality_number + 1,
'@allowed' => $cardinality_number,
]));
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.