Needs work
Project:
Variable Extra
Version:
7.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
4 Nov 2013 at 11:06 UTC
Updated:
11 Apr 2014 at 18:02 UTC
Jump to comment: Most recent
It took a while before I figured out how to add a multilingual variable which stores a reference to a file entity, selected through the media element (as provided by Media module). Here is my first attempt, based on my experience with Media 7.x-1.3, Media Youtube 7.x-2.0-rc3 and Variable 7.x-2.3.
Example code:
/**
* Implements hook_variable_info().
*/
function mymodule_variable_info($options) {
$variables = array();
// An image.
$variables['mymodule_image'] = array(
'type' => 'array',
'group' => 'mymodule_variables',
'title' => t('Image', array(), $options),
'localize' => TRUE,
'required' => TRUE,
'default' => array(),
'element' => array(
'#type' => 'media',
'#media_options' => array(
'global' => array(
'types' => array('image'),
'schemes' => array(),
'file_extensions' => 'png jpg gif',
),
),
'#attributes' => array('class' => array('media-widget')),
),
);
// A Youtube video.
$variables['mymodule_youtube_video'] = array(
'type' => 'array',
'group' => 'mymodule_variables',
'title' => t('Youtube video', array(), $options),
'localize' => TRUE,
'required' => TRUE,
'default' => array(),
'element' => array(
'#type' => 'media',
'#media_options' => array(
'global' => array(
'types' => array('video'),
'schemes' => array('youtube'),
),
),
'#attributes' => array('class' => array('media-widget')),
),
);
return $variables;
}
Note how I explicitly add the 'media-widget' class, which is required for the media selector to work properly. Normally, this is automatically added by the media element, but if you do not add it explicitly in the variable info definition, it gets lost on localizable variables.
Hope this helps anyone.
Comments
Comment #1
jose reyero commentedNice, though this is not the best place for documenting.
Consider adding it to the module's documentation or adding an example module in http://drupal.org/project/variable_extra
(A module providing a 'media' variable type would be even better.)
Comment #2
jose reyero commentedMoving to Variable Extra