function media_wysiwyg_format_form in D7 Media 7.4
Same name and namespace in other branches
- 7.2 modules/media_wysiwyg/includes/media_wysiwyg.pages.inc \media_wysiwyg_format_form()
- 7.3 modules/media_wysiwyg/includes/media_wysiwyg.pages.inc \media_wysiwyg_format_form()
Form callback for embedding media.
Allows the user to pick a view mode (format) and various other settings for a media instance. The values from this form follows the media token schema and its values are mapped directly to media tokens.
1 string reference to 'media_wysiwyg_format_form'
- media_wysiwyg_menu in modules/
media_wysiwyg/ media_wysiwyg.module - Implements hook_menu().
File
- modules/
media_wysiwyg/ includes/ media_wysiwyg.pages.inc, line 15 - Common pages for the Media WYSIWYG module.
Code
function media_wysiwyg_format_form($form, &$form_state, $file) {
$form_state['file'] = $file;
// Build instance settings from token schema defaults and anything from query
// parameters (ajax).
$token_schema = media_wysiwyg_schema_token();
$instance_settings = array_map(function ($property) {
return $property['default'];
}, $token_schema);
if (isset($_GET['instance_settings'])) {
$instance_settings = array_merge($instance_settings, drupal_json_decode($_GET['instance_settings']));
}
// For new media instances this array is the entire file entity form,
// including file type ('type'), which conflict with the 'type' property of
// our media token. Adjust our settings to adhere to the media token schema.
$instance_settings['type'] = 'media';
$instance_settings['fid'] = $file->fid;
$instance_settings['file_type'] = $file->type;
// Build view mode options, assert that we have a valid view mode in instance
// settings and build a rendered preview of each one of them.
$view_mode_options = media_wysiwyg_get_file_view_mode_options($file);
if (!count($view_mode_options)) {
throw new Exception('Unable to continue, no available formats for displaying media.');
}
if (!isset($view_mode_options[$instance_settings['view_mode']])) {
reset($view_mode_options);
$instance_settings['view_mode'] = key($view_mode_options);
}
if (!isset($form_state['storage']['view_mode_previews'])) {
$form_state['storage']['view_mode_previews'] = array();
foreach ($view_mode_options as $view_mode => $view_mode_label) {
$view_mode_preview = media_wysiwyg_get_file_without_label($file, $view_mode, array(
'wysiwyg' => TRUE,
));
$form_state['storage']['view_mode_previews'][$view_mode] = drupal_render($view_mode_preview);
}
}
// Add the previews by reference so that they can easily be altered by
// changing $form['#formats'].
$form['#formats'] =& $form_state['storage']['view_mode_previews'];
// Add the required libraries, JavaScript, settings and CSS for the form.
$us = drupal_get_path('module', 'media_wysiwyg');
$js_settings = array(
'media' => array(
'formatFormFormats' => &$form_state['storage']['view_mode_previews'],
),
);
$form['#attached']['library'][] = array(
'media',
'media_base',
);
$form['#attached']['library'][] = array(
'system',
'form',
);
$form['#attached']['css'][] = $us . '/css/media_wysiwyg.css';
$form['#attached']['js'][] = $us . '/js/media_wysiwyg.format_form.js';
$form['#attached']['js'][] = array(
'data' => $js_settings,
'type' => 'setting',
);
$form['title'] = array(
'#markup' => t('Embedding %filename', array(
'%filename' => $file->filename,
)),
);
$preview = media_get_thumbnail_preview($file);
$form['preview'] = array(
'#type' => 'markup',
'#markup' => drupal_render($preview),
);
// Everything under $form['options'] will get passed to the WYSIWYG
// environment and used as media instance settings which in turn are
// serialized to actual media tokens.
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('options'),
);
// Add the non-editable variables of media tokens.
foreach (array(
'type',
'fid',
'file_type',
) as $property) {
$form['options'][$property] = array(
'#type' => 'hidden',
'#value' => $instance_settings[$property],
);
}
// @TODO: Display more verbose information about which formatter and what it
// does.
$form['options']['view_mode'] = array(
'#type' => 'select',
'#title' => t('Display as'),
'#options' => $view_mode_options,
'#default_value' => $instance_settings['view_mode'],
'#description' => t('Choose the type of display you would like for this
file. Please be aware that files may display differently than they do when
they are inserted into an editor.'),
);
if (variable_get('media_wysiwyg_alignment', FALSE)) {
$form['options']['alignment'] = array(
'#type' => 'select',
'#title' => t('Alignment'),
'#options' => $token_schema['alignment']['options'],
'#description' => t('Choose how you would like the media to be aligned with surrounding content.'),
'#default_value' => $instance_settings['alignment'],
);
}
$form['options']['instance_fields'] = array(
'#type' => 'radios',
'#title' => t("Instance field values"),
'#options' => $token_schema['instance_fields']['options'],
'#description' => t("<em>Global</em> will use the values from the file itself. <em>Override</em> will set custom values for this instance only, based on the file's global field values.<br /><strong>Be aware:</strong> Using <em>Global</em> will cause later edits on the file will affect this instance as well."),
'#default_value' => $instance_settings['instance_fields'],
);
// Add fields from the file, so that we can override them if necessary.
$fields = media_wysiwyg_filter_field_parser($instance_settings, $file);
foreach ($fields as $field_name => $field_value) {
$file->{$field_name} = $field_value;
}
$form['options']['fields'] = array();
field_attach_form('file', $file, $form['options']['fields'], $form_state);
foreach (field_info_instances('file', $file->type) as $field_name => $field_instance) {
$info = field_info_field($field_name);
$allow = !empty($field_instance['settings']['wysiwyg_override']);
// Only single valued fields can be overridden normally, unless Media is
// configured otherwise with "media_wysiwyg_wysiwyg_override_multivalue".
if ($allow && $info['cardinality'] != 1) {
$allow = variable_get('media_wysiwyg_wysiwyg_override_multivalue', FALSE);
}
$form['options']['fields'][$field_name]['#access'] = $allow;
if ($allow) {
$form['options']['fields'][$field_name]['#states'] = array(
'visible' => array(
':input[name="instance_fields"]' => array(
'value' => "override",
),
),
);
}
}
// Field to attach external url's to files for linking.
if (variable_get('media_wysiwyg_external_link', FALSE)) {
if ($file->type == 'image') {
$form['options']['external_url'] = array(
'#type' => 'textfield',
'#title' => t('Link Image'),
'#description' => t('Enter a URL to turn the image into a link.'),
'#maxlength' => 2048,
'#default_value' => $instance_settings['external_url'],
);
}
}
// Add view mode preview.
media_wysiwyg_format_form_view_mode($form, $form_state, $file, $instance_settings);
// Similar to a form_alter, but we want this to run first so that
// media.types.inc can add the fields specific to a given type (like alt tags
// on media). If implemented as an alter, this might not happen, making other
// alters not be able to work on those fields.
// @todo: We need to pass in existing values for those attributes.
drupal_alter('media_wysiwyg_format_form_prepare', $form, $form_state, $file);
return $form;
}