// Validate field cardinality.
if ($form_state->
getValue('cardinality'
) === 'number' && !
$form_state->
getValue('cardinality_number'
)) { $form_state->
setError($element['cardinality_number'
],
$this->
t('Number of values is required.'
));
} // If a specific cardinality is used, validate that there are no entities
// with a higher delta.
elseif (!
$this->entity->
isNew() &&
isset($field_storage_definitions[$this->entity->
getName()]) &&
$form_state->
getValue('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',
$form_state->
getValue('cardinality'
)) ->
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' =>
$form_state->
getValue('cardinality'
) + 1, '@allowed' =>
$form_state->
getValue('cardinality'
)]));
} } } /**
* {@inheritdoc}
*/