media.fields.inc

  1. cis7 sites/all/modules/ulmus/media/includes/media.fields.inc
  2. cle7 sites/all/modules/ulmus/media/includes/media.fields.inc
  3. ecd7 sites/all/modules/ulmus/media/includes/media.fields.inc
  4. elmsmedia7 sites/all/modules/ulmus/media/includes/media.fields.inc
  5. harmony7 sites/all/modules/ulmus/media/includes/media.fields.inc
  6. icor7 sites/all/modules/ulmus/media/includes/media.fields.inc
  7. meedjum_blog7 sites/all/modules/ulmus/media/includes/media.fields.inc
  8. mooc7 sites/all/modules/ulmus/media/includes/media.fields.inc

Provide the media file selector widget and media field formatters to the Fields API.

Functions

File

sites/all/modules/ulmus/media/includes/media.fields.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provide the media file selector widget and media field formatters to the
  5. * Fields API.
  6. */
  7. /**
  8. * Implements hook_field_widget_info().
  9. */
  10. function media_field_widget_info() {
  11. return array(
  12. 'media_generic' => array(
  13. 'label' => t('Media file selector'),
  14. 'field types' => array('file', 'image'),
  15. 'settings' => array(
  16. 'progress_indicator' => 'throbber',
  17. 'allowed_types' => array('image'),
  18. 'browser_plugins' => array(),
  19. 'allowed_schemes' => array('public', 'private'),
  20. ),
  21. 'behaviors' => array(
  22. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  23. 'default value' => FIELD_BEHAVIOR_NONE,
  24. ),
  25. ),
  26. );
  27. }
  28. /**
  29. * Implements hook_field_formatter_info().
  30. *
  31. * Provides legacy support for the "Large filetype icon" file field formatter.
  32. * This was originally used when media entities contained file fields. The
  33. * current file entity architecture no longer needs this, but people may
  34. * have used this formatter for other file fields on their website.
  35. *
  36. * @todo Some day, remove this.
  37. */
  38. function media_field_formatter_info() {
  39. $formatters = array(
  40. 'media_large_icon' => array(
  41. 'label' => t('Large filetype icon'),
  42. 'field types' => array('file'),
  43. ),
  44. );
  45. return $formatters;
  46. }
  47. /**
  48. * Implements hook_field_formatter_view().
  49. *
  50. * Legacy support for the "Large filetype icon" file field formatter.
  51. * @see media_field_formatter_info()
  52. */
  53. function media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  54. $element = array();
  55. if ($display['type'] == 'media_large_icon') {
  56. // Use the media_thumbnail image style so that the output in media browser
  57. // is consistent.
  58. foreach ($items as $delta => $item) {
  59. $element[$delta] = array(
  60. '#theme' => 'media_formatter_large_icon',
  61. '#file' => (object) $item,
  62. );
  63. }
  64. }
  65. return $element;
  66. }
  67. /**
  68. * Implements hook_field_widget_settings_form().
  69. */
  70. function media_field_widget_settings_form($field, $instance) {
  71. $widget = $instance['widget'];
  72. $settings = $widget['settings'];
  73. $form = array();
  74. $streams = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
  75. $form['allowed_types'] = array(
  76. '#type' => 'checkboxes',
  77. '#title' => t('Allowed remote media types'),
  78. '#options' => file_entity_type_get_names(),
  79. '#default_value' => $settings['allowed_types'],
  80. '#description' => t('Media types which are allowed for this field when using remote streams.'),
  81. '#weight' => 1,
  82. '#access' => count(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE | STREAM_WRAPPERS_LOCAL)) != count($streams),
  83. );
  84. $options = array();
  85. foreach ($streams as $scheme => $data) {
  86. $options[$scheme] = t('@scheme (@name)', array('@scheme' => $scheme . '://', '@name' => $data['name']));
  87. }
  88. $form['allowed_schemes'] = array(
  89. '#type' => 'checkboxes',
  90. '#title' => t('Allowed URI schemes'),
  91. '#options' => $options,
  92. '#default_value' => $settings['allowed_schemes'],
  93. '#description' => t('URI schemes include public:// and private:// which are the Drupal files directories, and may also refer to remote sites.'),
  94. '#weight' => 2,
  95. );
  96. $plugins = media_get_browser_plugin_info();
  97. $form['browser_plugins'] = array(
  98. '#type' => 'checkboxes',
  99. '#title' => t('Enabled browser plugins'),
  100. '#options' => array(),
  101. '#default_value' => $settings['browser_plugins'],
  102. '#description' => t('If no plugins are selected, they will all be available.'),
  103. );
  104. foreach ($plugins as $key => $plugin) {
  105. $form['browser_plugins']['#options'][$key] = !empty($plugin['title']) ? $plugin['title'] : $key;
  106. }
  107. return $form;
  108. }
  109. /**
  110. * Implements hook_field_widget_form().
  111. */
  112. function media_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  113. $field_settings = $instance['settings'];
  114. $widget_settings = $instance['widget']['settings'];
  115. // @todo The Field API supports automatic serialization / unserialization, so
  116. // this should no longer be needed. After verifying with a module that uses
  117. // the 'data' column, remove this.
  118. // @see media_field_widget_value()
  119. $current_value = array();
  120. if (isset($items[$delta])) {
  121. $current_value = $items[$delta];
  122. // @todo $items[$delta] is sometimes a loaded media entity (an object)
  123. // rather than an array. This conflicts with Field API expectations (for
  124. // example, it results in fatal errors when previewing a node with a
  125. // multi-valued media field), so should be fixed. In the meantime, don't
  126. // assume that $current_value is an array.
  127. if (is_array($current_value) && isset($current_value['data']) && is_string($current_value['data'])) {
  128. $current_value['data'] = unserialize($current_value['data']);
  129. }
  130. }
  131. $element += array(
  132. // @todo This should be a fieldset, but throws a warning about
  133. // element_children.
  134. '#type' => 'media',
  135. '#collapsed' => TRUE,
  136. '#default_value' => $current_value,
  137. '#required' => $instance['required'],
  138. '#media_options' => array(
  139. 'global' => array(
  140. 'types' => array_filter($widget_settings['allowed_types']),
  141. 'enabledPlugins' => array_filter($instance['widget']['settings']['browser_plugins']),
  142. 'schemes' => $widget_settings['allowed_schemes'],
  143. 'file_directory' => isset($field_settings['file_directory']) ? $field_settings['file_directory'] : '',
  144. 'file_extensions' => isset($field_settings['file_extensions']) ? $field_settings['file_extensions'] : variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'),
  145. 'max_filesize' => isset($field_settings['max_filesize']) ? $field_settings['max_filesize'] : 0,
  146. 'uri_scheme' => !empty($field['settings']['uri_scheme']) ? $field['settings']['uri_scheme'] : file_default_scheme(),
  147. ),
  148. ),
  149. );
  150. if ($field['cardinality'] != 1) {
  151. $element['#title'] = check_plain($instance['label']);
  152. $element['#title_display'] = 'invisible';
  153. }
  154. if ($field['type'] == 'file') {
  155. $element['display'] = array(
  156. '#type' => 'value',
  157. '#value' => 1,
  158. );
  159. }
  160. // Add image field specific validators.
  161. if ($field['type'] == 'image') {
  162. if ($field_settings['min_resolution'] || $field_settings['max_resolution']) {
  163. $element['#media_options']['global']['min_resolution'] = $field_settings['min_resolution'];
  164. $element['#media_options']['global']['max_resolution'] = $field_settings['max_resolution'];
  165. }
  166. }
  167. return $element;
  168. }
  169. /**
  170. * Widget value.
  171. *
  172. * @todo Is this function ever called? If not, remove it. The Field API now
  173. * supports automatic serialization / unserialization, so this should no
  174. * longer be needed. After verifying with a module that uses the 'data'
  175. * column, remove this.
  176. *
  177. * @see media_field_widget_form()
  178. */
  179. function media_field_widget_value($element, $input, $form_state) {
  180. $return = $input;
  181. if (!is_array($return)) {
  182. $return = array();
  183. }
  184. if (isset($return['data'])) {
  185. $return['data'] = serialize($return['data']);
  186. }
  187. $return += array(
  188. 'fid' => 0,
  189. 'title' => '',
  190. 'data' => NULL,
  191. );
  192. return $return;
  193. }