function atom_reference_field_widget_form in Scald: Media Management made easy 7
Implements hook_field_widget_form.
File
- modules/
fields/ atom_reference/ atom_reference.module, line 303 - Defines a new field type, allowing to directly reference Scald Atoms from a node.
Code
function atom_reference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$all = scald_types();
$options = array();
$types = atom_reference_field_referenceable_types($instance);
foreach ($types as $name => $value) {
if ($value && isset($all[$name])) {
$options[$name] = $all[$name]->title;
}
}
$help = format_plural(count($options), 'Allowed resource format: %types', 'Allowed resource formats: %types', array(
'%types' => implode(', ', $options),
));
$preview_context = $instance['widget']['settings']['context'];
$element['#description'] .= ' ' . $help;
$element['#type'] = 'textfield';
$element['#attributes'] = array(
'data-types' => implode(',', array_keys($options)),
'data-dnd-context' => $preview_context,
);
$element['#default_value'] = isset($items[$delta]) ? $items[$delta]['sid'] : '';
$element['#preview_context'] = $preview_context;
$element['#process'][] = 'atom_reference_field_widget_form_process';
$element['#attached'] = array(
'library' => array(
array(
'atom_reference',
'library',
),
),
);
$options = array();
if (!empty($items[$delta]['options'])) {
$options = unserialize($items[$delta]['options']);
}
$return = array(
'sid' => $element,
);
if (isset($instance['settings']['allow_override']) && $instance['settings']['allow_override']) {
$rendering_context = 'use_the_default';
if (!empty($options['context'])) {
$rendering_context = $options['context'];
}
$context_element = array(
'#type' => 'select',
'#title' => t('Representation context'),
'#attributes' => array(
'class' => array(
'context-select',
),
),
'#options' => array(),
'#default_value' => $rendering_context,
'#description' => t('Scald rendering context used in field display.'),
);
$context_element['#weight'] = 20;
$contexts = scald_contexts_public();
$context_element['#options']['use_the_default'] = t('Use the default');
foreach ($contexts as $name => $context) {
if ($name !== 'sdl_library_item') {
$context_element['#options'][$name] = $context['title'];
}
}
$return['context'] = $context_element;
}
return $return;
}