From 003c5664bcec0f7f43f243b5280eecdddd9a05ef Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Fri, 3 Jan 2025 15:42:03 -0800 Subject: [PATCH 1/3] feat(SHS-5954): Allow mailto links for social media block --- .../src/Plugin/Block/SocialMediaBlock.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php b/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php index 074a8837c1..140f142ebb 100644 --- a/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php +++ b/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php @@ -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:example@example.com).')); + 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:example@stanford.edu).')); + } + } + } + /** * {@inheritdoc} */ From 48dcbc0c413c179861ebc5a07c95d98a337ce136 Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Mon, 13 Jan 2025 14:47:07 -0800 Subject: [PATCH 2/3] refactor(SHS-5954): Change validation to use Drupal core validateUrl --- .../humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php b/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php index 140f142ebb..e11f22e955 100644 --- a/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php +++ b/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php @@ -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; @@ -128,7 +129,7 @@ public function blockForm($form, FormStateInterface $form_state): array { 'link_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'], @@ -184,9 +185,7 @@ public static function validateUrl(array &$element, FormStateInterface $form_sta } } 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:example@stanford.edu).')); - } + URL::validateUrl($element, $form_state, $complete_form); } } From d0059c104cf0a766fd0914605e18d854a485f678 Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Mon, 13 Jan 2025 17:21:40 -0800 Subject: [PATCH 3/3] refactor(SHS-5954): Minor changes from SWS --- .../hs_blocks/src/Plugin/Block/SocialMediaBlock.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php b/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php index e11f22e955..b17329491a 100644 --- a/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php +++ b/docroot/modules/humsci/hs_blocks/src/Plugin/Block/SocialMediaBlock.php @@ -181,12 +181,12 @@ public static function validateUrl(array &$element, FormStateInterface $form_sta 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:example@example.com).')); - return; } + return; } - else { - URL::validateUrl($element, $form_state, $complete_form); - } + + URL::validateUrl($element, $form_state, $complete_form); + } /**