📜 ⬆️ ⬇️

Work with CCK Filefield. Insert and display Flash

image Sometimes it is necessary to display a file on a site not just by reference, but in some other way. Video and audio files you want to display the player, with the ability to view (listen), swf - immediately displayed on the page. There is an excellent Filefield module for attaching files to materials, but the choice of formatters for it is small. The file can be displayed simply by reference. Other modules slightly expand its functionality, for example Imagefield allows you to display images. I will try to explain in an easy way how to add the ability to display Flash content using the example of the SWFfield module.

Further there will be a lot of code, few pictures and in general everything is boring and sad ...

Start


Create a new directory swffield in the modules directory.
As with any module, we will need a .info file, in our case - swffield.info
Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-1.1"
  1. Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-1.1"
  2. Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-1.1"
  3. Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-1.1"
  4. Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-1.1"
  5. Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-1.1"
  6. Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-1.1"
  7. Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-1.1"


We also need the swffield.install file .
Copy Source | Copy HTML
  1. // hook_install (install module)
  2. function swffield_install () {
  3. // Load the content module
  4. drupal_load ( 'module' , 'content' );
  5. // We inform CCK about the installation of a new module
  6. content_notify ( 'install' , 'swffield' );
  7. }
  8. // hook_uninstall (remove the module)
  9. function swffield_uninstall () {
  10. drupal_load ( 'module' , 'content' );
  11. // We inform CCK about the removal of the new module
  12. content_notify ( 'uninstall' , 'swffield' );
  13. }
  14. // hook_enable (enable module)
  15. function swffield_enable () {
  16. drupal_load ( 'module' , 'content' );
  17. // We inform CCK about the inclusion of the new module
  18. content_notify ( 'enable' , 'swffield' );
  19. }
  20. // hook_disable (disable module)
  21. function swffield_disable () {
  22. drupal_load ( 'module' , 'content' );
  23. // We inform CCK about disabling the new module
  24. content_notify ( 'disable' , 'swffield' );
  25. }

Module


We will create the files swffield.module , swffield.render.inc (we will bring here all the code related to displaying the content to the user) and swffield.widget.inc (displaying the field in the admin panel). In principle, all code can be written in swffield.module - it will work.
')
swffield.module
Copy Source | Copy HTML
  1. // Connect the necessary files
  2. module_load_include ( 'inc' , 'swffield' , 'swffield.render' );
  3. module_load_include ( 'inc' , 'swffield' , 'swffield.widget' );
  4. // Initialization. Check if Filefield is there, if not, disable the module.
  5. function swffield_init () {
  6. if (! module_exists ( 'filefield' )) {
  7. module_disable ( array ( 'swffield' ));
  8. return ;
  9. }
  10. }
  11. // Set the data entry form of the widget and how it is processed.
  12. // Our field is no different from the Filefield field
  13. function swffield_elements () {
  14. $ filefield_elements = module_invoke ( 'filefield' , 'elements' );
  15. $ elements [ 'swffield_widget' ] = $ filefield_elements [ 'filefield_widget' ];
  16. return $ elements ;
  17. }
  18. // Report CCK Formatter Information
  19. function swffield_field_formatter_info () {
  20. $ formatters = array (
  21. 'swffield_flash' => array (
  22. 'label' => t ( 'Flash' ),
  23. 'field types' => array ( 'filefield' ),
  24. 'description' => t ( 'Displays Flash content' ),
  25. ),
  26. );
  27. return $ formatters ;
  28. }
  29. // Specify the standard field value as Filefield.
  30. function swffield_default_value (& $ form , & $ form_state , $ field , $ delta ) {
  31. return filefield_default_value ( $ form , $ form_state , $ field , $ delta );
  32. }
  33. // Specify when the field is considered empty
  34. function swffield_content_is_empty ( $ item , $ field ) {
  35. return filefield_content_is_empty ( $ item , $ field );
  36. }
  37. // Add another handler to save the field settings
  38. function swffield_form_content_field_overview_form_alter (& $ form , & $ form_state ) {
  39. $ form [ '#submit' ] [] = 'swffield_form_content_field_overview_submit' ;
  40. }
  41. // Handler for saving field settings
  42. function swffield_form_content_field_overview_submit (& $ form , & $ form_state ) {
  43. // If we add a new field to the material
  44. if ( isset ( $ form_state [ 'fields_added' ] [ '_add_new_field' ]) && isset ( $ form [ '#type_name' ])) {
  45. // Field Type
  46. $ new_field = $ form_state [ 'fields_added' ] [ '_add_new_field' ];
  47. // Material type
  48. $ node_type = $ form [ '#type_name' ];
  49. // Array with field data
  50. $ field = content_fields ( $ new_field , $ node_type );
  51. // If the field is added by our module
  52. if ( $ field [ 'widget' ] [ 'module' ] == 'swffield' ) {
  53. foreach ( $ field [ 'display_settings' ] as $ display_type => $ display_settings ) {
  54. if ( $ field [ 'display_settings' ] [ $ display_type ] [ 'format' ] == 'default' ) {
  55. // Set the display of the "swffield_flash" field
  56. $ field [ 'display_settings' ] [ $ display_type ] [ 'format' ] = 'swffield_flash' ;
  57. }
  58. }
  59. // Update the field
  60. content_field_instance_update ( $ field );
  61. }
  62. }
  63. }

