Hi guys...
Recently, I've been searching for a way to hide image styles at user pictures field. My first thought was to add into admin-settings this code (file imagecrop.admin.inc, function imagecrop_settings_form()):
//... Code
$form['imagecrop_userpictures'] = array(
'#type' => 'fieldset',
'#title' => t('User pictures'),
'#collapsible' => TRUE,
'#tree' => FALSE,
);
$form['imagecrop_userpictures']['imagecrop_userpictures_styles'] = array(
'#type' => 'checkboxes',
'#title' => t('Available styles'),
'#options' => get_imagecrop_styles(),
'#default_value' => variable_get('imagecrop_userpictures_styles', array()),
);
//... Code
Well, there isn't problem with this. It's only to be used after...
Now, watching at imagecrop.module, I see an implementation of hook_form_user_profile_form_alter, which is created to add the button and tools to crop user pictures. The current code is this:
/**
* Implements hook_form_user_profile_form_alter().
* Add imagecrop to profile picture, if enabled.
*/
function imagecrop_form_user_profile_form_alter(&$form, $form_state, $form_id) {
$hooks = variable_get('imagecrop_modules', array());
if (!empty($form['picture']['picture']['#value']) && !empty($hooks['profile_picture'])) {
$styles = get_imagecrop_styles(); // Issue is here.
if (count($styles) > 0) {
// Create fake file object for permission check.
$file = new stdClass();
$file->uid = $form['#user']->uid;
$imagecrop = new ImageCrop();
$imagecrop->setFile($file);
global $user;
if (!$imagecrop->hasUserAccess($user)) {
return;
}
$variables = array(
'styles' => $styles,
'js_file' => 'imagecrop_field.js',
'text' => t('Crop picture'),
'fid' => $form['picture']['picture']['#value']->fid,
);
$form['picture']['picture_current']['#markup'] .= imagecrop_linkitem($form, $variables);
}
}
}
For example, if I wish to change part of that code (the variable "$styles") like this:
/**
* Implements hook_form_user_profile_form_alter().
* Add imagecrop to profile picture, if enabled.
*/
function imagecrop_form_user_profile_form_alter(&$form, $form_state, $form_id) {
$hooks = variable_get('imagecrop_modules', array());
if (!empty($form['picture']['picture']['#value']) && !empty($hooks['profile_picture'])) {
// ----- Changed code starts here:
// I get selected styles from admin settings:
$styles = array_filter(variable_get('imagecrop_userpictures_styles', array()));
// If $styles is empty, therefore uses all styles by default:
if(empty($styles)) {
$styles = get_imagecrop_styles();
}
// ... And ends here. -----
if (count($styles) > 0) {
// Create fake file object for permission check.
$file = new stdClass();
$file->uid = $form['#user']->uid;
$imagecrop = new ImageCrop();
$imagecrop->setFile($file);
global $user;
if (!$imagecrop->hasUserAccess($user)) {
return;
}
$variables = array(
'styles' => $styles, // Variable with selected styles... Ok?
'js_file' => 'imagecrop_field.js',
'text' => t('Crop picture'),
'fid' => $form['picture']['picture']['#value']->fid,
);
$form['picture']['picture_current']['#markup'] .= imagecrop_linkitem($form, $variables);
}
}
}
Previous code doesn't work. At this momment, I've created three javascript crop image styles, two will be used with fields inside nodes, and the third will be used for pictures. The goal is, when we check some styles into admin settings (one in this case), only that style must be available like an option when user edit his picture. But, making previous changes, it doesn't work... I see all image styles like options into the combobox (in this case, would be just one, because just one is selected in admin settings).
I'ld like to add this as a feature for new versions... If I'm wrong or there is another way, please tell me how :)
Comments
Comment #1
ianthomas_ukComment #2
ianthomas_ukTo summarise the problem, when you click the 'Crop x' button, the widget will show all styles that are configured for that file, which may include styles that are not relevant for the place that you've clicked the crop button.
To fix this with no UI changes, we'd need to pass some extra context to the popup. But I wonder if it would be better to change the UI so you have a Crop button for each image style, which takes you straight into the cropping mode for that style, rather than having the intermediate read-only screen that we have at present.