function file_field_widget_multiple_count_validate

Validation callback for upload element on file widget. Checks if user has uploaded more files than allowed.

This validator is used only when cardinality not set to 1 or unlimited.

1 string reference to 'file_field_widget_multiple_count_validate'
FileWidget::formElement in drupal/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php
Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().

File

drupal/core/modules/file/file.field.inc, line 406
Field module functionality for the File module.

Code

function file_field_widget_multiple_count_validate($element, &$form_state, $form) {
  $parents = $element['#parents'];
  $values = NestedArray::getValue($form_state['values'], $parents);
  array_pop($parents);
  $current = count(element_children(NestedArray::getValue($form, $parents))) - 1;
  $field = field_info_field($element['#field_name']);
  $uploaded = count($values['fids']);
  $count = $uploaded + $current;
  if ($count > $field['cardinality']) {
    $keep = $uploaded - $count + $field['cardinality'];
    $removed_files = array_slice($values['fids'], $keep);
    $removed_names = array();
    foreach ($removed_files as $fid) {
      $file = file_load($fid);
      $removed_names[] = $file->filename;
    }
    drupal_set_message(t('Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', array(
      '%field' => $field['field_name'],
      '@max' => $field['cardinality'],
      '@count' => $keep,
      '%list' => implode(', ', $removed_names),
    )), 'warning');
    $values['fids'] = array_slice($values['fids'], 0, $keep);
    NestedArray::setValue($form_state['values'], $element['#parents'], $values);
  }
}