Reprocessing Attached Images in Drupal 7

I am trying to import nodes from my forum on drupal 7. Not in bulk, but one after the other so that news messages can be created and referenced in the forum. Kicker is that I also want to use image attachments ...

So far, using the http://drupal.org/node/889058#comment-3709802 code example here , basically work: Nodes are created, but images do not go through validation or processing.

I want attached images to be checked for compliance with the rules defined in the content type. in particular, the style associated with my image field, which resizes them to 600x600.

So, instead of just creating nodes programmatically with my own form, I decided to change the โ€œnewโ€ node using hook_node_prepare and use the existing form to create new content (based on the urgs passed to args). This works very well, and the creation form is pre-populated with all my data. including image! very cute.

I expected that then I would be able to click on the preview or save, and all the checks and resizing will happen with my image, but instead I get an error message:

"The file used in the Image field may not be referenced."

The reason for this is that my file does not have an entry in the file_usage table. * le sigh *

so how can I get all the good validation and processing that occurs when I manually select a file to upload? like resizing, entry in the file_usage table.

ajax , , api.

/ Drupal, ?

- /image api Drupal 7, ?

+3
1

( , , ), Drupal 'file_usage_add()'

user.module( PHP, , , - "Drupal way" ):

function user_validate_picture(&$form, &$form_state) {
  // If required, validate the uploaded picture.
  $validators = array(
    'file_validate_is_image' => array(),
    'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
    'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
  );

  // Save the file as a temporary file.
  $file = file_save_upload('picture_upload', $validators);
  if ($file === FALSE) {
    form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures'))));
  }
  elseif ($file !== NULL) {
    $form_state['values']['picture_upload'] = $file;
  }
}

$form ['# validate'] :

$ form ['# validate'] [] = 'user_validate_picture'

+1
source

Source: https://habr.com/ru/post/1789346/


All Articles