function contact_attach_form_alter in Contact Attach 7
Same name and namespace in other branches
- 5 contact_attach.module \contact_attach_form_alter()
- 6 contact_attach.module \contact_attach_form_alter()
Implements hook_form_alter().
File
- ./
contact_attach.module, line 73 - Allows attaching files to messages sent using contact forms.
Code
function contact_attach_form_alter(&$form, &$form_state, $form_id) {
if ($form_id === 'contact_personal_form' && user_access('attach files on personal contact forms') || $form_id === 'contact_site_form' && user_access('attach files on site-wide contact form')) {
switch ($form_id) {
case 'contact_site_form':
$contact_form_short = 'site';
$contact_form_permission = 'attach files on site-wide contact form';
break;
case 'contact_personal_form':
$contact_form_short = 'user';
$contact_form_permission = 'attach files on personal contact forms';
break;
}
$contact_attach_numbers = variable_get('contact_attach_number_' . $contact_form_short, array());
$roles = _contact_attach_get_valid_roles($contact_form_permission, $contact_attach_numbers);
if (module_exists('file') && variable_get('contact_attach_simple_field', 0) !== 1) {
$file_field_type = 'managed_file';
}
else {
$file_field_type = 'file';
}
// Send these values along to the form validation and submit handlers.
$form['file_field_type'] = array(
'#type' => 'value',
'#value' => $file_field_type,
);
$form['attachments_allowed'] = array(
'#type' => 'value',
'#value' => _contact_attach_return_max_attachments($roles, $contact_attach_numbers),
);
$form['allowed_extensions'] = array(
'#type' => 'value',
'#value' => _contact_attach_return_allowed_extensions($roles, $contact_form_short),
);
$form['file_size_limit'] = array(
'#type' => 'value',
'#value' => _contact_attach_return_max_file_size($roles, $contact_form_short),
);
$description = t('Files must be less than !size.', array(
'!size' => '<strong>' . format_size($form['file_size_limit']['#value']) . '</strong>',
));
$description .= '<br />' . t('Allowed file types: !extensions.', array(
'!extensions' => '<strong>' . $form['allowed_extensions']['#value'] . '</strong>',
));
if ($form['attachments_allowed']['#value'] !== 1) {
$form['attachments'] = array(
'#type' => 'fieldset',
'#title' => t('Attachments'),
);
$form['attachments']['#description'] = $description;
}
for ($i = 1; $i <= $form['attachments_allowed']['#value']; $i++) {
$form['attachments']['contact_attach_' . $i] = array(
'#type' => $form['file_field_type']['#value'],
'#weight' => $i,
);
if ($form['attachments_allowed']['#value'] !== 1) {
$form['attachments']['contact_attach_' . $i]['#title'] = t('Attachment #@i', array(
'@i' => $i,
));
}
else {
$form['attachments']['contact_attach_' . $i]['#title'] = t('Attachment');
$form['attachments']['contact_attach_' . $i]['#description'] = $description;
}
if ($form['file_field_type']['#value'] === 'managed_file') {
$form['attachments']['contact_attach_' . $i]['#upload_validators'] = array(
'file_validate_extensions' => array(
$form['allowed_extensions']['#value'],
),
'file_validate_size' => array(
(string) $form['file_size_limit']['#value'],
),
);
$form['attachments']['contact_attach_' . $i]['#progress_message'] = t('Attaching...');
}
}
// Use only our form submission handler.
$form['#submit'] = array(
'contact_attach_' . $form_id . '_submit',
);
$form['actions']['submit']['#weight'] = $i + 1;
if ($form['file_field_type']['#value'] === 'file') {
// Add a form validation handler to validate attachments.
$form['#validate'][] = 'contact_attach_contact_form_validate';
}
}
}