function _imagefield_widget_form in ImageField 5.2
Same name and namespace in other branches
- 5 imagefield.module \_imagefield_widget_form()
2 calls to _imagefield_widget_form()
- imagefield_js in ./
imagefield.module - Menu-callback for JavaScript-based uploads.
- imagefield_widget in ./
imagefield.module - Implementation of hook_widget().
File
- ./
imagefield.module, line 666 - Defines an image field type. imagefield uses content.module to store the fid, and the drupal files table to store the actual file data.
Code
function _imagefield_widget_form($node, $field, &$items) {
drupal_add_js('misc/progress.js');
drupal_add_js('misc/upload.js');
drupal_add_js(drupal_get_path('module', 'imagefield') . '/imagefield.js');
drupal_add_css(drupal_get_path('module', 'imagefield') . '/imagefield.css');
$fieldname = $field['field_name'];
$form = array();
$form[$fieldname] = array(
'#type' => 'fieldset',
'#title' => t($field['widget']['label']),
'#weight' => $field['widget']['weight'],
'#description' => t('Images are not saved until the form is submitted.'),
'#collapsible' => true,
'#collapsed' => false,
'#tree' => true,
'#prefix' => '<div id="' . form_clean_id($fieldname . '-attach-wrapper') . '">',
'#suffix' => '</div>',
);
$form[$fieldname]['new'] = array(
'#tree' => false,
'#prefix' => '<div id="' . form_clean_id($fieldname . '-attach-hide') . '">',
'#suffix' => '</div>',
'#weight' => 100,
);
$max_images = $field['widget']['max_number_images'];
if ($field['multiple'] && $max_images && $max_images <= count($items)) {
$form[$fieldname]['#prefix'] = '<div>';
$form[$fieldname]['new']['#prefix'] = '<div>';
$form[$fieldname]['new']['#value'] = format_plural($max_images, 'You can only attach one image to this field. Delete the image if you wish to be able to upload a different one.', 'You can only attach @count images to this field. Delete an image if you wish to be able to upload different images.');
}
else {
// multiupload: add an add new field button.
// onchange ajax upload each file input after validation.
// update date 'file_inputs' with qty of file inputs.
// name file inputs $fieldname .'_upload_'. number of file input.
// inprepare form_values hook iterate over each input.
// can we display files sizes to predict the total size of an upload.
$extensions_description = empty($field['widget']['file_extensions']) ? '' : t('<br />Allowed extensions: %ext', array(
'%ext' => $field['widget']['file_extensions'],
));
// Seperate from tree becase of that silly things won't be
// displayed if they are a child of '#type' = form issue
$form[$fieldname]['new'][$fieldname . '_upload'] = array(
'#type' => 'file',
'#title' => t('Upload a new image'),
'#description' => $field['widget']['description'] . $extensions_description,
'#tree' => false,
'#weight' => 9,
'#attributes' => array(
'class' => 'imagefield imagefield-' . $fieldname,
'accept' => str_replace(' ', ',', trim($field['widget']['file_extensions'])),
),
);
$form[$fieldname]['new']['upload'] = array(
'#type' => 'button',
'#value' => t('Upload'),
'#name' => 'cck_imagefield_' . $fieldname . '_op',
'#id' => form_clean_id($fieldname . '-attach-button'),
'#tree' => false,
'#weight' => 10,
);
// The class triggers the js upload behaviour.
$form[$fieldname . '-attach-url'] = array(
'#type' => 'hidden',
'#value' => url('imagefield/js', null, null, true),
'#attributes' => array(
'class' => 'upload',
),
);
}
// @todo split following if block into its own function.
// Store the file data object to be carried on.
if (!empty($items)) {
foreach ($items as $delta => $file) {
if ($file['filepath'] && !$file['flags']['hidden']) {
$form[$fieldname][$delta] = array(
'#theme' => 'imagefield_edit_image_row',
);
$form[$fieldname][$delta]['flags']['delete'] = array(
'#type' => 'checkbox',
'#title' => t('Delete'),
'#default_value' => isset($file['flags']['delete']) ? $file['flags']['delete'] : 0,
);
if (function_exists('token_replace')) {
global $user;
$filename = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path(token_replace($field['widget']['image_path'], 'user', $user))) : $file['filepath'];
}
else {
$filename = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path($field['widget']['image_path'])) : $file['filepath'];
}
$form[$fieldname][$delta]['admin_preview'] = array(
'#type' => 'markup',
'#value' => theme('imagefield_image', $file, $file['alt'], $file['title'], array(
'width' => '150',
), false),
);
$form[$fieldname][$delta]['description'] = array(
'#type' => 'markup',
'#value' => '<strong>' . t('Filename: ') . '</strong>' . $file['filename'],
);
$form[$fieldname][$delta]['alt'] = array(
'#type' => 'hidden',
'#value' => $file['filename'],
);
// overwrite with an input field if custom_alt is flagged;
if ($field['widget']['custom_alt']) {
$form[$fieldname][$delta]['alt'] = array(
'#type' => 'textfield',
'#title' => t('Alternate text'),
'#default_value' => $file['alt'],
'#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
'#maxlength' => 255,
'#size' => 10,
);
}
$form[$fieldname][$delta]['title'] = array(
'#type' => 'hidden',
'#value' => $file['filename'],
);
// overwrite with an input field if custom_title is flagged;
if ($field['widget']['custom_title']) {
$form[$fieldname][$delta]['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $file['title'],
'#description' => t('Text to be displayed on mouse overs.'),
'#maxlength' => 255,
'#size' => 10,
);
}
// Special handling for single value fields
if (!$field['multiple']) {
$form[$fieldname][$delta]['replace'] = array(
'#type' => 'markup',
'#value' => t('If a new image is chosen, the current image will be replaced upon submitting the form.'),
);
}
}
elseif ($file['filepath'] && $file['flags']['hidden']) {
// Render all the form values of this item if it is hidden.
$form[$fieldname][$delta]['flags']['hidden'] = array(
'#type' => 'value',
'#value' => $file['flags']['hidden'],
);
$form[$fieldname][$delta]['flags']['delete'] = array(
'#type' => 'value',
'#value' => $file['flags']['delete'],
);
$form[$fieldname][$delta]['title'] = array(
'#type' => 'value',
'#value' => $file['title'],
);
$form[$fieldname][$delta]['alt'] = array(
'#type' => 'value',
'#value' => $file['alt'],
);
}
if (isset($file['sessionid'])) {
$form[$fieldname][$delta]['sessionid'] = array(
'#type' => 'value',
'#value' => $file['sessionid'],
);
}
$form[$fieldname][$delta]['filename'] = array(
'#type' => 'value',
'#value' => $file['filename'],
);
$form[$fieldname][$delta]['filepath'] = array(
'#type' => 'value',
'#value' => $file['filepath'],
);
$form[$fieldname][$delta]['preview'] = array(
'#type' => 'value',
'#value' => $file['preview'],
);
$form[$fieldname][$delta]['filemime'] = array(
'#type' => 'value',
'#value' => $file['filemime'],
);
$form[$fieldname][$delta]['filesize'] = array(
'#type' => 'value',
'#value' => $file['filesize'],
);
$form[$fieldname][$delta]['fid'] = array(
'#type' => 'value',
'#value' => $file['fid'],
);
}
}
// Some useful info for our js callback.
$form['vid'] = array(
'#type' => 'hidden',
'#value' => $node->vid,
'#tree' => false,
);
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $node->nid,
'#tree' => false,
);
$form['type'] = array(
'#type' => 'hidden',
'#value' => $node->type,
'#tree' => false,
);
return $form;
}