Commit 7542905c authored by JacobSingh's avatar JacobSingh

Added ability for view_modes to be provided on install, made file_styles a...

Added ability for view_modes to be provided on install, made file_styles a dependency.  Also added test for the same, test currently fails because I can't deal with how slow the testing framework is and how hard it is to debug stuff...
parent ea255b00
......@@ -11,5 +11,8 @@ files[] = media.admin.inc
files[] = media.theme.inc
files[] = media.types.inc
files[] = MediaReadOnlyStreamWrapper.inc
; Tests
files[] = test/media.types.test
dependencies[] = file
dependencies[] = image
dependencies[] = file_styles
......@@ -51,6 +51,14 @@ function media_install() {
function media_enable() {
// @TODO: Revisit the default types to provide. For instance, PDFs and text.
// @TODO: We may need to disable the media bundle & field in hook_disable.
$view_mode_defaults = array(
'media_preview' => 'styles_file_square_thumbnail',
'original' => 'file_default',
'small' => 'styles_file_small',
'large' => 'styles_file_large',
);
$types = array();
// This is the default type which will be used if no other type is found
......@@ -59,12 +67,7 @@ function media_enable() {
$types['default']->label = "Other";
$types['default']->base = TRUE;
//@todo: I don't like this but we have to do something here.
$types['default']->view_mode_defaults = array(
'preview' => 'file_default',
'original' => 'file_default',
'small' => 'file_none',
'large' => 'file_none',
);
$types['default']->view_mode_defaults = $view_mode_defaults;
$types['default']->weight = 1000;
// This is the default.
......@@ -78,16 +81,7 @@ function media_enable() {
$types['image']->machine_name = 'image';
$types['image']->label = "Image";
$types['image']->base = TRUE;
$types['image']->view_mode_defaults = array(
'preview' => 'file_default',
'original' => 'file_default',
'small' => 'file_none',
'large' => 'file_none',
);
if (module_exists('file_styles')) {
// @TODO: Not sure why this placeholder is here.
}
$types['image']->view_mode_defaults = $view_mode_defaults;
$types['image']->type_callback_args =
array(
......@@ -101,12 +95,9 @@ function media_enable() {
$types['audio']->machine_name = 'audio';
$types['audio']->label = "Audio";
$types['audio']->base = TRUE;
$types['audio']->view_mode_defaults = array(
'preview' => 'file_default',
'original' => 'file_default',
'small' => 'file_none',
'large' => 'file_none',
);
// @todo: these are image specific, needs to change
$types['audio']->view_mode_defaults = $view_mode_defaults;
$types['audio']->type_callback_args = array(
'match_type' => 'all',
'mimetypes' => array('/^audio/'),
......@@ -118,12 +109,9 @@ function media_enable() {
$types['video']->machine_name = 'video';
$types['video']->label = "Video";
$types['video']->base = TRUE;
$types['video']->view_mode_defaults = array(
'preview' => 'file_default',
'original' => 'file_default',
'small' => 'file_none',
'large' => 'file_none',
);
// @todo: these are image specific, needs to change
$types['video']->view_mode_defaults = $view_mode_defaults;
$types['video']->callbacks = array();
// This is the default, otherwise could provide
......
......@@ -58,29 +58,59 @@ function media_entity_info() {
* The default media types are currently 'Audio', 'Image', 'Video', and
* 'Other', which are defined in media_install().
*
* @param object $type
* @param object &$type
* $type is an object with the following fields:
* ->machine_name => The name of the media asset type, such as 'video';
* ->label => The human readable name;
* ->base => @TODO: What's this?
* ->base => boolean: If the media type cannot be removed.
* ->type_callback => Function call to filter an instance to its bundle.
* ->type_callback_args => An array of args to be passed to type_callback.
* ->view_mode_defaults => An array of view_mode_key => format.
* @return void;
*/
function media_type_save($type) {
function media_type_save(&$type) {
$types = variable_get('media_types', array());
$type = media_type_set_defaults($type);
$types[$type->machine_name] = $type;
// Set up the defaults for this media type bundle.
media_type_configure_fields($type);
media_type_configure_formatters($type->machine_name, $type->view_mode_defaults);
// We don't need to save this information
unset($type->view_mode_defaults);
variable_set('media_types', $types);
// Clear the caches
drupal_static_reset('media_type_get_types');
drupal_static_reset('media_type_get_mime_map');
}
/**
* Assigns formatters to view_modes for the file field of a media type.
*
* Each media type contains a field called "file" and certain stock view modes.
* This function will assign a format to each view mode. For instance, images
* would want to use a thumbnail format for files when in the "preview"
* view_mode. The called would pass in 'image' for $machine_name and
* array('preview' => 'styles_file_square_thumbnail') to $view_modes_to_formatters.
*
* @param string $machine_name
* @param array $view_modes_to_formatters
* @throws Exception
* @return void
*/
function media_type_configure_formatters($machine_name, $view_modes_to_formatters) {
$instance = field_info_instance('media', 'file', $machine_name);
if (!$instance) {
throw new Exception('Unable to set formatter preferences for '. $machine_name);
}
foreach ($instance['display'] as $view_mode => $display) {
if (isset($view_modes_to_formatters[$view_mode])) {
$instance['display'][$view_mode]['type'] = $view_modes_to_formatters[$view_mode];
}
}
field_update_instance($instance);
}
/**
* Loads a media type based on its machine name.
*
......
<?php
// @Id;
/**
* @file
* Tests for media
*/
/**
* Test media type creation and managedment
*/
class MediaTypeTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Media types',
'description' => 'Tests media types',
'group' => 'Media',
);
}
function setUp() {
parent::setUp();
// Nice, TDD FTW. #totalsarcasm
variable_set('simpletest_verbose', TRUE);
}
private function createType($overrides) {
$type = new StdClass();
$type->machine_name = 'test';
$type->label = "Test";
$type->base = TRUE;
$type->view_mode_defaults = array(
'media_preview' => 'styles_file_square_thumbnail',
'media_original' => 'file_default',
);
$type->type_callback_args =
array(
'match_type' => 'all',
'mimetypes' => array('/^test/'),
'extensions' => array('jpg', 'jpeg', 'gif', 'png', 'tiff'),
'streams' => array('public', 'private'),
);
foreach ($overrides as $k => $v) {
$type->$k = $v;
}
media_type_save($type);
return $type;
}
/**
* Test creating a new type. Basic CRUD
*/
function testCreate() {
$type_machine_name = 'foo';
$type = $this->createType(array('machine_name' => $type_machine_name, 'label' => 'foobar'));
$loaded_type = media_type_load($type_machine_name);
$this->assertEqual($loaded_type->label, 'foobar');
}
/**
* Test view mode assignment. Currently fails, don't know why
* @return unknown_type
*/
function testViewModesAssigned() {
$view_modes = array(
'media_preview' => 'styles_file_square_thumbnail',
'media_original' => 'file_default',
);
$type_machine_name = 'foo';
$type = $this->createType(array('machine_name' => $type_machine_name, 'view_mode_defaults' => $view_modes));
$instance = field_info_instance('media', 'file', $type_machine_name);
foreach ($view_modes as $view_mode => $formatter) {
$this->assertEqual($instance['display'][$view_mode]['type'], $formatter, 'When viewed as ' . $media_preview . ' Should be shown as ' . $formatter .' actually set to ' . $instance['display'][$view_mode]['type']);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment