function gc_mapping_edit_form_validate in GatherContent 8
Validation callback for edit mapping form.
In this function, we validate if:
- title of title_field is mapped
- each field is used only once, if we are mapping single language
content type or page OR
- each field is used only once per fieldset, if we are mapping multilingual
- each language is used only once except `und`.
@inheritdoc
File
- forms/
gc.mapping-edit.inc, line 304 - Multistep mapping form.
Code
function gc_mapping_edit_form_validate($form, &$form_state) {
if ($form_state['triggering_element']['#id'] == 'edit-submit') {
$form_definition_elements = array(
'return',
'form_build_id',
'form_token',
'form_id',
'op',
);
$non_data_elements = array_merge($form_definition_elements, array(
'gc_template',
'content_type',
'id',
'updated',
));
$mapping_data = array();
foreach ($form_state['values'] as $key => $value) {
if (!in_array($key, $non_data_elements) && substr_compare($key, 'tab', 0, 3) === 0) {
$mapping_data[$key] = $value;
}
}
// Check if is translatable.
$mapping_entity = \Drupal::entityManager()
->getStorage('gc_mapping', array(
$form_state['values']['id'],
));
$mapping = reset($mapping_entity);
$content_type = empty($mapping->content_type) ? $form_state['values']['content_type'] : $mapping->content_type;
$translatable = \Drupal::moduleHandler()
->moduleExists('entity_translation') && \Drupal::moduleHandler()
->moduleExists('title') && entity_translation_node_supported_type($content_type) && title_field_replacement_enabled('node', $content_type, 'title');
// Validate if each language is used only once
// for translatable content types.
$content_lang = array();
$metatag_lang = array();
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 {
form_set_error($tab_id . '[language]', t('Each language can be used only once'));
}
}
}
}
// Validate if each field is used only once.
$content_fields = array();
$metatag_fields = array();
if ($translatable) {
foreach ($content_lang as $lang) {
$content_fields[$lang] = array();
}
foreach ($metatag_lang as $lang) {
$metatag_fields[$lang] = array();
}
$content_fields['und'] = $metatag_fields['und'] = array();
}
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 (!in_array($element, ${$tab_type . '_fields'}[$tab['language']])) {
${$tab_type . '_fields'}[$tab['language']][] = $element;
}
else {
form_set_error($tab_id, t('A GatherContent field can only be mapped to a single Drupal field. So each field can only be mapped to once.'));
}
}
else {
if (!in_array($element, ${$tab_type . '_fields'})) {
${$tab_type . '_fields'}[] = $element;
}
else {
form_set_error($tab_id, 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)) {
form_set_error('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) {
form_set_error('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('field_title', $lang_fields) && $k != \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED) {
form_set_error('form', t('You have to map Drupal Title field for translatable content'));
}
}
}
}
}