From f21288ad677b591bb55067be07e1e49dbfaade07 Mon Sep 17 00:00:00 2001
From: Tim Plunkett <git@plnktt.com>
Date: Sat, 3 Sep 2011 12:33:28 -0400
Subject: [PATCH] Issue #1268662 by tim.plunkett: Provide a Media component for Webform.

---
 components/media.inc |   83 ++++++++++++++++++++++++++++++++++++++++++++++++++
 webform.module       |   15 ++++++++-
 2 files changed, 97 insertions(+), 1 deletions(-)
 create mode 100644 components/media.inc

diff --git a/components/media.inc b/components/media.inc
new file mode 100644
index 0000000..1f9c580
--- /dev/null
+++ b/components/media.inc
@@ -0,0 +1,83 @@
+<?php
+
+/**
+ * @file
+ * Webform module media component.
+ */
+
+/**
+ * Implements _webform_defaults_component().
+ */
+function _webform_defaults_media() {
+  return array(
+    'name' => '',
+    'form_key' => NULL,
+    'mandatory' => 0,
+    'pid' => 0,
+    'weight' => 0,
+    'extra' => array(
+      'allowed_types' => array(),
+      'allowed_schemes' => array(),
+      'description' => '',
+      'private' => FALSE,
+    ),
+  );
+}
+
+/**
+ * Implements _webform_edit_component().
+ */
+function _webform_edit_media($component) {
+  $instance = array(
+    'widget' => array('settings' => $component['extra']),
+    'required' => $component['mandatory'],
+  );
+  $form['extra'] = media_field_widget_settings_form(NULL, $instance);
+  return $form;
+}
+
+/**
+ * Implements _webform_render_component().
+ */
+function _webform_render_media($component, $value = NULL, $filter = TRUE) {
+  $form = array();
+  $form_state = array();
+  $instance = array(
+    'widget' => array('settings' => $component['extra']),
+    'required' => $component['mandatory'],
+  );
+
+  $media = array(
+    '#title' => '',
+    '#required' => '',
+    '#description' => '',
+  );
+
+  $element = array(
+    'media' => media_field_widget_form($form, $form_state, NULL, $instance, NULL, NULL, NULL, $media),
+    '#webform_component' => $component,
+    '#weight' => $component['weight'],
+    '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
+    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
+    '#required' => (bool) $component['mandatory'],
+    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
+    '#theme_wrappers' => array('webform_element'),
+  );
+
+  return $element;
+}
+
+/**
+ * Implements _webform_submit_component().
+ */
+function _webform_submit_media($component, $value) {
+  return isset($value['media']) ? $value['media'] : NULL;
+}
+
+/**
+ * Implements _webform_display_component().
+ */
+function _webform_display_media($component, $value, $format = 'html') {
+  $fid = isset($value['fid']) ? $value['fid'] : NULL;
+  return file_view(file_load($fid));
+}
diff --git a/webform.module b/webform.module
index 23a35bd..9f3020a 100644
--- a/webform.module
+++ b/webform.module
@@ -713,7 +713,7 @@ function webform_element_info() {
  * Implements hook_webform_component_info().
  */
 function webform_webform_component_info() {
-  return array(
+  $components = array(
     'date' => array(
       'label' => t('Date'),
       'description' => t('Presents month, day, and year fields.'),
@@ -830,6 +830,19 @@ function webform_webform_component_info() {
       'file' => 'components/time.inc',
     ),
   );
+
+  if (module_exists('media')) {
+    $components['media'] = array(
+      'label' => t('Media'),
+      'description' => t('Media fields allow third-party media to be displayed.'),
+      'features' => array(
+        'conditional' => FALSE,
+      ),
+      'file' => 'components/media.inc',
+    );
+  }
+
+  return $components;
 }
 
 /**
-- 
1.7.3.2

