public function MatomoAdminSettingsForm::validateForm in Matomo Analytics 8
Form validation handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormBase::validateForm
File
- src/
Form/ MatomoAdminSettingsForm.php, line 528
Class
- MatomoAdminSettingsForm
- Configure Matomo settings for this site.
Namespace
Drupal\matomo\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
// Custom variables validation.
foreach ($form_state
->getValue([
'matomo_custom_var',
'slots',
]) as $custom_var) {
$form_state
->setValue([
'matomo_custom_var',
'slots',
$custom_var['slot'],
'name',
], trim($custom_var['name']));
$form_state
->setValue([
'matomo_custom_var',
'slots',
$custom_var['slot'],
'value',
], trim($custom_var['value']));
// Validate empty names/values.
if (empty($custom_var['name']) && !empty($custom_var['value'])) {
$form_state
->setErrorByName("matomo_custom_var][slots][" . $custom_var['slot'] . "][name", $this
->t('The custom variable @slot-number requires a <em>Name</em> if a <em>Value</em> has been provided.', [
'@slot-number' => $custom_var['slot'],
]));
}
elseif (!empty($custom_var['name']) && empty($custom_var['value'])) {
$form_state
->setErrorByName("matomo_custom_var][slots][" . $custom_var['slot'] . "][value", $this
->t('The custom variable @slot-number requires a <em>Value</em> if a <em>Name</em> has been provided.', [
'@slot-number' => $custom_var['slot'],
]));
}
}
$form_state
->setValue('matomo_custom_var', $form_state
->getValue([
'matomo_custom_var',
'slots',
]));
// Trim some text area values.
$form_state
->setValue('matomo_site_id', trim($form_state
->getValue('matomo_site_id')));
$form_state
->setValue('matomo_visibility_request_path_pages', trim($form_state
->getValue('matomo_visibility_request_path_pages')));
$form_state
->setValue('matomo_codesnippet_before', trim($form_state
->getValue('matomo_codesnippet_before')));
$form_state
->setValue('matomo_codesnippet_after', trim($form_state
->getValue('matomo_codesnippet_after')));
$form_state
->setValue('matomo_visibility_user_role_roles', array_filter($form_state
->getValue('matomo_visibility_user_role_roles')));
$form_state
->setValue('matomo_trackmessages', array_filter($form_state
->getValue('matomo_trackmessages')));
if (!preg_match('/^\\d{1,}$/', $form_state
->getValue('matomo_site_id'))) {
$form_state
->setErrorByName('matomo_site_id', $this
->t('A valid Matomo site ID is an integer only.'));
}
$url = $form_state
->getValue('matomo_url_http');
if ($url && substr($url, -strlen('/')) !== '/') {
$url .= '/';
$form_state
->setValueForElement($form['general']['matomo_url_http'], $url);
}
$url = $url . 'matomo.php';
$skip_error_check = $form_state
->getValue('matomo_url_skiperror');
try {
$result = $this->httpClient
->get($url);
if (!$skip_error_check && $result
->getStatusCode() != 200) {
$form_state
->setErrorByName('matomo_url_http', $this
->t('The validation of "@url" failed with error "@error" (HTTP code @code).', [
'@url' => UrlHelper::filterBadProtocol($url),
'@error' => $result
->getReasonPhrase(),
'@code' => $result
->getStatusCode(),
]));
}
} catch (RequestException $exception) {
if (!$skip_error_check) {
$form_state
->setErrorByName('matomo_url_http', $this
->t('The validation of "@url" failed with an exception "@error" (HTTP code @code).', [
'@url' => UrlHelper::filterBadProtocol($url),
'@error' => $exception
->getMessage(),
'@code' => $exception
->getCode(),
]));
}
}
$matomo_url_https = $form_state
->getValue('matomo_url_https');
if (!empty($matomo_url_https)) {
$url = $matomo_url_https;
if ($url && substr($url, -strlen('/')) !== '/') {
$url .= '/';
$form_state
->setValueForElement($form['general']['matomo_url_https'], $url);
}
$url = $url . 'matomo.php';
try {
$result = $this->httpClient
->get($url);
if (!$skip_error_check && $result
->getStatusCode() != 200) {
$form_state
->setErrorByName('matomo_url_https', $this
->t('The validation of "@url" failed with error "@error" (HTTP code @code).', [
'@url' => UrlHelper::filterBadProtocol($url),
'@error' => $result
->getReasonPhrase(),
'@code' => $result
->getStatusCode(),
]));
}
} catch (RequestException $exception) {
if (!$skip_error_check) {
$form_state
->setErrorByName('matomo_url_https', $this
->t('The validation of "@url" failed with an exception "@error" (HTTP code @code).', [
'@url' => UrlHelper::filterBadProtocol($url),
'@error' => $exception
->getMessage(),
'@code' => $exception
->getCode(),
]));
}
}
}
// Verify that every path is prefixed with a slash, but don't check PHP
// code snippets and do not check for slashes if no paths configured.
if ($form_state
->getValue('matomo_visibility_request_path_mode') != 2 && !empty($form_state
->getValue('matomo_visibility_request_path_pages'))) {
$pages = preg_split('/(\\r\\n?|\\n)/', $form_state
->getValue('matomo_visibility_request_path_pages'));
foreach ($pages as $page) {
if (strpos($page, '/') !== 0 && $page !== '<front>') {
$form_state
->setErrorByName('matomo_visibility_request_path_pages', $this
->t('Path "@page" not prefixed with slash.', [
'@page' => $page,
]));
// Drupal forms show one error only.
break;
}
}
}
// Clear obsolete local cache if cache has been disabled.
if ($form_state
->isValueEmpty('matomo_cache') && $form['advanced']['matomo_cache']['#default_value']) {
matomo_clear_js_cache();
}
// This is for the Newbie's who cannot read a text area description.
if (preg_match('/(.*)<\\/?script(.*)>(.*)/i', $form_state
->getValue('matomo_codesnippet_before'))) {
$form_state
->setErrorByName('matomo_codesnippet_before', $this
->t('Do not include the <script> tags in the javascript code snippets.'));
}
if (preg_match('/(.*)<\\/?script(.*)>(.*)/i', $form_state
->getValue('matomo_codesnippet_after'))) {
$form_state
->setErrorByName('matomo_codesnippet_after', $this
->t('Do not include the <script> tags in the javascript code snippets.'));
}
}