swffield.widget.inc
Copy Source | Copy HTML
  1. // Report CCK Widget Information
  2. function swffield_widget_info () {
  3. return array (
  4. 'swffield_widget' => array (
  5. 'label' => t ( 'Flash' ),
  6. 'field types' => array ( 'filefield' ),
  7. 'multiple values' => CONTENT_HANDLE_CORE,
  8. 'callbacks' => array ( 'default value' => CONTENT_CALLBACK_CUSTOM),
  9. 'description' => 'Flash content' ,
  10. ),
  11. );
  12. }
  13. // This hook will be called every time our field is added to the form
  14. function swffield_widget (& $ form , & $ form_state , $ field , $ items , $ delta = 0 ) {
  15. $ element = filefield_widget ( $ form , $ form_state , $ field , $ items , $ delta );
  16. return $ element ;
  17. }
  18. // Settings widget (field)
  19. function swffield_widget_settings ( $ op , $ widget ) {
  20. switch ( $ op ) {
  21. case 'form' :
  22. Return swffield_widget_settings_form ( $ widget );
  23. case 'validate' :
  24. Return swffield_widget_settings_validate ( $ widget );
  25. case 'save' :
  26. Return swffield_widget_settings_save ( $ widget );
  27. }
  28. }
  29. // Form of field settings
  30. function swffield_widget_settings_form ( $ widget ) {
  31. $ form = module_invoke ( 'filefield' , 'widget_settings' , 'form' , $ widget );
  32. // By default, CCK substitutes the txt file type.
  33. // Replace it with swf
  34. if ( $ form [ 'file_extensions' ] [ '#default_value' ] == 'txt' ) {
  35. $ form [ 'file_extensions' ] [ '#default_value' ] = 'swf' ;
  36. }
  37. // Width field to specify the width of the displayed content
  38. $ form [ 'width' ] = array (
  39. '#type' => 'textfield' ,
  40. '#title' => 'Width' ,
  41. '#default_value' => $ widget [ 'width' ]? $ widget [ 'width' ]: 470 ,
  42. '#size' => 15 ,
  43. '#maxlength' => 5 ,
  44. '#description' => 'Flash content width' ,
  45. '#weight' => 2 . 1 ,
  46. );
  47. // Similar to "Height"
  48. $ form [ 'height' ] = array (
  49. '#type' => 'textfield' ,
  50. '#title' => 'Height' ,
  51. '#default_value' => $ widget [ 'height' ]? $ widget [ 'height' ]: 350 ,
  52. '#size' => 15 ,
  53. '#maxlength' => 5 ,
  54. '#description' => 'Flash content height' ,
  55. '#weight' => 2 . 2 ,
  56. );
  57. // Alternate text settings if the user has Javascript disabled or the Flash Player isn’t
  58. $ form [ 'alt_text' ] = array (
  59. '#type' => 'textarea' ,
  60. '#title' => 'Alternate Text' ,
  61. '#default_value' => $ widget [ 'alt_text' ]? $ widget [ 'alt_text' ]: 'Dear friends! <br /> Due to the fact that the technology of our site requires a pre-installed Adobe Flash Player, we strongly recommend that you install the latest version of the plug-in for your browser from <a href = " http://www.adobe.com/go/getflashplayer">Adobe.com </a>. ' ,
  62. '#description' => 'This text will be displayed if the user does not have the Flash player installed' ,
  63. '#weight' => 2 . 3 ,
  64. );
  65. return $ form ;
  66. }
  67. // Settings form validator
  68. function swffield_widget_settings_validate ( $ widget ) {
  69. $ extensions = array_filter (explode ( '' , $ widget [ 'file_extensions' ]));
  70. $ flash_extensions = array ( 'swf' );
  71. // Check the file extension
  72. if (count (array_diff ( $ extensions , $ flash_extensions ))) {
  73. form_set_error ( 'file_extensions' , 'Only SWF format is supported' );
  74. }
  75. // Check Width And Height
  76. foreach ( array ( 'width' , 'height' ) as $ resolution ) {
  77. if ( empty ( $ widget [ $ resolution ]) ||! preg_match ( '/ ^ [0-9] + $ /' , $ widget [ $ resolution ])) {
  78. form_set_error ( $ resolution , ' Invalid content size specified.' );
  79. }
  80. }
  81. return module_invoke ( 'filefield' , 'widget_settings' , 'validate' , $ widget );
  82. }
  83. // Save the field settings
  84. function swffield_widget_settings_save ( $ widget ) {
  85. $ filefield_settings = module_invoke ( 'filefield' , 'widget_settings' , 'save' , $ widget );
  86. return array_merge ( $ filefield_settings , array ( 'width' , 'height' , 'alt_text' ));
  87. }

