0

I'm trying to ignore the auto down scaling of large images and replace the validation step with a hard check that the size matches the maximum size provided. I'm trying to do this through the HOOK_form_alter hook and while it appears to be affecting the form render array, the AJAX request seems to ignore my changes and run the default upload validation steps.

Here's what i'm doing: In the seven.theme file:

function seven_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $imageForValidation = NULL;

  if (isset($form['field_name_here'])) {
    $imageForValidation = $form['field_name_here'];
  }

  if ($imageForValidation != NULL) {
    // Validate
    $imageForValidation['widget'][0]['#upload_validators']['myTheme_image_validator'] = array();
    unset($imageForValidation['widget'][0]['#upload_validators']['file_validate_image_resolution']);
  }

  return $form;
}

function myTheme_image_validator($file) {
  $errors = array();

  // This doesn't even get called.

  return $errors;
}

I've debugged with xdebug and the render array is indeed getting affected, but the result of uploading an image is always the same as the default setup. At the very least I expect the unset validator to not get run, but it still downscales.

Has anyone got round this issue before?

1
  • 1
    You need to alter the original form, if you make a local copy of the field array that won't happen any more. Use $imageForValidation = &$form['field_name_here']; to fix – Clive Jul 7 '17 at 17:46
0

Clive was spot on. Turns out I wasn't passing my variable by reference. I was thrown off by (what is now old hat in Drupal 8) returning the form variable at the end of the form_alter hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.