function media_field_widget_form in D7 Media 7.2
Same name and namespace in other branches
- 7.4 includes/media.fields.inc \media_field_widget_form()
- 7 includes/media.fields.inc \media_field_widget_form()
- 7.3 includes/media.fields.inc \media_field_widget_form()
Implements hook_field_widget_form().
File
- includes/
media.fields.inc, line 82 - Provide media selector widget and media field formatters to the fields API.
Code
function media_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$defaults = array(
'fid' => 0,
'display' => !empty($field['settings']['display_default']),
'description' => '',
);
// Load the items for form rebuilds from the field state as they might not be
// in $form_state['values'] because of validation limitations. Also, they are
// only passed in as $items when editing existing entities.
$field_state = field_form_get_state($element['#field_parents'], $field['field_name'], $langcode, $form_state);
if (isset($field_state['items'])) {
$items = $field_state['items'];
}
$field_settings = $instance['settings'];
$widget_settings = $instance['widget']['settings'];
// Essentially we use the media type, extended with some enhancements.
$element_info = element_info('media');
$multiselect = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
$element += array(
'#type' => 'media',
'#value_callback' => 'media_field_widget_value',
'#process' => array_merge($element_info['#process'], array(
'media_field_widget_process',
)),
'#media_options' => array(
'global' => array(
'types' => array_filter($widget_settings['allowed_types']),
'enabledPlugins' => array_filter($instance['widget']['settings']['browser_plugins']),
'schemes' => array_filter($widget_settings['allowed_schemes']),
'file_directory' => isset($field_settings['file_directory']) ? $field_settings['file_directory'] : '',
'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'),
'max_filesize' => isset($field_settings['max_filesize']) ? $field_settings['max_filesize'] : 0,
'uri_scheme' => !empty($field['settings']['uri_scheme']) ? $field['settings']['uri_scheme'] : file_default_scheme(),
'multiselect' => $multiselect,
'field' => $field['field_name'],
),
),
// Allows this field to return an array instead of a single value.
'#extended' => TRUE,
);
// If translation is enabled for the entity, store its form/source langcodes
// on the elements for further usage in media_element_process().
if (module_invoke('entity_translation', 'enabled', $element['#entity_type'], $element['#entity'])) {
$translation_handler = entity_translation_get_handler($element['#entity_type'], $element['#entity']);
if ($translation_handler) {
$element['#media_parent_entity_form_langcode'] = $translation_handler
->getActiveLanguage();
if ($source_langcode = $translation_handler
->getSourceLanguage()) {
$element['#media_parent_entity_source_langcode'] = $source_langcode;
}
}
}
elseif (module_exists('translation') && $element['#entity_type'] == 'node' && translation_supported_type($element['#entity']->type)) {
$element['#media_parent_entity_form_langcode'] = $element['#entity']->language;
$element['#media_parent_entity_source_langcode'] = $element['#entity']->language;
}
elseif ($element['#entity_type'] == 'field_collection_item' && !empty($form['#entity']) && property_exists($form['#entity'], 'language')) {
$element['#media_parent_entity_form_langcode'] = $form['#entity']->language;
}
elseif ($element['#entity_type'] == 'paragraphs_item' && !empty($form['#entity'])) {
$host = $element['#entity']
->hostEntity();
if (isset($host->language)) {
$element['#media_parent_entity_form_langcode'] = $host->language;
$element['#media_parent_entity_source_langcode'] = $host->language;
}
}
// Add image field specific validators.
if ($field['type'] == 'image') {
if ($field_settings['min_resolution'] || $field_settings['max_resolution']) {
$element['#media_options']['global']['min_resolution'] = $field_settings['min_resolution'];
$element['#media_options']['global']['max_resolution'] = $field_settings['max_resolution'];
}
}
if ($field['cardinality'] == 1) {
// Set the default value.
$element['#default_value'] = !empty($items) ? $items[0] : $defaults;
// If there's only one field, return it as delta 0.
if (empty($element['#default_value']['fid'])) {
$element['#description'] = theme('media_upload_help', array(
'description' => $element['#description'],
));
}
$elements = array(
$element,
);
}
else {
// If there are multiple values, add an element for each existing one.
foreach ($items as $item) {
$elements[$delta] = $element;
$elements[$delta]['#default_value'] = $item;
$elements[$delta]['#weight'] = isset($item['_weight']) ? $item['_weight'] : $delta;
$delta++;
}
// And then add one more empty row for new uploads except when this is a
// programmed form as it is not necessary.
if (($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $field['cardinality']) && empty($form_state['programmed'])) {
$elements[$delta] = $element;
$elements[$delta]['#default_value'] = $defaults;
$elements[$delta]['#weight'] = $delta;
$elements[$delta]['#required'] = $element['#required'] && $delta == 0;
}
// The group of elements all-together need some extra functionality
// after building up the full list (like draggable table rows).
$elements['#file_upload_delta'] = $delta;
$elements['#theme'] = 'media_widget_multiple';
$elements['#theme_wrappers'] = array(
'fieldset',
);
$elements['#process'] = array(
'media_field_widget_process_multiple',
);
$elements['#title'] = $element['#title'];
$elements['#description'] = $element['#description'];
$elements['#field_name'] = $element['#field_name'];
$elements['#language'] = $element['#language'];
$elements['#display_field'] = intval(!empty($field['settings']['display_field']));
// Add some properties that will eventually be added to the media upload
// field. These are added here so that they may be referenced easily through
// a hook_form_alter().
$elements['#file_upload_title'] = t('Attach media');
$elements['#file_upload_description'] = theme('media_upload_help', array(
'description' => '',
));
}
return $elements;
}