swffield.render.inc
Copy Source | Copy HTML
  1. // Theme Hook
  2. // Set the theme functions for the widget, formatter and site display
  3. function swffield_theme () {
  4. $ theme = array (
  5. 'swffield_widget' => array (
  6. 'arguments' => array ( 'element' => NULL),
  7. ),
  8. 'swffield_formatter_swffield_flash' => array (
  9. 'arguments' => array ( 'element' => NULL),
  10. ),
  11. 'swffield_flash' => array (
  12. 'arguments' => array ( 'item' => NULL, 'attributes' => NULL),
  13. ),
  14. );
  15. return $ theme ;
  16. }
  17. // Theming of the widget is the same as for any element of the form
  18. function theme_swffield_widget ( $ element ) {
  19. return theme ( 'form_element' , $ element , $ element [ '#children' ]);
  20. }
  21. // Thematization of the formatter
  22. function theme_swffield_formatter_swffield_flash ( $ element ) {
  23. // Check if the file is loaded
  24. if ( empty ( $ element [ '#item' ] [ 'fid' ])) {
  25. return '' ;
  26. }
  27. $ field = content_fields ( $ element [ '#field_name' ], $ element [ '#node' ] -> type);
  28. $ item = $ element [ '#item' ];
  29. // Class to display the field
  30. $ class = 'swffield swffield-' . $ field [ 'field_name' ];
  31. // Return the output using the "swffield_flash" function
  32. return theme ( 'swffield_flash' , $ item , array ( 'class' => $ class , 'width' => $ field [ 'widget' ] [ 'width' ], 'height' => $ field [ 'widget' ] [ 'height' ], 'alt' => $ field [ 'widget' ] [ 'alt_text' ]));
  33. }
  34. // Auxiliary function of themes
  35. function theme_swffield_flash ( $ item , $ attributes ) {
  36. // Load the library "swfobject.js"
  37. drupal_add_js (drupal_get_path ( 'module' , 'swffield' ). "/" . "swfobject.js" );
  38. // Generate ID for the displayed item
  39. $ id = "swffield-" . rand ( 1 , 10000 );
  40. // The path to the file
  41. $ file = "/" . $ item [ 'filepath' ];
  42. // Ready item
  43. return "<div id = '" . $ id . "'class ='" . $ attributes [ 'class' ]. "'>"
  44. . $ attributes [ 'alt' ]
  45. . "<script type = 'text / javascript'>"
  46. . "var flashvars = {};"
  47. . "var params = {bgcolor: '# ffffff', allowFullScreen: 'false', allowScriptAccess: 'always', wmode: 'opaque'};"
  48. . "new swfobject.embedSWF ('" . $ file . "', '" . $ id . "', '" . $ attributes [ ' width ' ]. "', '" . $ attributes [ ' height ' ]. " ',' 9.0.0 ', false, flashvars, params); "
  49. . "</ script>"
  50. . "</ div>" ;
  51. }

It uses excellent js SWFobject library. The swfobject.js file must be put in the directory with our module.
Another subtle point. For each display of our element on the page, it is necessary to generate a unique ID, by which the content will be inserted later by javascript. I use rand (1, 10000) . If there is a more adequate way, I will be glad to hear it in the comments.

Conclusion


That's all that is needed to add the display of SWF content on the pages of our site. Similarly, you can, for example, add the display "Video Player" for FLV or "Audio Player" for MP3.

Good luck!

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


All Articles