-
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.
SHS-5954: Social Media Footer block — allow mailto: links (#1706)
* feat(SHS-5954): Allow mailto links for social media block * refactor(SHS-5954): Change validation to use Drupal core validateUrl * refactor(SHS-5954): Minor changes from SWS --------- Co-authored-by: ahughes3 <[email protected]>
- Loading branch information
1 parent
0d41267
commit beb82ac
Showing
1 changed file
with
29 additions
and
2 deletions.
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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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', | ||
|
@@ -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) { | ||
$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; | ||
} | ||
|
||
URL::validateUrl($element, $form_state, $complete_form); | ||
|
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|