Skip to content

Instantly share code, notes, and snippets.

@init90
Last active July 12, 2018 12:30
Show Gist options
  • Save init90/d5fd1e170c3af8290020412f66e2a280 to your computer and use it in GitHub Desktop.
Save init90/d5fd1e170c3af8290020412f66e2a280 to your computer and use it in GitHub Desktop.
Drupal 8, Add process function to managed_file field.
$default_process_fn = \Drupal::service('element_info')->getInfoProperty('managed_file', '#process', NULL);
$form['field_photo'] = [
'#type' => 'managed_file',
'#required' => TRUE,
'#title' => $this->getFieldTitle($product, 'field_photo'),
'#description' => $this->getFieldDescription($product, 'field_photo'),
'#upload_location' => 'public://',
'#process' => array_merge($default_process_fn, [[get_class($this), 'managedFileProcess']]),
'#upload_validators' => [
'file_validate_extensions' => [
$this->getAvailableExtensionsFromFieldSettings('field_photo', 'photo_print'),
],
],
];
/**
* Process managed file.
*/
public static function managedFileProcess(&$element, FormStateInterface $form_state, &$form) {
/**
* After upload file, $form_state['values'] is clean. Code below prevent this problem.
* Quite perhaps it's Drupal bag.
*
* Related issue:
* https://www.drupal.org/node/2311199
* https://drupal.stackexchange.com/questions/242641/drupal-8-theme-settings-form-api-remove-permanent-managed-file
*
* Problem place - drupal_validate_form().
*/
$element['upload_button']['#limit_validation_errors'] = FALSE;
$element['remove_button']['#limit_validation_errors'] = FALSE;
return $element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment