-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SHS-5954): Allow mailto links for social media block
- Loading branch information
Marc Berger
committed
Jan 3, 2025
1 parent
a12f965
commit 003c566
Showing
1 changed file
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
|
@@ -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} | ||
*/ | ||
|