📜 ⬆️ ⬇️

File upload via Form API


Because I did not get this process right away, then I decided to describe it, maybe someone will come in handy and help.

To make it more interesting, I will describe the creation of a module for randomly displaying pictures.

Task
The task is to implement the ability to upload files to Drupal through the Form API. And the example described below is just an example of this implementation.

Functional
We will have admin panel where you can add or delete pictures. And also there will be a function that will return a randomly selected picture from the admin panel. Everything is very simple:)
')
Implementation
First you need to create a page in the admin panel, with our settings.
Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  1. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  2. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  3. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  4. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  5. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  6. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  7. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  8. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  9. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  10. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }
  11. Copy Source | Copy HTML /** <br/> * Implementation of hook_menu(). <br/> */ function ivt_header_menu() { $items [ 'admin/settings/ivt_header_settings' ] = array ( 'title' => t( 'IVT Header Settings' ), 'page callback' => 'drupal_get_form' , 'page arguments' => array ( '_ivt_header_settings_form' ), 'access arguments' => array ( 'access administration pages' ), 'type' => MENU_NORMAL_ITEM, ); return $items ; }

We describe the form itself with a list of downloaded files for deletion, a field for downloading a file, and a button.
Copy Source | Copy HTML
  1. function _ivt_header_settings_form () {
  2. $ form [ '#attributes' ] [ 'enctype' ] = 'multipart / form-data' ;
  3. $ form [ 'header_images_del' ] = array (
  4. '#type' => 'checkboxes' ,
  5. '#title' => t ( 'Delete images' ),
  6. '#options' => variable_get ( 'ivt_header_images' , array ()),
  7. );
  8. $ form [ 'header_images_upload' ] = array (
  9. '#type' => 'file' ,
  10. '#title' => t ( 'Attach new image' ),
  11. );
  12. $ form [ 'submit' ] = array (
  13. '#type' => 'submit' ,
  14. '#value' => t ( 'Go!' )
  15. );
  16. return $ form ;
  17. }

It will look like this:


Let's write a validator for this form, in which we will upload images to the site.
Copy Source | Copy HTML
  1. function _ivt_header_settings_form_validate ( $ form , & $ form_state ) {
  2. if ( isset ( $ form [ 'header_images_upload' ])) {
  3. // validation for file extension
  4. $ validators = array ( 'file_validate_extensions' => array ( 'png' ));
  5. // folder where the file will be loaded
  6. $ dir = file_directory_path (). '/ headers' ;
  7. // check if the directory exists. if it is not there, it will be created
  8. if (file_check_directory ( $ dir , 1 )) {
  9. // load the file
  10. $ file = file_save_upload ( 'header_images_upload' , $ validators , $ dir );
  11. // add new file field to $ form_state
  12. if ( $ file ) $ form_state [ 'values' ] [ 'header_image_file' ] = $ file ;
  13. }
  14. }
  15. }

In submite we will delete the selected files and add new ones.
Copy Source | Copy HTML
  1. function _ivt_header_settings_form_submit ( $ form , & $ form_state ) {
  2. $ image_file = $ form_state [ 'values' ] [ 'header_image_file' ];
  3. $ images_del = $ form_state [ 'values' ] [ 'header_images_del' ];
  4. $ images_del = array_filter ( $ images_del );
  5. // get the list of already uploaded files
  6. $ header_images = variable_get ( 'ivt_header_images' , array ());
  7. // if during submission a file was selected for download
  8. if ( $ image_file ) {
  9. // add it to our list
  10. $ header_images [ $ image_file -> fid] = l ( $ image_file -> filename, $ image_file -> filepath);
  11. drupal_set_message (t ( 'Image% has has been uploaded!' , array ( '% filename' => $ image_file -> filename)));
  12. }
  13. // if files were deleted during submission
  14. if ( $ images_del ) {
  15. // run through all files
  16. foreach ( $ images_del as $ fid ) {
  17. // load the file from the database
  18. $ sql_file = db_fetch_object (db_query ( "SELECT filepath, filename FROM files WHERE fid = '% s'" , $ fid ));
  19. // delete the file from the database
  20. db_query ( 'DELETE FROM {files} WHERE fid =% d' , $ fid );
  21. // delete the file
  22. file_delete ( $ sql_file -> filepath);
  23. // delete the file from our list
  24. unset ( $ header_images [ $ fid ]);
  25. drupal_set_message (t ( 'Image% has has been removed!' , array ( '% filename' => $ sql_file -> filename)));
  26. }
  27. }
  28. // save our list
  29. variable_set ( 'ivt_header_images' , $ header_images );
  30. }

It remains only to write a function that would take an arbitrary picture from our list and return it.
Copy Source | Copy HTML
  1. function ivt_header_get_image ( $ fid = null ) {
  2. global $ base_url ;
  3. // if there is no argument, then take arbitrarily
  4. if (! $ fid ) {
  5. // load the list
  6. $ header_images = variable_get ( 'ivt_header_images' , array ());
  7. // take a random
  8. $ fid = array_rand ( $ header_images );
  9. }
  10. // get the path to the image
  11. $ sql_file = db_fetch_object (db_query ( "SELECT filepath FROM files WHERE fid = '% s'" , $ fid ));
  12. $ filepath = $ sql_file -> filepath;
  13. $ image_html = "<img src = '" . $ base_url . '/' . $ filepath . "'/>" ;
  14. // return the image
  15. return $ header_images ? $ image_html : '' ;
  16. }

That's all.

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


All Articles