You are here

function filefield_sources_field_submit in FileField Sources 8

Same name and namespace in other branches
  1. 7 filefield_sources.module \filefield_sources_field_submit()

Form submission handler for all FileField Source buttons.

Clone of \Drupal\file\Plugin\Field\FieldWidget\FileWidget::submit(), with a few changes:

  • Submit button is one level down compare to 'Upload' source's submit button.
  • Replace static in static::getWidgetState and static::setWidgetState by WidgetBase.
  • Rebuild the form after all.
5 string references to 'filefield_sources_field_submit'
Attach::process in src/Plugin/FilefieldSource/Attach.php
Process callback for file field source plugin.
Clipboard::process in src/Plugin/FilefieldSource/Clipboard.php
Process callback for file field source plugin.
Imce::process in src/Plugin/FilefieldSource/Imce.php
Process callback for file field source plugin.
Reference::process in src/Plugin/FilefieldSource/Reference.php
Process callback for file field source plugin.
Remote::process in src/Plugin/FilefieldSource/Remote.php
Process callback for file field source plugin.

File

./filefield_sources.module, line 301
Extend FileField to allow files from multiple sources.

Code

function filefield_sources_field_submit(&$form, FormStateInterface $form_state) {

  // During the form rebuild, formElement() will create field item widget
  // elements using re-indexed deltas, so clear out FormState::$input to
  // avoid a mismatch between old and new deltas. The rebuilt elements will
  // have #default_value set appropriately for the current state of the field,
  // so nothing is lost in doing this.
  $button = $form_state
    ->getTriggeringElement();
  $parents = array_slice($button['#parents'], 0, -3);
  NestedArray::setValue($form_state
    ->getUserInput(), $parents, NULL);

  // Go one level up in the form, to the widgets container.
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));
  $field_name = $element['#field_name'];
  $parents = $element['#field_parents'];
  $submitted_values = NestedArray::getValue($form_state
    ->getValues(), array_slice($button['#parents'], 0, -3));
  foreach ($submitted_values as $delta => $submitted_value) {
    if (empty($submitted_value['fids'])) {
      unset($submitted_values[$delta]);
    }
  }

  // If there are more files uploaded via the same widget, we have to separate
  // them, as we display each file in it's own widget.
  $new_values = [];
  foreach ($submitted_values as $delta => $submitted_value) {
    if (is_array($submitted_value['fids'])) {
      foreach ($submitted_value['fids'] as $fid) {
        $new_value = $submitted_value;
        $new_value['fids'] = [
          $fid,
        ];
        $new_values[] = $new_value;
      }
    }
    else {
      $new_value = $submitted_value;
    }
  }

  // Re-index deltas after removing empty items.
  $submitted_values = array_values($new_values);

  // Update form_state values.
  NestedArray::setValue($form_state
    ->getValues(), array_slice($button['#parents'], 0, -3), $submitted_values);

  // Update items.
  $field_state = WidgetBase::getWidgetState($parents, $field_name, $form_state);
  $field_state['items'] = $submitted_values;
  WidgetBase::setWidgetState($parents, $field_name, $form_state, $field_state);

  // We need to rebuild the form, so that uploaded file can be displayed.
  $form_state
    ->setRebuild();
}