Posted by nedjo on August 18, 2010 at 11:47pm
8 followers
| Project: | Accessible Helper Module |
| Version: | 7.x-2.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Issue Summary
Here's a draft fix for the imagefield issues discussed in #347070: Ability to make alt and title text required for each image. Makes alt text required, provides better help text, removes length limit.
<?php
/**
* Implementation of hook_elements().
*
* Add a #process callback to the imagefield widget element so we can perform alters.
* See <a href="http://drupal.org/node/347070.
" title="http://drupal.org/node/347070.
" rel="nofollow">http://drupal.org/node/347070.
</a> */
function accessible_fix_elements() {
$elements = array();
$elements['imagefield_widget']['#process'][] = 'accessible_fix_imagefield_widget_process';
return $elements;
}
/**
* Element #process callback function.
*/
function accessible_fix_imagefield_widget_process($element, $edit, &$form_state, $form) {
if (isset($element['data']) && isset($element['data']['alt'])) {
$element['data']['alt']['#required'] = TRUE;
$element['data']['alt']['#length'] = 175;
$element['data']['alt']['#description'] = t('This text will be used by screen readers, search engines, or when the image cannot be loaded. Describe the image in sufficient detail, as if you were trying to explain it to someone over the phone. Try to use descriptions of the content (what are you looking at) and simple visual terms (shape, color, texture). If the image includes text, always include that in the alt text.');
unset($element['data']['alt']['#value']);
}
return $element;
}
?>
Comments
#1
Sweet.
I tested it, and it seems to work for me if the accessible_fix module's "weight" is changed to something greater than 0. It won't work if imagefield's process function is called first, because it'll overwrite the ['data']['alt'] array.
Weight can be changed in drupal with something like this:
$sql = 'UPDATE system SET weight = 1 ' .'WHERE filename = ' . $base_path . '/modules/accessible/accessible_fix/accessible_fix.module';
update_sql($sql);
I'll probably add something like that into the accessible_fix.install file.
#2
Please use a query like :
update_sql("UPDATE {system} SET weight = 1 WHERE type = 'module' AND name = 'accessible_fix'");Because the installation path for modules may be different, e.g. for multisites.
#3
Code tested : works fine!
#4
Hmmm sorry, I missed an error :
the key for textfield length should be #maxlength and not #length.
So the line
$element['data']['alt']['#length'] = 175;should be replaced by$element['data']['alt']['#maxlength'] = 175;.#5
Need to check that this made it to 7.x-2.x branch.
#6
I don't see it as an option here - admin/config/regional/access/accessible_api
#7
I had to modify the code slightly to work for Drupal 7. Here's the code that I got to work for me in Drupal7:
<?php
/**
* Implementation of hook_field_widget_WIDGET_TYPE_form_alter()
*
* Add a #process callback to the imagefield widget element so we can perform alters.
*/
function accessible_fix_field_widget_image_image_form_alter(&$element, &$form_state, $context) {
foreach (element_children($element) as $delta){
$element[$delta]['#process'][] = 'accessible_fix_imagefield_widget_process';
}
}
/**
* Element #process callback function.
*/
function accessible_fix_imagefield_widget_process($element, &$form_state, $form) {
$element['alt']['#required'] = TRUE;
return $element;
}
?>
Though this only shows a validation error after the user has tried to save the node, because the Alternate text is only available after the image has been uploaded.
I'd prefer to make the Alternate text available to the users before they submit/upload so they can fill everything in at once and submit once. Will work on that next. If anyone can share a working solution, would really appreciate it.
#8
That would be slicker for sure (with the alt tags being presented before the file is uploaded). Thanks for uploading the code. Can you upload a patch so that it's easier to see the differences proposed?
#9
Fixed my previous code: Alt and Title text fields are shown on the node creation form without requiring for the image file to be uploaded first (can select file for upload and specify Alt text so that everything is saved in one click). Note that I created this as a separate module and have not had a chance to create this as a patch to the accessible_fix module. Will upload .patch file if I get a chance to create.
EDIT: added validation for when image field is NOT required but alt text is enabled.
2nd EDIT (12-Mar-2012): fixed bug in the code: Image field was not enforced as long as alt text was filled out!
Code assumes that when Alt text is enabled, then it's required.
<?php
/**
* Implementation of hook_field_widget_WIDGET_TYPE_form_alter()
*
* Add a #process callback to the imagefield widget element so we can perform alters.
*/
function accessible_fix_field_widget_image_image_form_alter(&$element, &$form_state, $context) {
// Add the custom process function and validator to the image field
foreach (element_children($element) as $delta){
$element[$delta]['#element_validate'] = array('_accesible_img_validate','file_managed_file_validate');
$element[$delta]['#process'][] = 'accessible_fix_imagefield_widget_process';
}
}
/**
* Element #process callback function.
*/
function accessible_fix_imagefield_widget_process($element, &$form_state, $form) {
$instance = field_widget_instance($element, $form_state);
$settings = $instance['settings'];
// Make Alt field as required only if Alt field enabled and Image field is required
$element['alt']['#required'] = (bool) $instance['required'] && $settings['alt_field'];
// Show Alt field and Title field on node creation form if Alt/Title fields enabled
$element['alt']['#access'] = $settings['alt_field'];
$element['title']['#access'] = $settings['title_field'];
return $element;
}
/**
* Element validate function for image field.
*/
function _accesible_img_validate($element, &$form_state, $form) {
// Alternate text required when field enabled and a file exists
if ($element['alt']['#access'] && $element['#file'] && empty($element['alt']['#value']))
form_error($element['alt'], t('Alternate text field is required.'));
}
?>
Other nice to haves:
Without proper theming, Image field inputs look like separate fields. See img_field_without_field_group.jpg attached.
I've currently hacked this by using field_group module. See img_field_with_field_group.jpg for example.
#10
+1 for this feature
#11
Please take a look at the EIM module.