public function MappingSteps::doValidate in GatherContent 8.4
Same name and namespace in other branches
- 8.5 gathercontent_ui/src/Form/MappingEditSteps/MappingSteps.php \Drupal\gathercontent_ui\Form\MappingEditSteps\MappingSteps::doValidate()
Do validation.
File
- gathercontent_ui/
src/ Form/ MappingEditSteps/ MappingSteps.php, line 148
Class
- MappingSteps
- Class MappingSteps.
Namespace
Drupal\gathercontent_ui\Form\MappingEditStepsCode
public function doValidate(array &$form, FormStateInterface $formState) {
$form_definition_elements = [
'return',
'form_build_id',
'form_token',
'form_id',
'op',
];
$non_data_elements = array_merge($form_definition_elements, [
'content_type',
'id',
'updated',
'gathercontent_project',
'gathercontent_template',
'er_mapping_type',
'submit',
'close',
]);
$mapping_data = [];
foreach ($formState
->getValues() as $key => $value) {
if (!in_array($key, $non_data_elements)) {
$mapping_data[$key] = $value;
}
}
// Check if is translatable.
$content_type = empty($this->mapping
->getContentType()) ? $formState
->getValue('content_type') : $this->mapping
->getContentType();
$translatable = \Drupal::moduleHandler()
->moduleExists('content_translation') && \Drupal::service('content_translation.manager')
->isEnabled('node', $content_type);
// Validate if each language is used only once
// for translatable content types.
$content_lang = [];
$metatag_lang = [];
if ($translatable) {
foreach ($mapping_data as $tab_id => $tab) {
$tab_type = isset($tab['type']) ? $tab['type'] : 'content';
if ($tab['language'] != 'und') {
if (!in_array($tab['language'], ${$tab_type . '_lang'})) {
${$tab_type . '_lang'}[] = $tab['language'];
}
else {
$element = $tab_id . '[language]';
$formState
->setErrorByName($element, $this
->t('Each language can be used only once'));
}
}
}
}
// Validate if each field is used only once.
$content_fields = [];
$metatag_fields = [];
if ($translatable) {
foreach ($content_lang as $lang) {
$content_fields[$lang] = [];
}
foreach ($metatag_lang as $lang) {
$metatag_fields[$lang] = [];
}
$content_fields['und'] = $metatag_fields['und'] = [];
}
foreach ($mapping_data as $tab_id => $tab) {
$tab_type = isset($tab['type']) ? $tab['type'] : 'content';
if (isset($tab['elements'])) {
foreach ($tab['elements'] as $k => $element) {
if (empty($element)) {
continue;
}
if ($translatable) {
if ($tab_type == 'content' && in_array($this->template->config[$tab_id]->elements[$k]->type, [
'text',
'section',
]) || !in_array($element, ${$tab_type . '_fields'}[$tab['language']])) {
${$tab_type . '_fields'}[$tab['language']][] = $element;
}
else {
if (!strpos($element, '||')) {
$formState
->setErrorByName($tab_id, $this
->t('A GatherContent field can only be mapped to a single Drupal field. So each field can only be mapped to once.'));
}
}
}
else {
if ($tab_type == 'content' && in_array($this->template->config[$tab_id]->elements[$k]->type, [
'text',
'section',
]) || !in_array($element, ${$tab_type . '_fields'})) {
${$tab_type . '_fields'}[] = $element;
}
else {
if (!strpos($element, '||')) {
$formState
->setErrorByName($tab_id, $this
->t('A GatherContent field can only be mapped to a single Drupal field. So each field can only be mapped to once.'));
}
}
}
}
}
}
// Validate if at least one field in mapped.
if (!$translatable && empty($content_fields) && empty($metatag_fields)) {
$formState
->setErrorByName('form', t('You need to map at least one field to create mapping.'));
}
elseif ($translatable && count($content_fields) === 1 && empty($content_fields['und']) && empty($metatag_fields['und']) && count($metatag_fields) === 1) {
$formState
->setErrorByName('form', t('You need to map at least one field to create mapping.'));
}
// Validate if title is mapped for translatable content.
if ($translatable) {
foreach ($content_fields as $k => $lang_fields) {
if (!in_array('title', $lang_fields) && $k != 'und') {
$formState
->setErrorByName('form', t('You have to map Drupal Title field for translatable content.'));
}
}
}
}