0

Does anyone know if there is a way in Drupal 7 to change dynamically the value of minimum / maximum resolution for an image base on a previous select list.

Example :

I have a select with 3 elements :

  • Square
  • Portrait
  • Landscape

When I select Square, the min / max resolution would be set to 200x200, if Portrait 200x400 and landscape 400x200.

I could create 3 different image field but in the future I'll have more type of layouts and I'd rather have only one image field (for database and theming purpose)

I was thinking State API but no solution there.

0

I came up with a good solution using #ajax property from the Form API :

I declare hook_form_alter and add the #ajax on my push type select field.

/**
 * hook_form_alter
 */
function my_module_form_node_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'push_node_form') {
    // Add id to field image for ajax reload
    $form['field_push_image']['#prefix'] = '<div id="image-field-to-reload">';
    $form['field_push_image']['#suffix'] = '</div>';
    // Create ajax reload function
    $form['field_push_type'][LANGUAGE_NONE]['#ajax'] = array(
      'callback' => 'reload_image_field_resolution',
      'wrapper' => 'image-field-to-reload',
      'method' => 'replace',
      'effect' => 'fade',
    );
  }
}

Then in the field settings I put a resolution as a key from the select list like this :

200x200|Square

200x400|Portrait

400x200|Landscape

And then I reload the image field with the proper resolution base on the select value key.

/**
 * Ajax callback to reload the image field
 */
function reload_image_field_resolution($form, $form_state) {
  // Get the value of the push type
  $type_value = $form['field_push_type'][LANGUAGE_NONE]['#value'];
  // Explode the value to make sure that it's a good safe key value representing
  // a resolution like this : 100x100
  $resolution = explode('x', $type_value);

  if (count($resolution) == 2 && is_numeric($resolution[0]) && is_numeric($resolution[1])) {
    // If the value is good, build a new array containing the new resolution
    $min_max_resolution = array(
      $type_value,
      $type_value
    );
    // Reset the max and min with of the image field
    $form['field_push_image'][LANGUAGE_NONE][0]['#upload_validators']['file_validate_image_resolution'] = $min_max_resolution;
    // Set the variable to pass to the theme function with empty description otherwise
    // the old string (with the basic resolution) will be concatenated to the new string.
    $variables = array(
      'description' => '',
      'upload_validators' => $form['field_push_image'][LANGUAGE_NONE][0]['#upload_validators']
    );
    // Return the approriate altered string using theme_file_upload_help()
    $form['field_push_image'][LANGUAGE_NONE][0]['#description'] = theme_file_upload_help($variables);
  }

  // Return the image field with its new resolution
  return $form['field_push_image'];
}

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.