-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathWidgetValidatorTrait.php
More file actions
54 lines (49 loc) · 2.18 KB
/
Copy pathPathWidgetValidatorTrait.php
File metadata and controls
54 lines (49 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Drupal\domain_path;
use Drupal\Core\Form\FormStateInterface;
/**
* Supplies validator methods for path widgets.
*/
trait PathWidgetValidatorTrait {
/**
* @inheritdoc
*/
public static function validateFormElement(array &$element, FormStateInterface $form_state) {
// Trim the submitted value of whitespace and slashes.
$alias = rtrim(trim($element['alias']['#value']), " \\/");
if (!empty($alias)) {
$form_state->setValueForElement($element['alias'], $alias);
// If 'all affiliates' is checked, whether this alias is already in use.
$allAffiliates = $form_state->getValue('field_domain_all_affiliates');
if (!empty($allAffiliates['value'])) {
// Validate that the submitted alias does not exist on any domains.
$domains = \Drupal::service('domain.loader')->loadMultiple();
foreach ($domains as $domain) {
$domain_id = $domain->getDomainId();
$is_exists = \Drupal::service('path.alias_storage')
->aliasExistsByDomain($alias, $element['langcode']['#value'], $element['source']['#value'], $domain_id);
if ($is_exists) {
$form_state->setError($element, t('The alias is already in use by content available to all affiliates.'));
break;
}
}
}
else if ($domain_values = $form_state->getValue(DOMAIN_ACCESS_FIELD)) {
// If domains are checked, check existence of alias on each domain.
foreach ($domain_values as $domain_value) {
if ($domain = \Drupal::service('domain.loader')->load($domain_value['target_id'])) {
// Validate that the submitted alias does not exist yet.
$is_exists = \Drupal::service('path.alias_storage')
->aliasExistsByDomain($alias, $element['langcode']['#value'], $element['source']['#value'], $domain->getDomainId());
if ($is_exists) {
$form_state->setError($element, t('The alias is already in use on :domain.', [':domain' => $domain->get('name')]));
}
}
}
}
}
if ($alias && $alias[0] !== '/') {
$form_state->setError($element, t('The alias needs to start with a slash.'));
}
}
}