Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHS-5954: Social Media Footer block — allow mailto: links #1706

Merged
merged 7 commits into from
Jan 14, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Element\Url;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -126,10 +127,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.'),
'#description' => $this->t('Social Media Profile URL. Must start with https:// or mailto:'),
'#default_value' => $link['link_url'],
'#element_validate' => [
[get_class($this), 'validateUrl'],
],
],
'link_title' => [
'#type' => 'textfield',
Expand Down Expand Up @@ -162,6 +166,29 @@ 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) {
codechefmarc marked this conversation as resolved.
Show resolved Hide resolved
$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)) {
Comment on lines +181 to +182
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine these two.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the problems here is that if someone enters in mailto: with an invalid email address, that case will not be caught and instead will be validated as a URL. I can change so we don't get any custom messaging for invalid mailto links and combine if you wish.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, this works for me. 👍

$form_state->setError($element, t('The mailto link must include a valid email address (e.g., mailto:[email protected]).'));
}
return;
}

URL::validateUrl($element, $form_state, $complete_form);

}

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