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.Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-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"Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-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"Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-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"Copy Source | Copy HTML name = SwfField description = Flash CCK. dependencies[] = content dependencies[] = filefield package = CCK core = 6.x version = "6.x-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"
Copy Source | Copy HTML
- // hook_install (install module)
- function swffield_install () {
- // Load the content module
- drupal_load ( 'module' , 'content' );
- // We inform CCK about the installation of a new module
- content_notify ( 'install' , 'swffield' );
- }
- // hook_uninstall (remove the module)
- function swffield_uninstall () {
- drupal_load ( 'module' , 'content' );
- // We inform CCK about the removal of the new module
- content_notify ( 'uninstall' , 'swffield' );
- }
- // hook_enable (enable module)
- function swffield_enable () {
- drupal_load ( 'module' , 'content' );
- // We inform CCK about the inclusion of the new module
- content_notify ( 'enable' , 'swffield' );
- }
- // hook_disable (disable module)
- function swffield_disable () {
- drupal_load ( 'module' , 'content' );
- // We inform CCK about disabling the new module
- content_notify ( 'disable' , 'swffield' );
- }
Copy Source | Copy HTML
- // Connect the necessary files
- module_load_include ( 'inc' , 'swffield' , 'swffield.render' );
- module_load_include ( 'inc' , 'swffield' , 'swffield.widget' );
- // Initialization. Check if Filefield is there, if not, disable the module.
- function swffield_init () {
- if (! module_exists ( 'filefield' )) {
- module_disable ( array ( 'swffield' ));
- return ;
- }
- }
- // Set the data entry form of the widget and how it is processed.
- // Our field is no different from the Filefield field
- function swffield_elements () {
- $ filefield_elements = module_invoke ( 'filefield' , 'elements' );
- $ elements [ 'swffield_widget' ] = $ filefield_elements [ 'filefield_widget' ];
- return $ elements ;
- }
- // Report CCK Formatter Information
- function swffield_field_formatter_info () {
- $ formatters = array (
- 'swffield_flash' => array (
- 'label' => t ( 'Flash' ),
- 'field types' => array ( 'filefield' ),
- 'description' => t ( 'Displays Flash content' ),
- ),
- );
- return $ formatters ;
- }
- // Specify the standard field value as Filefield.
- function swffield_default_value (& $ form , & $ form_state , $ field , $ delta ) {
- return filefield_default_value ( $ form , $ form_state , $ field , $ delta );
- }
- // Specify when the field is considered empty
- function swffield_content_is_empty ( $ item , $ field ) {
- return filefield_content_is_empty ( $ item , $ field );
- }
- // Add another handler to save the field settings
- function swffield_form_content_field_overview_form_alter (& $ form , & $ form_state ) {
- $ form [ '#submit' ] [] = 'swffield_form_content_field_overview_submit' ;
- }
- // Handler for saving field settings
- function swffield_form_content_field_overview_submit (& $ form , & $ form_state ) {
- // If we add a new field to the material
- if ( isset ( $ form_state [ 'fields_added' ] [ '_add_new_field' ]) && isset ( $ form [ '#type_name' ])) {
- // Field Type
- $ new_field = $ form_state [ 'fields_added' ] [ '_add_new_field' ];
- // Material type
- $ node_type = $ form [ '#type_name' ];
- // Array with field data
- $ field = content_fields ( $ new_field , $ node_type );
- // If the field is added by our module
- if ( $ field [ 'widget' ] [ 'module' ] == 'swffield' ) {
- foreach ( $ field [ 'display_settings' ] as $ display_type => $ display_settings ) {
- if ( $ field [ 'display_settings' ] [ $ display_type ] [ 'format' ] == 'default' ) {
- // Set the display of the "swffield_flash" field
- $ field [ 'display_settings' ] [ $ display_type ] [ 'format' ] = 'swffield_flash' ;
- }
- }
- // Update the field
- content_field_instance_update ( $ field );
- }
- }
- }
Copy Source | Copy HTML
- // Report CCK Widget Information
- function swffield_widget_info () {
- return array (
- 'swffield_widget' => array (
- 'label' => t ( 'Flash' ),
- 'field types' => array ( 'filefield' ),
- 'multiple values' => CONTENT_HANDLE_CORE,
- 'callbacks' => array ( 'default value' => CONTENT_CALLBACK_CUSTOM),
- 'description' => 'Flash content' ,
- ),
- );
- }
- // This hook will be called every time our field is added to the form
- function swffield_widget (& $ form , & $ form_state , $ field , $ items , $ delta = 0 ) {
- $ element = filefield_widget ( $ form , $ form_state , $ field , $ items , $ delta );
- return $ element ;
- }
- // Settings widget (field)
- function swffield_widget_settings ( $ op , $ widget ) {
- switch ( $ op ) {
- case 'form' :
- Return swffield_widget_settings_form ( $ widget );
- case 'validate' :
- Return swffield_widget_settings_validate ( $ widget );
- case 'save' :
- Return swffield_widget_settings_save ( $ widget );
- }
- }
- // Form of field settings
- function swffield_widget_settings_form ( $ widget ) {
- $ form = module_invoke ( 'filefield' , 'widget_settings' , 'form' , $ widget );
- // By default, CCK substitutes the txt file type.
- // Replace it with swf
- if ( $ form [ 'file_extensions' ] [ '#default_value' ] == 'txt' ) {
- $ form [ 'file_extensions' ] [ '#default_value' ] = 'swf' ;
- }
- // Width field to specify the width of the displayed content
- $ form [ 'width' ] = array (
- '#type' => 'textfield' ,
- '#title' => 'Width' ,
- '#default_value' => $ widget [ 'width' ]? $ widget [ 'width' ]: 470 ,
- '#size' => 15 ,
- '#maxlength' => 5 ,
- '#description' => 'Flash content width' ,
- '#weight' => 2 . 1 ,
- );
- // Similar to "Height"
- $ form [ 'height' ] = array (
- '#type' => 'textfield' ,
- '#title' => 'Height' ,
- '#default_value' => $ widget [ 'height' ]? $ widget [ 'height' ]: 350 ,
- '#size' => 15 ,
- '#maxlength' => 5 ,
- '#description' => 'Flash content height' ,
- '#weight' => 2 . 2 ,
- );
- // Alternate text settings if the user has Javascript disabled or the Flash Player isn’t
- $ form [ 'alt_text' ] = array (
- '#type' => 'textarea' ,
- '#title' => 'Alternate Text' ,
- '#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>. ' ,
- '#description' => 'This text will be displayed if the user does not have the Flash player installed' ,
- '#weight' => 2 . 3 ,
- );
- return $ form ;
- }
- // Settings form validator
- function swffield_widget_settings_validate ( $ widget ) {
- $ extensions = array_filter (explode ( '' , $ widget [ 'file_extensions' ]));
- $ flash_extensions = array ( 'swf' );
- // Check the file extension
- if (count (array_diff ( $ extensions , $ flash_extensions ))) {
- form_set_error ( 'file_extensions' , 'Only SWF format is supported' );
- }
- // Check Width And Height
- foreach ( array ( 'width' , 'height' ) as $ resolution ) {
- if ( empty ( $ widget [ $ resolution ]) ||! preg_match ( '/ ^ [0-9] + $ /' , $ widget [ $ resolution ])) {
- form_set_error ( $ resolution , ' Invalid content size specified.' );
- }
- }
- return module_invoke ( 'filefield' , 'widget_settings' , 'validate' , $ widget );
- }
- // Save the field settings
- function swffield_widget_settings_save ( $ widget ) {
- $ filefield_settings = module_invoke ( 'filefield' , 'widget_settings' , 'save' , $ widget );
- return array_merge ( $ filefield_settings , array ( 'width' , 'height' , 'alt_text' ));
- }
Copy Source | Copy HTML
- // Theme Hook
- // Set the theme functions for the widget, formatter and site display
- function swffield_theme () {
- $ theme = array (
- 'swffield_widget' => array (
- 'arguments' => array ( 'element' => NULL),
- ),
- 'swffield_formatter_swffield_flash' => array (
- 'arguments' => array ( 'element' => NULL),
- ),
- 'swffield_flash' => array (
- 'arguments' => array ( 'item' => NULL, 'attributes' => NULL),
- ),
- );
- return $ theme ;
- }
- // Theming of the widget is the same as for any element of the form
- function theme_swffield_widget ( $ element ) {
- return theme ( 'form_element' , $ element , $ element [ '#children' ]);
- }
- // Thematization of the formatter
- function theme_swffield_formatter_swffield_flash ( $ element ) {
- // Check if the file is loaded
- if ( empty ( $ element [ '#item' ] [ 'fid' ])) {
- return '' ;
- }
- $ field = content_fields ( $ element [ '#field_name' ], $ element [ '#node' ] -> type);
- $ item = $ element [ '#item' ];
- // Class to display the field
- $ class = 'swffield swffield-' . $ field [ 'field_name' ];
- // Return the output using the "swffield_flash" function
- return theme ( 'swffield_flash' , $ item , array ( 'class' => $ class , 'width' => $ field [ 'widget' ] [ 'width' ], 'height' => $ field [ 'widget' ] [ 'height' ], 'alt' => $ field [ 'widget' ] [ 'alt_text' ]));
- }
- // Auxiliary function of themes
- function theme_swffield_flash ( $ item , $ attributes ) {
- // Load the library "swfobject.js"
- drupal_add_js (drupal_get_path ( 'module' , 'swffield' ). "/" . "swfobject.js" );
- // Generate ID for the displayed item
- $ id = "swffield-" . rand ( 1 , 10000 );
- // The path to the file
- $ file = "/" . $ item [ 'filepath' ];
- // Ready item
- return "<div id = '" . $ id . "'class ='" . $ attributes [ 'class' ]. "'>"
- . $ attributes [ 'alt' ]
- . "<script type = 'text / javascript'>"
- . "var flashvars = {};"
- . "var params = {bgcolor: '# ffffff', allowFullScreen: 'false', allowScriptAccess: 'always', wmode: 'opaque'};"
- . "new swfobject.embedSWF ('" . $ file . "', '" . $ id . "', '" . $ attributes [ ' width ' ]. "', '" . $ attributes [ ' height ' ]. " ',' 9.0.0 ', false, flashvars, params); "
- . "</ script>"
- . "</ div>" ;
- }
Source: https://habr.com/ru/post/96549/
All Articles