function _imagefield_crop_widget_form in Imagefield Crop 5
1 call to _imagefield_crop_widget_form()
- imagefield_crop_widget in ./
imagefield_crop.module - Implementation of hook_widget().
File
- ./
imagefield_crop.module, line 347
Code
function _imagefield_crop_widget_form($node, $field, &$node_field) {
$fieldname = $field['field_name'];
// REFACTOR: Do we need CSS file from imagefield.module?
drupal_add_css(drupal_get_path('module', 'imagefield') . '/imagefield.css');
$module_path = drupal_get_path('module', 'imagefield_crop');
drupal_add_css($module_path . '/imagefield_crop.css');
jquery_interface_add();
if ($field['widget']['enforce_ratio']) {
list($w, $h) = explode('x', $field['widget']['resolution']);
$ratio = $h / $w;
}
else {
$ratio = 0;
}
drupal_add_js("Drupal.imagefield_crop_{$fieldname} = { ratio: {$ratio} }", 'inline');
drupal_add_js($module_path . '/imagefield_crop.js');
$form = array();
$form[$fieldname] = array(
'#type' => 'fieldset',
'#title' => $field['widget']['label'],
'#weight' => $field['widget']['weight'],
'#collapsible' => $field['widget']['collapsible'] != 1,
'#collapsed' => $field['widget']['collapsible'] == 3,
'#tree' => TRUE,
);
// Seperate from tree becase of that silly things won't be
// displayed if they are a child of '#type' = form issue
$form[$fieldname][$fieldname . '_upload'] = array(
'#type' => 'file',
'#description' => $field['widget']['description'],
'#tree' => FALSE,
'#weight' => 9,
);
$form[$fieldname]['upload'] = array(
'#type' => 'button',
'#value' => t('Upload'),
'#name' => 'cck_imagefield_' . $fieldname . '_op',
// REFACTOR: cck_imagefield_crop_ ??
'#attributes' => array(
'id' => $fieldname . '-attach-button',
),
'#tree' => FALSE,
'#weight' => 10,
);
// Store the file data object to be carried on.
list($w, $h) = explode('x', $field['widget']['resolution']);
if (is_array($node_field) && count($node_field)) {
$crop_attrs = array(
// REFACTOR: Are weights necessary here?
'x' => array(
'weight' => 5,
'default' => 0,
),
'y' => array(
'weight' => 6,
'default' => 0,
),
'width' => array(
'weight' => 7,
'default' => $w ? $w : 50,
),
'height' => array(
'weight' => 8,
'default' => $h ? $h : 50,
),
'changed' => array(
'weight' => 0,
'default' => 0,
),
);
// ###
// drupal_set_message('node_field is <pre>'. print_r($node_field, TRUE) .'</pre>');
// ###
foreach ($node_field as $delta => $file) {
if ($file['filepath'] && !$file['flags']['delete']) {
$cropid = "{$fieldname}-{$delta}";
$form[$fieldname][$delta] = array(
'#theme' => 'imagefield_crop_edit_crop_image_row',
'#croparea' => $field['widget']['croparea'],
'#cropid' => $cropid,
);
$form[$fieldname][$delta]['flags']['delete'] = array(
'#type' => 'checkbox',
'#title' => t('Delete'),
'#default_value' => 0,
);
$filename = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path($field['widget']['image_path'])) : $file['filepath'];
if (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath']))) {
if ($field['widget']['enforce_ratio']) {
list($fw, $fh) = explode('x', $field['widget']['resolution']);
$ratio = $fw / $fh;
$image_ratio = $width / $height;
if ($ratio > $image_ratio) {
$height = $width / $ratio;
}
else {
$width = $height * $ratio;
}
// if the enforced ratio is different, force crop
$crop_attrs['changed']['default'] = $ratio != $image_ratio;
}
$crop_attrs['width']['default'] = $width;
$crop_attrs['height']['default'] = $height;
}
$form[$fieldname][$delta]['preview'] = array(
'#type' => 'markup',
'#value' => theme('imagefield_crop_crop_image', $file, $file['alt'], $file['title'], NULL, TRUE, "{$fieldname}-{$delta}"),
);
$form[$fieldname][$delta]['crop'] = array();
foreach ($crop_attrs as $attr => $value) {
$form[$fieldname][$delta]['crop'][$attr] = array(
'#type' => 'hidden',
'#title' => $attr,
'#size' => 4,
'#maxlength' => 25,
'#weight' => $value['weight'],
'#attributes' => array(
'class' => 'edit-image-crop-' . $attr,
),
'#value' => $file['crop'][$attr] ? $file['crop'][$attr] : $value['default'],
);
}
/**
* Show crop preview
*/
$cropped = $file['crop'];
$cropped['fid'] = 'upload';
/* fool the theme function */
$form[$fieldname][$delta]['crop']['preview'] = array(
'#type' => 'markup',
'#value' => theme('imagefield_image', $cropped, '', '', array(
'class' => 'imagefield-crop-image-preview',
)),
);
// ###
// drupal_set_message('imagefield['. $fieldname .'] '. $op .' node field: <pre>'. print_r($node_field, true) .'</pre>');
// ###
$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('Alternate text to be displayed if the image cannot be displayed.'),
'#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']['delete']) {
// Hide all the form values if this item is marked for deletion
$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'],
);
}
$form[$fieldname][$delta]['filename'] = array(
'#type' => 'value',
'#value' => $file['filename'],
);
$form[$fieldname][$delta]['filepath'] = array(
'#type' => 'value',
'#value' => $file['filepath'],
);
$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'],
);
//$form[$fieldname][$delta]['preview-path'] = array('#type' => 'value', '#value' => $filename);
$form[$fieldname][$delta]['crop']['filepath'] = array(
'#type' => 'value',
'#value' => $file['crop']['filepath'],
);
//$form[$fieldname][$delta]['crop']['preview-path'] = array('#type' => 'value', '#value' => $file['crop']['preview']);
}
}
else {
if ($field['widget']['always_show']) {
$cropid = "{$fieldname}-0";
// just show croppable area, without any image
$form[$fieldname][0] = array(
'#theme' => 'imagefield_crop_edit_crop_image_row',
'#croparea' => $field['widget']['croparea'],
'#cropid' => $cropid,
);
$form[$fieldname][0]['preview'] = array(
'#type' => 'markup',
/* REFACTOR: use different theme based on croppable widget settings */
'#value' => theme('imagefield_crop_crop_image', NULL, '', '', NULL, FALSE, $cropid),
);
}
}
return $form;
}