@ -14,6 +14,7 @@ function ill_corporate_forms_form_node_ill_institution_request_form_alter(array
_ill_corporate_forms_add_item_type_states($form);
$form['#validate'][] = '_ill_corporate_forms_validate_partner_email';
$form['#validate'][] = '_ill_corporate_forms_validate_institution_code';
$form['#validate'][] = '_ill_corporate_forms_validate_request_emails';
// Hide the title field; presave hook will populate it.
$form['title']['#access'] = FALSE;
@ -26,11 +27,28 @@ function ill_corporate_forms_form_node_ill_institution_request_edit_form_alter(a
_ill_corporate_forms_add_item_type_states($form);
$form['#validate'][] = '_ill_corporate_forms_validate_partner_email';
$form['#validate'][] = '_ill_corporate_forms_validate_institution_code';
$form['#validate'][] = '_ill_corporate_forms_validate_request_emails';
// Hide the title field; presave hook will populate it.
$form['title']['#access'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter() for the ill_institution node form.
*/
function ill_corporate_forms_form_node_ill_institution_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
$form['#validate'][] = '_ill_corporate_forms_validate_institution_emails';
$form['#validate'][] = '_ill_corporate_forms_validate_institution_phone';
}
/**
* Implements hook_form_FORM_ID_alter() for the ill_institution edit form.
*/
function ill_corporate_forms_form_node_ill_institution_edit_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
$form['#validate'][] = '_ill_corporate_forms_validate_institution_emails';
$form['#validate'][] = '_ill_corporate_forms_validate_institution_phone';
}
/**
* Adds #states for item-type conditional fields on the request form.
*
@ -42,6 +60,7 @@ function _ill_corporate_forms_add_item_type_states(array &$form): void {
// Book/Video/Thesis fields: visible when item_type is "book" OR "book_chapter".
$book_fields = [
'field_ill_item_title',
'field_ill_author_editor',
'field_ill_isbn',
'field_ill_publication_date',
@ -201,3 +220,67 @@ function _ill_corporate_forms_validate_partner_email(array &$form, FormStateInte
}
}
/**
* Extracts a string value from a form state field value.
*
* @param mixed $field_value
* The raw value from $form_state->getValue().
*
* @return string
* The extracted string value, or empty string if not found.
*/
function _ill_corporate_forms_extract_field_value($field_value): string {
if (is_array($field_value) && isset($field_value[0]['value'])) {
return (string) $field_value[0]['value'];
}
if (is_string($field_value)) {
return $field_value;
}
return '';
}
/**
* Validates email format for the ill_institution_request form.
*/
function _ill_corporate_forms_validate_request_emails(array &$form, FormStateInterface $form_state): void {
$email = _ill_corporate_forms_extract_field_value($form_state->getValue('field_ill_req_email'));
if (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$form_state->setErrorByName('field_ill_req_email',
t('The email address "@email" is not valid. Please enter a valid email address (e.g., name@example.com).', ['@email' => $email]));
}
}
/**
* Validates email format for the ill_institution form.
*/
function _ill_corporate_forms_validate_institution_emails(array &$form, FormStateInterface $form_state): void {
$fields = [
'field_ill_email' => 'Email Address',
'field_ill_contact_email' => 'Contact Email Address',
];
foreach ($fields as $field_name => $label) {
$email = _ill_corporate_forms_extract_field_value($form_state->getValue($field_name));
if (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$form_state->setErrorByName($field_name,
t('The @label "@email" is not valid. Please enter a valid email address (e.g., name@example.com).', [
'@label' => $label,
'@email' => $email,
]));
}
}
}
/**
* Validates phone number format for the ill_institution form.
*
* Requires the format ddd-ddd-dddd (e.g., 555-123-4567).
*/
function _ill_corporate_forms_validate_institution_phone(array &$form, FormStateInterface $form_state): void {
$phone = _ill_corporate_forms_extract_field_value($form_state->getValue('field_ill_contact_phone'));
if (!empty($phone) && !preg_match('/^\d{3}-\d{3}-\d{4}$/', $phone)) {
$form_state->setErrorByName('field_ill_contact_phone',
t('The phone number "@phone" is not in the correct format. Please enter the phone number as ddd-ddd-dddd (e.g., 555-123-4567).', ['@phone' => $phone]));
}
}