From 730a8a98f2a49cd2c09b733ba01e05c6e17816d8 Mon Sep 17 00:00:00 2001 From: Heshan Wanigasooriya Date: Sun, 5 Dec 2010 16:30:18 +0000 Subject: Updating the upload video field for Drupal 7 field API --- types/videoupload/videoupload.field.inc | 557 +++++++++++++++++++++++++++++++- types/videoupload/videoupload.info | 6 +- types/videoupload/videoupload.module | 43 +-- video.info | 1 + 4 files changed, 571 insertions(+), 36 deletions(-) diff --git a/types/videoupload/videoupload.field.inc b/types/videoupload/videoupload.field.inc index a79177e..01edac9 100644 --- a/types/videoupload/videoupload.field.inc +++ b/types/videoupload/videoupload.field.inc @@ -1,7 +1,556 @@ array( + 'label' => t('Video'), + 'description' => t('This field stores the ID of an video file as an integer value.'), + 'settings' => array( + 'uri_scheme' => variable_get('file_default_scheme', 'public'), + 'default_image' => 0, + ), + 'instance_settings' => array( + 'file_extensions' => 'avi wmv flv', + 'file_directory' => '', + 'max_filesize' => '', + 'max_resolution' => '', + 'min_resolution' => '', + ), + 'default_widget' => 'video_video', + 'default_formatter' => 'video', + ), + ); +} + +/** + * Implements hook_field_settings_form(). + */ +function videoupload_field_settings_form($field, $instance) { + $defaults = field_info_field_settings($field['type']); + $settings = array_merge($defaults, $field['settings']); + + $scheme_options = array(); + foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $stream_wrapper) { + $scheme_options[$scheme] = $stream_wrapper['name']; + } + $form['uri_scheme'] = array( + '#type' => 'radios', + '#title' => t('Upload destination'), + '#options' => $scheme_options, + '#default_value' => $settings['uri_scheme'], + '#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'), + ); + + $form['default_image'] = array( + '#title' => t('Default image'), + '#type' => 'managed_file', + '#description' => t('If no image is uploaded, this image will be shown on display.'), + '#default_value' => $field['settings']['default_image'], + '#upload_location' => 'public://default_images/', + ); + + return $form; + +} + +/** + * Implements hook_field_instance_settings_form(). + */ +function videoupload_field_instance_settings_form($field, $instance) { + $settings = $instance['settings']; + + // Use the file field instance settings form as a basis. + $form = file_field_instance_settings_form($field, $instance); + + // Add maximum and minimum resolution settings. + $max_resolution = explode('x', $settings['max_resolution']) + array('', ''); + $form['max_resolution'] = array( + '#type' => 'item', + '#title' => t('Maximum image resolution'), + '#element_validate' => array('_image_field_resolution_validate'), + '#weight' => 4.1, + '#field_prefix' => '
', + '#field_suffix' => '
', + '#description' => t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Leave blank for no restriction. If a larger image is uploaded, it will be resized to reflect the given width and height. Resizing images on upload will cause the loss of EXIF data in the image.'), + ); + $form['max_resolution']['x'] = array( + '#type' => 'textfield', + '#title' => t('Maximum width'), + '#title_display' => 'invisible', + '#default_value' => $max_resolution[0], + '#size' => 5, + '#maxlength' => 5, + '#field_suffix' => ' x ', + ); + $form['max_resolution']['y'] = array( + '#type' => 'textfield', + '#title' => t('Maximum height'), + '#title_display' => 'invisible', + '#default_value' => $max_resolution[1], + '#size' => 5, + '#maxlength' => 5, + '#field_suffix' => ' ' . t('pixels'), + ); + + $min_resolution = explode('x', $settings['min_resolution']) + array('', ''); + $form['min_resolution'] = array( + '#type' => 'item', + '#title' => t('Minimum image resolution'), + '#element_validate' => array('_image_field_resolution_validate'), + '#weight' => 4.2, + '#field_prefix' => '
', + '#field_suffix' => '
', + '#description' => t('The minimum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Leave blank for no restriction. If a smaller image is uploaded, it will be rejected.'), + ); + $form['min_resolution']['x'] = array( + '#type' => 'textfield', + '#title' => t('Minimum width'), + '#title_display' => 'invisible', + '#default_value' => $min_resolution[0], + '#size' => 5, + '#maxlength' => 5, + '#field_suffix' => ' x ', + ); + $form['min_resolution']['y'] = array( + '#type' => 'textfield', + '#title' => t('Minimum height'), + '#title_display' => 'invisible', + '#default_value' => $min_resolution[1], + '#size' => 5, + '#maxlength' => 5, + '#field_suffix' => ' ' . t('pixels'), + ); + + // Remove the description option. + unset($form['description_field']); + + // Add title and alt configuration options. + $form['alt_field'] = array( + '#type' => 'checkbox', + '#title' => t('Enable Alt field'), + '#default_value' => $settings['alt_field'], + '#description' => t('The alt attribute may be used by search engines, screen readers, and when the image cannot be loaded.'), + '#weight' => 10, + ); + $form['title_field'] = array( + '#type' => 'checkbox', + '#title' => t('Enable Title field'), + '#default_value' => $settings['title_field'], + '#description' => t('The title attribute is used as a tooltip when the mouse hovers over the image.'), + '#weight' => 11, + ); + + return $form; +} + +/** + * Element validate function for resolution fields. + */ +function _video_field_resolution_validate($element, &$form_state) { + if (!empty($element['x']['#value']) || !empty($element['y']['#value'])) { + foreach (array('x', 'y') as $dimension) { + $value = $element[$dimension]['#value']; + if (!is_numeric($value)) { + form_error($element[$dimension], t('Height and width values must be numeric.')); + return; + } + if (intval($value) == 0) { + form_error($element[$dimension], t('Both a height and width value must be specified in the !name field.', array('!name' => $element['#title']))); + return; + } + } + form_set_value($element, intval($element['x']['#value']) . 'x' . intval($element['y']['#value']), $form_state); + } + else { + form_set_value($element, '', $form_state); + } +} + +/** + * Implements hook_field_load(). + */ +function videoupload_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) { + file_field_load($entity_type, $entities, $field, $instances, $langcode, $items, $age); +} + +/** + * Implements hook_field_prepare_view(). + */ +function videoupload_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { + // If there are no files specified at all, use the default. + foreach ($entities as $id => $entity) { + if (empty($items[$id]) && $field['settings']['default_image']) { + if ($file = file_load($field['settings']['default_image'])) { + $items[$id][0] = (array) $file + array( + 'is_default' => TRUE, + 'alt' => '', + 'title' => '', + ); + } + } + } +} + +/** + * Implements hook_field_presave(). + */ +function videoupload_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { + file_field_presave($entity_type, $entity, $field, $instance, $langcode, $items); +} + +/** + * Implements hook_field_insert(). + */ +function videoupload_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) { + file_field_insert($entity_type, $entity, $field, $instance, $langcode, $items); +} + +/** + * Implements hook_field_update(). + */ +function videoupload_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) { + file_field_update($entity_type, $entity, $field, $instance, $langcode, $items); +} + +/** + * Implements hook_field_delete(). + */ +function videoupload_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) { + file_field_delete($entity_type, $entity, $field, $instance, $langcode, $items); +} + +/** + * Implements hook_field_delete_revision(). + */ +function videoupload_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) { + file_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, $items); +} + +/** + * Implements hook_field_is_empty(). + */ +function videoupload_field_is_empty($item, $field) { + return file_field_is_empty($item, $field); +} + +/** + * Implements hook_field_widget_info(). + */ +function videoupload_field_widget_info() { + return array( + 'video_video' => array( + 'label' => t('Video'), + 'field types' => array('video'), + 'settings' => array( + 'progress_indicator' => 'throbber', + 'preview_image_style' => 'thumbnail', + ), + 'behaviors' => array( + 'multiple values' => FIELD_BEHAVIOR_CUSTOM, + 'default value' => FIELD_BEHAVIOR_NONE, + ), + ), + ); +} + +/** + * Implements hook_field_widget_settings_form(). + */ +function videoupload_field_widget_settings_form($field, $instance) { + $widget = $instance['widget']; + $settings = $widget['settings']; + + // Use the file widget settings form. + $form = file_field_widget_settings_form($field, $instance); + + $form['preview_image_style'] = array( + '#title' => t('Preview image style'), + '#type' => 'select', + '#options' => image_style_options(FALSE), + '#empty_option' => '<' . t('no preview') . '>', + '#default_value' => $settings['preview_image_style'], + '#description' => t('The preview image will be shown while editing the content.'), + '#weight' => 15, + ); + + return $form; +} + +/** + * Implements hook_field_widget_form(). + */ +function videoupload_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { + + // Add display_field setting to field because file_field_widget_form() assumes it is set. + $field['settings']['display_field'] = 0; + + $elements = file_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element); + $settings = $instance['settings']; + + foreach (element_children($elements) as $delta) { + // Add upload resolution validation. + if ($settings['max_resolution'] || $settings['min_resolution']) { + $elements[$delta]['#upload_validators']['file_validate_image_resolution'] = array($settings['max_resolution'], $settings['min_resolution']); + } + + // If not using custom extension validation, ensure this is an image. + $supported_extensions = array('png', 'gif', 'jpg', 'jpeg'); + $extensions = isset($elements[$delta]['#upload_validators']['file_validate_extensions'][0]) ? $elements[$delta]['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions); + $extensions = array_intersect(explode(' ', $extensions), $supported_extensions); + $elements[$delta]['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions); + + // Add all extra functionality provided by the image widget. + $elements[$delta]['#process'][] = 'image_field_widget_process'; + } + + if ($field['cardinality'] == 1) { + // If there's only one field, return it as delta 0. + if (empty($elements[0]['#default_value']['fid'])) { + $elements[0]['#description'] = theme('file_upload_help', array('description' => $instance['description'], 'upload_validators' => $elements[0]['#upload_validators'])); + } + } + else { + $elements['#file_upload_description'] = theme('file_upload_help', array('upload_validators' => $elements[0]['#upload_validators'])); + } + return $elements; +} + +/** + * An element #process callback for the image_image field type. + * + * Expands the image_image type to include the alt and title fields. + */ +function videoupload_field_widget_process($element, &$form_state, $form) { + $item = $element['#value']; + $item['fid'] = $element['fid']['#value']; + + $instance = field_widget_instance($element, $form_state); + + $settings = $instance['settings']; + $widget_settings = $instance['widget']['settings']; + + $element['#theme'] = 'image_widget'; + $element['#attached']['css'][] = drupal_get_path('module', 'image') . '/image.css'; + + // Add the image preview. + if ($element['#file'] && $widget_settings['preview_image_style']) { + $element['preview'] = array( + '#type' => 'markup', + '#markup' => theme('image_style', array('style_name' => $widget_settings['preview_image_style'], 'path' => $element['#file']->uri)), + ); + } + + // Add the additional alt and title fields. + $element['alt'] = array( + '#title' => t('Alternate text'), + '#type' => 'textfield', + '#default_value' => isset($item['alt']) ? $item['alt'] : '', + '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'), + '#maxlength' => variable_get('image_alt_length', 80), // See http://www.gawds.org/show.php?contentid=28. + '#weight' => -2, + '#access' => (bool) $item['fid'] && $settings['alt_field'], + ); + $element['title'] = array( + '#type' => 'textfield', + '#title' => t('Title'), + '#default_value' => isset($item['title']) ? $item['title'] : '', + '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'), + '#maxlength' => variable_get('image_title_length', 500), + '#weight' => -1, + '#access' => (bool) $item['fid'] && $settings['title_field'], + ); + + return $element; +} + +/** + * Returns HTML for an image field widget. + * + * @param $variables + * An associative array containing: + * - element: A render element representing the image field widget. + * + * @ingroup themeable + */ +function theme_videoupload_widget($variables) { + $element = $variables['element']; + $output = ''; + $output .= '
'; + + if (isset($element['preview'])) { + $output .= '
'; + $output .= drupal_render($element['preview']); + $output .= '
'; + } + + $output .= '
'; + if ($element['fid']['#value'] != 0) { + $element['filename']['#markup'] .= ' (' . format_size($element['#file']->filesize) . ') '; + } + $output .= drupal_render_children($element); + $output .= '
'; + $output .= '
'; + + return $output; +} + +/** + * Implements hook_field_formatter_info(). */ +function videoupload_field_formatter_info() { + $formatters = array( + 'video' => array( + 'label' => t('Video'), + 'field types' => array('video'), + 'settings' => array('image_style' => '', 'image_link' => ''), + ), + ); + + return $formatters; +} + +/** + * Implements hook_field_formatter_settings_form(). + */ +function videoupload_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + + $image_styles = image_style_options(FALSE); + $element['image_style'] = array( + '#title' => t('Image style'), + '#type' => 'select', + '#default_value' => $settings['image_style'], + '#empty_option' => t('None (original image)'), + '#options' => $image_styles, + ); + + $link_types = array( + 'content' => t('Content'), + 'file' => t('File'), + ); + $element['image_link'] = array( + '#title' => t('Link image to'), + '#type' => 'select', + '#default_value' => $settings['image_link'], + '#empty_option' => t('Nothing'), + '#options' => $link_types, + ); + + return $element; +} + +/** + * Implements hook_field_formatter_settings_summary(). + */ +function videoupload_field_formatter_settings_summary($field, $instance, $view_mode) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + + $summary = array(); + + $image_styles = image_style_options(FALSE); + // Unset possible 'No defined styles' option. + unset($image_styles['']); + // Styles could be lost because of enabled/disabled modules that defines + // their styles in code. + if (isset($image_styles[$settings['image_style']])) { + $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']])); + } + else { + $summary[] = t('Original image'); + } + + $link_types = array( + 'content' => t('Linked to content'), + 'file' => t('Linked to file'), + ); + // Display this setting only if image is linked. + if (isset($link_types[$settings['image_link']])) { + $summary[] = $link_types[$settings['image_link']]; + } + + return implode('
', $summary); +} + +/** + * Implements hook_field_formatter_view(). + */ +function videoupload_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $element = array(); + + // Check if the formatter involves a link. + if ($display['settings']['image_link'] == 'content') { + $uri = entity_uri($entity_type, $entity); + } + elseif ($display['settings']['image_link'] == 'file') { + $link_file = TRUE; + } + + foreach ($items as $delta => $item) { + if (isset($link_file)) { + $uri = array( + 'path' => file_create_url($item['uri']), + 'options' => array(), + ); + } + $element[$delta] = array( + '#theme' => 'image_formatter', + '#item' => $item, + '#image_style' => $display['settings']['image_style'], + '#path' => isset($uri) ? $uri : '', + ); + } + + return $element; +} + +/** + * Returns HTML for an image field formatter. + * + * @param $variables + * An associative array containing: + * - item: An array of image data. + * - image_style: An optional image style. + * - path: An array containing the link 'path' and link 'options'. + * + * @ingroup themeable + */ +function theme_videoupload_formatter($variables) { + $item = $variables['item']; + $image = array( + 'path' => $item['uri'], + 'alt' => $item['alt'], + ); + // Do not output an empty 'title' attribute. + if (drupal_strlen($item['title']) > 0) { + $image['title'] = $item['title']; + } + + if ($variables['image_style']) { + $image['style_name'] = $variables['image_style']; + $output = theme('image_style', $image); + } + else { + $output = theme('image', $image); + } + + if ($variables['path']) { + $path = $variables['path']['path']; + $options = $variables['path']['options']; + // When displaying an image inside a link, the html option must be TRUE. + $options['html'] = TRUE; + $output = l($output, $path, $options); + } -?> + return $output; +} diff --git a/types/videoupload/videoupload.info b/types/videoupload/videoupload.info index 3c5e92d..f3b3405 100644 --- a/types/videoupload/videoupload.info +++ b/types/videoupload/videoupload.info @@ -5,6 +5,6 @@ description = Handle video upload for video module using filefield and CCK. package = Video core = 7.x dependencies[] = video -files[] = uploadfield.module -files[] = uploadfield.field.inc -files[] = uploadfield.install \ No newline at end of file +files[] = videoupload.module +files[] = videoupload.field.inc +files[] = videoupload.install \ No newline at end of file diff --git a/types/videoupload/videoupload.module b/types/videoupload/videoupload.module index 9664e1e..b482c67 100644 --- a/types/videoupload/videoupload.module +++ b/types/videoupload/videoupload.module @@ -6,51 +6,36 @@ * uploadfield core hooks and menu callbacks. */ -module_load_include('inc', 'uploadfield', 'uploadfield_widget'); +module_load_include('inc', 'videoupload', 'videoupload.field'); /** * Implementation of hook_perm(). */ -function uploadfield_perm() { +function videoupload_perm() { return array('use default thumbnail', 'bypass conversion video'); } /** * Implementation of hook_theme(). */ -function uploadfield_theme() { +function videoupload_theme() { $theme = array(); $theme['uploadfield_widget'] = array( 'arguments' => array('element' => NULL), - 'file' => 'uploadfield.theme.inc', + 'file' => 'videoupload.theme.inc', ); $theme['uploadfield_widget_item'] = array( 'arguments' => array('element' => NULL), - 'file' => 'uploadfield.theme.inc', + 'file' => 'videoupload.theme.inc', ); return $theme; } -/** - * Implementation of CCK's hook_widget_info(). - */ -function uploadfield_widget_info() { - return array( - 'uploadfield_widget' => array( - 'label' => t('Video'), - 'field types' => array('filefield'), - 'multiple values' => CONTENT_HANDLE_CORE, - 'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM), - 'description' => t('An edit widget for video files, including video thumbnails and transcoding to flash.'), - ), - ); -} - /** * Implementation of hook_elements(). */ -function uploadfield_elements() { +function videoupload_elements() { $elements = array(); // An uploadfield is really just a FileField with extra processing. $filefield_elements = module_invoke('filefield', 'elements'); @@ -65,7 +50,7 @@ function uploadfield_elements() { /** * Implementation of hook_file_download. */ -function uploadfield_file_download($filepath) { +function videoupload_file_download($filepath) { // Return headers for default images. if (strpos($filepath, 'video_thumbs') !== FALSE) { $full_path = file_create_path($filepath); @@ -81,7 +66,7 @@ function uploadfield_file_download($filepath) { /** * Implementation of CCK's hook_widget_settings(). */ -function uploadfield_widget_settings($op, $widget) { +function videoupload_widget_settings($op, $widget) { switch ($op) { case 'form': return uploadfield_widget_settings_form($widget); @@ -95,7 +80,7 @@ function uploadfield_widget_settings($op, $widget) { /** * Implementation of hook_widget(). */ -function uploadfield_widget(&$form, &$form_state, $field, $items, $delta = NULL) { +function videoupload_widget(&$form, &$form_state, $field, $items, $delta = NULL) { $item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => '', 'video_thumb' => '')); if (isset($items[$delta])) { $item = array_merge($item, $items[$delta]); @@ -106,7 +91,7 @@ function uploadfield_widget(&$form, &$form_state, $field, $items, $delta = NULL) /** * Implementation of CCK's hook_default_value(). */ -function uploadfield_default_value(&$form, &$form_state, $field, $delta) { +function videoupload_default_value(&$form, &$form_state, $field, $delta) { return filefield_default_value($form, $form_state, $field, $delta); } @@ -115,14 +100,14 @@ function uploadfield_default_value(&$form, &$form_state, $field, $delta) { * * Modify the add new field form to make "Video" the default formatter. */ -function uploadfield_form_content_field_overview_form_alter(&$form, &$form_state) { +function videoupload_form_content_field_overview_form_alter(&$form, &$form_state) { $form['#submit'][] = 'uploadfield_form_content_field_overview_submit'; } /** * Submit handler to set a new field's formatter to "video_plain". */ -function uploadfield_form_content_field_overview_submit(&$form, &$form_state) { +function videoupload_form_content_field_overview_submit(&$form, &$form_state) { if (isset($form_state['fields_added']['_add_new_field']) && isset($form['#type_name'])) { $new_field = $form_state['fields_added']['_add_new_field']; $node_type = $form['#type_name']; @@ -141,6 +126,6 @@ function uploadfield_form_content_field_overview_submit(&$form, &$form_state) { /** * filefield source support */ -function uploadfield_filefield_sources_widgets() { - return array('uploadfield_widget'); +function videoupload_filefield_sources_widgets() { + return array('videoupload_widget'); } \ No newline at end of file diff --git a/video.info b/video.info index c18d370..43a878d 100644 --- a/video.info +++ b/video.info @@ -4,6 +4,7 @@ name = Video description = Implementation of a video field package = Video core = 7.x +dependencies[] = file files[] = video.module files[] = video.admin.inc files[] = video.drush.inc -- cgit v1.2.3