Skip to content

Commit

Permalink
feat(SHS-5954): Allow mailto links for social media block
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Berger committed Jan 3, 2025
1 parent a12f965 commit 003c566
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,13 @@ public function blockForm($form, FormStateInterface $form_state): array {
$form['links'][$key] = [
'#type' => 'container',
'link_url' => [
'#type' => 'url',
'#type' => 'textfield',
'#title' => $this->t('URL'),
'#description' => $this->t('Social Media Profile URL.'),
'#default_value' => $link['link_url'],
'#element_validate' => [
[get_class($this), 'validateUrl'],
],
],
'link_title' => [
'#type' => 'textfield',
Expand Down Expand Up @@ -162,6 +165,31 @@ public function blockForm($form, FormStateInterface $form_state): array {
return $form;
}

/**
* Custom validation for the link_url field.
*/
public static function validateUrl(array &$element, FormStateInterface $form_state, array &$complete_form) {
$value = $element['#value'];

if (empty($value)) {
return;
}

$mailto_regex = '/^mailto:[\w.%+-]+@[A-Za-z0-9-]+\.[A-Za-z]{2,}(?:\?[^\s]*)?$/i';

if (str_starts_with($value, 'mailto')) {
if (!preg_match($mailto_regex, $value)) {
$form_state->setError($element, t('The mailto link must include a valid email address (e.g., mailto:[email protected]).'));
return;
}
}
else {
if (!filter_var($value, FILTER_VALIDATE_URL)) {
$form_state->setError($element, t('The URL must be a valid web address (e.g., https://www.stanford.edu) or a valid email address (e.g., mailto:[email protected]).'));
}
}
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 003c566

Please sign in to comment.