View source
<?php
use Drupal\content_translation\BundleTranslationSettingsInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\entity_translation_unified_form\EtufHelper;
use Drupal\field\Entity\FieldStorageConfig;
define('ENTITY_TRANSLATION_ENABLED', 4);
function entity_translation_unified_form_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter' && isset($implementations['entity_translation_unified_form'])) {
$hookInit = $implementations['entity_translation_unified_form'];
unset($implementations['entity_translation_unified_form']);
$implementations['entity_translation_unified_form'] = $hookInit;
}
}
function entity_translation_unified_form_preprocess_html(&$variables) {
$current_path = \Drupal::service('path.current')
->getPath();
$patterns = "/node/add/*\n/node/*/edit";
$match = \Drupal::service('path.matcher')
->matchPath($current_path, $patterns);
if (!empty($match)) {
$variables['attributes']['class'][] = 'etuf-sbs';
$variables['attributes']['class'][] = 'sync';
}
}
function entity_translation_unified_form_page_attachments(array &$page) {
$current_path = \Drupal::service('path.current')
->getPath();
$patterns = "/node/add/*\n/node/*/edit";
$match = \Drupal::service('path.matcher')
->matchPath($current_path, $patterns);
if (!empty($match)) {
$bundle = '';
if (strpos($current_path, '/edit') > 4) {
$node = \Drupal::routeMatch()
->getParameter('node');
$bundle = $node
->bundle();
}
else {
$array_of_segments = explode('/', $current_path);
$bundle = end($array_of_segments);
}
if (entity_translation_unified_form_sbs_enabled('node', $bundle)) {
$theme = \Drupal::service('theme.manager')
->getActiveTheme()
->getName();
if (file_exists(drupal_get_path('module', 'entity_translation_unified_form') . "/css/etuf-side-by-side-{$theme}.css")) {
$page['#attached']['library'][] = 'entity_translation_unified_form/etuf-' . $theme;
}
else {
$page['#attached']['library'][] = 'entity_translation_unified_form/etuf';
}
$other_languages = EtufHelper::getOtherEnabledLanguages();
foreach ($other_languages as $other_langcode => $other_language) {
$page['#attached']['drupalSettings']['other_langs'][] = $other_langcode;
}
}
if (!entity_translation_unified_form_moderation_sync_disabled('node', $bundle)) {
$page['#attached']['library'][] = 'entity_translation_unified_form/etuf-moderation-sync';
$other_languages = EtufHelper::getOtherEnabledLanguages();
foreach ($other_languages as $other_langcode => $other_language) {
$page['#attached']['drupalSettings']['other_langs'][] = $other_langcode;
}
}
}
}
function entity_translation_unified_form_form_node_type_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#process'][] = 'entity_translation_unified_form_form_node_type_form_process';
$form['actions']['submit']['#submit'][] = 'entity_translation_unified_form_form_node_type_form_submit';
}
function entity_translation_unified_form_form_node_type_form_process(&$form, $form_state) {
$entity_type_id = 'node';
$bundle = $form['type']['#default_value'];
$states = [
'visible' => [
':input[name="language_content_type"]' => [
'value' => ENTITY_TRANSLATION_ENABLED,
],
],
];
$form['workflow']['entity_translation_unified_form'] = [
'#title' => t('Unified form'),
'#type' => 'fieldset',
'#weight' => 10,
'#states' => $states,
];
$form['workflow']['entity_translation_unified_form']['entity_translation_unified_form_enable'] = [
'#title' => t('Place all content-translatable fields for all enabled languages inline on the node add/edit form'),
'#type' => 'checkbox',
'#default_value' => $form["#id"] === 'node-type-add-form' ? 0 : entity_translation_unified_form_bundle_enabled($entity_type_id, $bundle),
'#disabled' => FALSE,
'#states' => $states,
];
$form['workflow']['entity_translation_unified_form']['entity_translation_unified_form_sbs_enable'] = [
'#title' => t('Enable side-by-side UI mode on the node add/edit form'),
'#type' => 'checkbox',
'#default_value' => $form["#id"] === 'node-type-add-form' ? 0 : entity_translation_unified_form_sbs_enabled($entity_type_id, $bundle),
'#disabled' => FALSE,
'#states' => $states,
];
$form['workflow']['entity_translation_unified_form']['entity_translation_unified_form_moderation_sync_disable'] = [
'#title' => t('Disable moderation sync mode on the node add/edit form'),
'#type' => 'checkbox',
'#default_value' => $form["#id"] === 'node-type-add-form' ? 0 : entity_translation_unified_form_moderation_sync_disabled($entity_type_id, $bundle),
'#disabled' => FALSE,
'#states' => $states,
];
return $form;
}
function entity_translation_unified_form_form_node_type_form_submit(&$form, FormStateInterface $form_state) {
$entity_type_id = 'node';
$bundle = $form_state
->getValues()['type'];
$enabled = $form_state
->getValues()['entity_translation_unified_form_enable'];
entity_translation_unified_form_set_bundle_enabled($entity_type_id, $bundle, $enabled);
$enabled = $form_state
->getValues()['entity_translation_unified_form_sbs_enable'];
entity_translation_unified_form_set_sbs_enabled($entity_type_id, $bundle, $enabled);
$enabled = $form_state
->getValues()['entity_translation_unified_form_moderation_sync_disable'];
entity_translation_unified_form_set_moderation_sync_disabled($entity_type_id, $bundle, $enabled);
$enabled = $form_state
->getValues()['entity_translation_unified_form_translate_labels'];
entity_translation_unified_form_set_translate_labels_enabled($entity_type_id, $bundle, $enabled);
}
function entity_translation_unified_form_set_bundle_enabled($entity_type_id, $bundle, $enabled) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
$settings['entity_translation_unified_form_enable'] = $enabled;
$content_translation_manager
->setBundleTranslationSettings($entity_type_id, $bundle, $settings);
}
}
function entity_translation_unified_form_bundle_enabled($entity_type_id, $bundle) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
return !empty($settings['entity_translation_unified_form_enable']);
}
return FALSE;
}
function entity_translation_unified_form_set_sbs_enabled($entity_type_id, $bundle, $enabled) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
$settings['entity_translation_unified_form_sbs_enable'] = $enabled;
$content_translation_manager
->setBundleTranslationSettings($entity_type_id, $bundle, $settings);
}
}
function entity_translation_unified_form_set_moderation_sync_disabled($entity_type_id, $bundle, $enabled) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
if (isset($settings['entity_translation_unified_form_moderation_sync_disable'])) {
$settings['entity_translation_unified_form_moderation_sync_disable'] = $enabled;
}
else {
$settings['entity_translation_unified_form_moderation_sync_disable'] = FALSE;
}
$content_translation_manager
->setBundleTranslationSettings($entity_type_id, $bundle, $settings);
}
}
function entity_translation_unified_form_sbs_enabled($entity_type_id, $bundle) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
return !empty($settings['entity_translation_unified_form_sbs_enable']);
}
return FALSE;
}
function entity_translation_unified_form_set_translate_labels_enabled($entity_type_id, $bundle, $enabled) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
$settings['entity_translation_unified_form_translate_labels'] = $enabled;
$content_translation_manager
->setBundleTranslationSettings($entity_type_id, $bundle, $settings);
}
}
function entity_translation_unified_form_translate_labels_enabled($entity_type_id, $bundle) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
return !empty($settings['entity_translation_unified_form_translate_labels']);
}
return FALSE;
}
function entity_translation_unified_form_moderation_sync_disabled($entity_type_id, $bundle) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
return !empty($settings['entity_translation_unified_form_moderation_sync_disable']);
}
return FALSE;
}
function entity_translation_unified_form_bundle_display_mode($entity_type_id, $bundle) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
if (!empty($settings['entity_translation_unified_form_theme'])) {
return $settings['entity_translation_unified_form_theme'];
}
}
return NULL;
}
function entity_translation_unified_form_language_display($entity_type_id, $bundle) {
$content_translation_manager = \Drupal::service('content_translation.manager');
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager
->getBundleTranslationSettings($entity_type_id, $bundle);
if (!empty($settings['entity_translation_unified_form_language'])) {
return $settings['entity_translation_unified_form_language'];
}
}
return NULL;
}
function entity_translation_unified_form_get_language_display_options() {
return [
'current' => t('Current language'),
'native' => t('Native language'),
'code' => t('Language code'),
];
}
function entity_translation_unified_form_get_mode_options() {
$mode_options =& drupal_static(__FUNCTION__);
if (empty($mode_options)) {
$type = \Drupal::service('plugin.manager.entity_translation_unified_form_mode');
$plugin_definitions = $type
->getDefinitions();
$mode_options = [];
foreach ($plugin_definitions as $definition) {
$mode_id = $definition['id'];
$mode_options[$mode_id] = $definition['admin_label'];
}
}
return $mode_options;
}
function entity_translation_unified_form_get_mode_plugin($mode_plugin_id) {
$type = \Drupal::service('plugin.manager.entity_translation_unified_form_mode');
$plugin_definitions = $type
->getDefinitions();
foreach ($plugin_definitions as $classname => $definition) {
if ($mode_plugin_id == $definition['id']) {
return $type
->createInstance($classname);
}
}
return NULL;
}
function entity_translation_unified_form_form_language_content_settings_form_alter(&$form, FormStateInterface $form_state) {
if (!\Drupal::currentUser()
->hasPermission('administer content translation')) {
return;
}
$content_translation_manager = \Drupal::service('content_translation.manager');
$entity_manager = \Drupal::entityTypeManager();
$bundle_info_service = \Drupal::service('entity_type.bundle.info');
foreach ($form['#labels'] as $entity_type_id => $label) {
$entity_type = $entity_manager
->getDefinition($entity_type_id);
$entity_type_translatable = $content_translation_manager
->isSupported($entity_type_id);
if ($entity_type_translatable) {
foreach ($bundle_info_service
->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
$form['settings'][$entity_type_id][$bundle]['settings']['content_translation']['entity_translation_unified_form_enable'] = [
'#title' => t('Place all content-translatable fields for all enabled languages inline on the entity add/edit form'),
'#type' => 'checkbox',
'#default_value' => entity_translation_unified_form_bundle_enabled($entity_type_id, $bundle),
'#states' => [
'visible' => [
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
'checked' => TRUE,
],
],
],
];
$form['settings'][$entity_type_id][$bundle]['settings']['content_translation']['entity_translation_unified_form_theme'] = [
'#title' => t('Select the inline display mode'),
'#type' => 'select',
'#default_value' => entity_translation_unified_form_bundle_display_mode($entity_type_id, $bundle),
'#options' => entity_translation_unified_form_get_mode_options(),
'#states' => [
'visible' => [
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
'checked' => TRUE,
],
'and',
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_enable]"]' => [
'checked' => TRUE,
],
],
],
];
$form['settings'][$entity_type_id][$bundle]['settings']['content_translation']['entity_translation_unified_form_language'] = [
'#title' => t('Select how to display language name'),
'#type' => 'select',
'#default_value' => entity_translation_unified_form_language_display($entity_type_id, $bundle),
'#options' => entity_translation_unified_form_get_language_display_options(),
'#states' => [
'visible' => [
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_enable]"]' => [
'checked' => TRUE,
],
'and',
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_theme]"]' => [
'value' => 'EntityTranslationUnifiedFormInlineMode',
],
'and',
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
'checked' => TRUE,
],
],
],
];
$form['settings'][$entity_type_id][$bundle]['settings']['content_translation']['entity_translation_unified_form_translate_labels'] = [
'#title' => t('Translate fields labels and descriptions in each language'),
'#type' => 'checkbox',
'#default_value' => entity_translation_unified_form_translate_labels_enabled($entity_type_id, $bundle),
'#states' => [
'visible' => [
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_enable]"]' => [
'checked' => TRUE,
],
'and',
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_theme]"]' => [
'value' => 'EntityTranslationUnifiedFormInlineMode',
],
'and',
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
'checked' => TRUE,
],
],
],
];
if ($entity_type_id == 'node') {
$form['settings'][$entity_type_id][$bundle]['settings']['content_translation']['entity_translation_unified_form_sbs_enable'] = [
'#title' => t('Enable side-by-side UI mode on the node add/edit form'),
'#type' => 'checkbox',
'#default_value' => entity_translation_unified_form_sbs_enabled('node', $bundle),
'#states' => [
'visible' => [
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
'checked' => TRUE,
],
'and',
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_enable]"]' => [
'checked' => TRUE,
],
],
],
];
}
if ($entity_type_id == 'node') {
$form['settings'][$entity_type_id][$bundle]['settings']['content_translation']['entity_translation_unified_form_moderation_sync_disable'] = [
'#title' => t('Disable moderation sync mode on the node add/edit form for this bundle'),
'#type' => 'checkbox',
'#default_value' => entity_translation_unified_form_moderation_sync_disabled('node', $bundle),
'#states' => [
'visible' => [
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
'checked' => TRUE,
],
],
],
];
}
}
}
}
}
function entity_translation_unified_form_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form_object = $form_state
->getFormObject();
if (!$form_object instanceof EntityForm) {
return;
}
$entity = $form_object
->getEntity();
$entity_type_id = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
if (!empty($entity) && entity_translation_unified_form_bundle_enabled($entity_type_id, $bundle)) {
$key = array_search('::save', $form['actions']['submit']['#submit']);
if (isset($form['actions']['preview'])) {
$keyp = array_search('::preview', $form['actions']['preview']['#submit']);
}
if ($key !== FALSE) {
array_splice($form['actions']['submit']['#submit'], $key, 0, 'entity_translation_unified_form_node_form_submit');
if (isset($form['actions']['preview'])) {
array_splice($form['actions']['preview']['#submit'], $keyp, 0, 'entity_translation_unified_form_node_form_preview');
}
entity_translation_unified_form_add_fields($form, $form_state);
}
}
}
function entity_translation_unified_form_add_fields(&$form, FormStateInterface $form_state) {
$form_state
->loadInclude('entity_translation_unified_form', 'inc', 'entity_translation_unified_form.theme');
$form_object = $form_state
->getFormObject();
if ($form_object instanceof EntityForm) {
$entity = $form_object
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$entityManager = \Drupal::service('entity_field.manager');
$fields = $entityManager
->getFieldDefinitions($entity_type, $bundle);
$mode_plugin_id = entity_translation_unified_form_bundle_display_mode($entity_type, $bundle);
if ($mode_plugin_id != NULL) {
$sbs = entity_translation_unified_form_sbs_enabled($entity_type, $bundle);
if ($sbs) {
$mode_plugin_id = 'EntityTranslationUnifiedFormInlineMode';
}
$mode_plugin = entity_translation_unified_form_get_mode_plugin($mode_plugin_id);
foreach ($fields as $field_name => $field_definition) {
entity_translation_unified_form_node_insert_other_language_fields($form, $form_state, $field_definition, $mode_plugin);
}
}
}
}
function entity_translation_unified_form_def_value($val, $mono = false) {
if ($mono) {
return $val;
}
if (!is_array($val)) {
return $val;
}
if (isset($val['date'])) {
return $val['date'];
}
if (count($val) == 1) {
return current($val);
}
return $val;
}
function entity_translation_unified_form_build_field($form, $form_state, $entity, $entity_type, $bundle, $field_name, $langcode) {
$language_manager = \Drupal::languageManager();
$initial_language = $language_manager
->getConfigOverrideLanguage();
$translate_labels = entity_translation_unified_form_translate_labels_enabled($entity_type, $bundle);
if ($translate_labels) {
$language_manager
->setConfigOverrideLanguage($language_manager
->getLanguage($langcode));
\Drupal::service("entity_field.manager")
->clearCachedFieldDefinitions();
}
if ($entity
->hasTranslation($langcode)) {
$entity = $entity
->getTranslation($langcode);
}
else {
$entity = $entity
->addTranslation($langcode);
}
$form_display = EntityFormDisplay::collectRenderDisplay($entity, 'default');
if ($entity
->hasTranslation($langcode)) {
if ($entity_type == 'node' && $entity
->isLatestRevision()) {
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type);
if (!empty($entity
->id())) {
$latestTranslationAffectedRevisionId = $storage
->getLatestTranslationAffectedRevisionId($entity
->id(), $langcode);
if (is_numeric($latestTranslationAffectedRevisionId)) {
$entityOtherLangLatestRevision = $storage
->loadRevision($latestTranslationAffectedRevisionId);
}
}
if (empty($entityOtherLangLatestRevision)) {
$definition = $entity
->getFieldDefinition($field_name);
if (isset($definition) && $definition
->isTranslatable()) {
$items = $entity
->getTranslation($langcode)
->get($field_name);
}
else {
$items = $entity
->get($field_name);
}
}
else {
$definitionLatest = $entityOtherLangLatestRevision
->getFieldDefinition($field_name);
if (!empty($definitionLatest
->getType()) && $definitionLatest
->isTranslatable()) {
$items = $entityOtherLangLatestRevision
->getTranslation($langcode)
->get($field_name);
}
else {
$items = $entityOtherLangLatestRevision
->get($field_name);
}
}
}
else {
$items = $entity
->getTranslation($langcode)
->get($field_name);
}
}
else {
$items = $entity
->addTranslation($langcode)
->get($field_name);
}
$widget = $form_display
->getRenderer($field_name);
if (empty($widget)) {
if ($translate_labels) {
$language_manager
->setConfigOverrideLanguage($initial_language);
\Drupal::service("entity_field.manager")
->clearCachedFieldDefinitions();
}
return [];
}
$field_form = $widget
->form($items, $form, $form_state);
if ($entity
->isNew()) {
if (!$translate_labels) {
$language_manager
->setConfigOverrideLanguage($language_manager
->getLanguage($langcode));
\Drupal::service("entity_field.manager")
->clearCachedFieldDefinitions();
}
$field_definition = \Drupal\field\Entity\FieldConfig::loadByName($entity_type, $bundle, $field_name);
if ($field_definition) {
$def = $field_definition
->getDefaultValue($entity);
}
else {
$def = [];
}
if (!$translate_labels) {
$language_manager
->setConfigOverrideLanguage($initial_language);
\Drupal::service("entity_field.manager")
->clearCachedFieldDefinitions();
}
if (isset($def) and !empty($def)) {
if (isset($field_form['widget']['#default_value'])) {
$field_form['widget']['#default_value'] = entity_translation_unified_form_def_value($def, true);
}
else {
foreach ($def as $idx => $cont) {
if (isset($field_form['widget'][$idx]['value'])) {
if (!empty($cont)) {
$field_form['widget'][$idx]['value']['#default_value'] = entity_translation_unified_form_def_value($cont);
}
}
}
}
}
}
if ($translate_labels) {
$language_manager
->setConfigOverrideLanguage($initial_language);
\Drupal::service("entity_field.manager")
->clearCachedFieldDefinitions();
}
return $field_form;
}
function entity_translation_unified_form_node_insert_other_language_fields(&$form, &$form_state, $field_definition, $mode_plugin) {
$language_manager = \Drupal::languageManager();
$language = $language_manager
->getCurrentLanguage();
$form_object = $form_state
->getFormObject();
$entity = NULL;
if ($form_object instanceof EntityForm) {
$entity = $form_object
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$sbs = entity_translation_unified_form_sbs_enabled($entity_type, $bundle);
$field_name = $field_definition
->getName();
$field = FieldStorageConfig::loadByName($entity_type, $field_name);
$form_display = EntityFormDisplay::collectRenderDisplay($entity, 'default');
$display_options = $field_definition
->getDisplayOptions('form');
$hidden = empty($display_options) || isset($display_options['type']) && $display_options['type'] == 'hidden';
if ($field_definition
->isTranslatable() && !$hidden) {
$other_languages = EtufHelper::getOtherEnabledLanguages();
foreach ($other_languages as $other_langcode => $other_language) {
$current_language = $language_manager
->getLanguage($other_langcode);
$field_form = entity_translation_unified_form_build_field($form, $form_state, $entity, $entity_type, $bundle, $field_name, $other_langcode);
if (!empty($field_form)) {
$field_form['widget']['#field_name'] = $field_name . '-etuf-' . $other_langcode;
if (!is_array($field_form['widget']['#parents'])) {
$field_form['widget']['#parents'] = [];
}
$field_form['widget']['#parents'] = [];
$field_form['widget']['#parents'][] = $field_form['widget']['#field_name'];
if (isset($field_form['widget']['add_more'])) {
$field_form['widget']['add_more']['#name'] = $field_form['widget']['#field_name'] . '_add_more';
}
$field_form['#etuf_field_language'] = $current_language;
$field_wrapper = $mode_plugin
->getFieldThemeWrapper($form, $form_state, $field_form, $field_name, $current_language);
if (!empty($field_wrapper)) {
$field_form['#theme_wrappers'][] = $field_wrapper;
}
$mode_plugin
->fieldformAlter($form, $form_state, $field_form, $field_name, $current_language);
$form[$field_name][$other_langcode] = $field_form;
}
}
$form[$field_name]['#multilingual'] = TRUE;
$form[$field_name]['#etuf_field_language'] = $language;
if (!isset($form[$field_name]['#access'])) {
$form[$field_name]['#access'] = NULL;
}
$form[$field_name]['#attributes']['class'][] = 'etuf-translated-field';
$field_wrapper = $mode_plugin
->getFieldThemeWrapper($form, $form_state, $form[$field_name], $field_name, $language);
if (!empty($field_wrapper)) {
$form[$field_name]['#theme_wrappers'][] = $field_wrapper;
}
$mode_plugin
->fieldFormAlter($form, $form_state, $form[$field_name], $field_name, $language);
$field_group_wrapper = $mode_plugin
->getFieldGroupThemeWrapper($form, $form_state, $form[$field_name], $field_name);
if (!empty($field_group_wrapper)) {
$form[$field_name]['#theme_wrappers'][] = $field_group_wrapper;
}
}
elseif (entity_translation_unified_form_sbs_enabled($entity_type, $bundle) && !$hidden) {
if (isset($form[$field_name]['#prefix'])) {
$form[$field_name]['#prefix'] = '<div class="etuf-sbs-none">' . $form[$field_name]['#prefix'];
}
else {
$form[$field_name]['#prefix'] = '<div class="etuf-sbs-none">';
}
if (isset($form[$field_name]['#suffix'])) {
$form[$field_name]['#suffix'] = $form[$field_name]['#suffix'] . '</div>';
}
else {
$form[$field_name]['#suffix'] = '</div>';
}
}
if (entity_translation_unified_form_sbs_enabled($entity_type, $bundle)) {
if (!$hidden && isset($form[$field_name]['#weight'])) {
$form['etuf-' . $field_name . '-sep'] = [
'#markup' => '',
'#prefix' => '<div class="etuf-sbs-none">',
'#suffix' => '</div>',
'#weight' => $form[$field_name]['#weight'],
];
}
}
}
}
function entity_translation_unified_form_node_form_preview(array $form, FormStateInterface $form_state) {
$language = \Drupal::languageManager()
->getCurrentLanguage();
$langcode = $language
->getId();
$other_languages = EtufHelper::getOtherEnabledLanguages();
$form_object = $form_state
->getFormObject();
$entity = NULL;
if ($form_object instanceof EntityForm) {
$entity = $form_object
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$entityManager = \Drupal::service('entity_field.manager');
$fields = $entityManager
->getFieldDefinitions($entity_type, $bundle);
$values = $form_state
->getValues();
$stateOtherLang = '';
foreach ($other_languages as $other_langcode => $other_language) {
$action = 'add';
if ($entity
->hasTranslation($other_langcode)) {
$action = 'update';
$translation = $entity
->getTranslation($other_langcode);
}
else {
$translation = $entity
->addTranslation($other_langcode);
}
$translation
->setRevisionTranslationAffected(TRUE);
foreach ($fields as $field_name => $field_definition) {
$etuf_name = $field_name . '-etuf-' . $other_langcode;
$field_type = $field_definition
->getType();
if (isset($values[$etuf_name])) {
if ($field_type == 'metatag' && isset($values[$etuf_name])) {
$metatags_from_form = $values[$etuf_name];
$metatags_reset_form = reset($metatags_from_form);
$metatags_prep = [];
foreach ($metatags_reset_form as $outer_key => $metatags_part) {
if (is_array($metatags_part)) {
foreach ($metatags_part as $key => $value) {
$metatags_prep[$key] = $value;
}
}
}
$metatags_serialized = serialize($metatags_prep);
$translation
->set($field_name, $metatags_serialized);
}
elseif ($field_name == 'created' && $field_type == 'created') {
$dateObj = $values[$etuf_name][0]['value'];
$translation
->set($field_name, $dateObj
->getTimestamp());
}
elseif ($field_type == 'image') {
if (isset($values[$etuf_name][0]['fids'][0])) {
$numberOfFiles = count($values[$etuf_name]);
if ($numberOfFiles == 1) {
if (!isset($values[$etuf_name][0]['target_id'])) {
$values[$etuf_name][0]['target_id'] = $values[$etuf_name][0]['fids'][0];
unset($values[$etuf_name][0]['fids']);
}
}
elseif ($numberOfFiles > 1) {
for ($i = 0; $i < $numberOfFiles; $i++) {
if (!isset($values[$etuf_name][$i]['target_id']) && isset($values[$etuf_name][$i]['fids'][0])) {
$values[$etuf_name][$i]['target_id'] = $values[$etuf_name][$i]['fids'][0];
unset($values[$etuf_name][$i]['fids']);
}
}
}
}
$translation
->set($field_name, $values[$etuf_name]);
}
elseif (isset($field_name) && !is_null($translation)) {
$translation
->set($field_name, $values[$etuf_name]);
}
elseif ($field_name == 'created' && $field_type == 'created' && isset($values[$field_name])) {
$dateObj = $values[$field_name][0]['value'];
if (!is_null($dateObj)) {
$translation
->set($field_name, $dateObj
->getTimestamp());
}
}
elseif (isset($translation) && $field_name == 'revision_timestamp') {
$values[$etuf_name] = \Drupal::time()
->getCurrentTime();
}
elseif (isset($translation) && $field_name == 'changed' && $field_type == 'changed') {
$values[$etuf_name] = \Drupal::time()
->getCurrentTime();
}
}
}
if (!isset($once_only)) {
$once_only = TRUE;
if (!empty($stateOtherLang)) {
$entity
->setRevisionTranslationAffected(TRUE);
$entity
->setChangedTime(time());
}
}
if (method_exists($translation, 'getTitle') && empty($translation
->getTitle())) {
$title = $entity
->getTitle();
$translation
->setTitle($title);
}
}
}
}
function entity_translation_unified_form_node_form_submit(array $form, FormStateInterface $form_state) {
$language = \Drupal::languageManager()
->getCurrentLanguage();
$langcode = $language
->getId();
$other_languages = EtufHelper::getOtherEnabledLanguages();
$form_object = $form_state
->getFormObject();
$entity = NULL;
if ($form_object instanceof EntityForm) {
$entity = $form_object
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$entityManager = \Drupal::service('entity_field.manager');
$fields = $entityManager
->getFieldDefinitions($entity_type, $bundle);
$values = $form_state
->getValues();
if ($entity instanceof RevisionLogInterface) {
$lastRevisionLogMessage[$langcode] = $entity
->getRevisionLogMessage();
}
$stateOtherLang = '';
foreach ($other_languages as $other_langcode => $other_language) {
if ($entity instanceof RevisionLogInterface) {
$lastRevisionLogMessage[$other_langcode] = $entity->revision_log->value;
}
$action = 'add';
if ($entity
->hasTranslation($other_langcode)) {
$action = 'update';
$translation = $entity
->getTranslation($other_langcode);
}
else {
$translation = $entity
->addTranslation($other_langcode);
}
foreach ($fields as $field_name => $field_definition) {
$etuf_name = $field_name . '-etuf-' . $other_langcode;
$field_type = $field_definition
->getType();
if (isset($values[$etuf_name])) {
if ($field_type == 'metatag' && isset($values[$etuf_name])) {
$metatags_from_form = $values[$etuf_name];
$metatags_reset_form = reset($metatags_from_form);
$metatags_prep = [];
foreach ($metatags_reset_form as $outer_key => $metatags_part) {
if (is_array($metatags_part)) {
foreach ($metatags_part as $key => $value) {
$metatags_prep[$key] = $value;
}
}
}
$metatags_serialized = serialize($metatags_prep);
$translation
->set($field_name, $metatags_serialized);
}
elseif ($field_name == 'moderation_state') {
$moderationArray = reset($values[$etuf_name]);
$stateOtherLang = $moderationArray['value'];
$translation
->getTranslation($other_langcode)
->set($field_name, $stateOtherLang);
}
elseif ($field_name == 'created' && $field_type == 'created') {
$dateObj = $values[$etuf_name][0]['value'];
$translation
->set($field_name, $dateObj
->getTimestamp());
}
elseif ($field_type == 'image') {
if (isset($values[$etuf_name][0]['fids'][0])) {
$numberOfFiles = count($values[$etuf_name]);
if ($numberOfFiles == 1) {
if (!isset($values[$etuf_name][0]['target_id'])) {
$values[$etuf_name][0]['target_id'] = $values[$etuf_name][0]['fids'][0];
unset($values[$etuf_name][0]['fids']);
}
}
elseif ($numberOfFiles > 1) {
for ($i = 0; $i < $numberOfFiles; $i++) {
if (!isset($values[$etuf_name][$i]['target_id']) && isset($values[$etuf_name][$i]['fids'][0])) {
$values[$etuf_name][$i]['target_id'] = $values[$etuf_name][$i]['fids'][0];
unset($values[$etuf_name][$i]['fids']);
}
}
}
}
$translation
->set($field_name, $values[$etuf_name]);
}
elseif (isset($field_name) && !is_null($translation)) {
$translation
->set($field_name, $values[$etuf_name]);
}
elseif ($field_name == 'created' && $field_type == 'created' && isset($values[$field_name])) {
$dateObj = $values[$field_name][0]['value'];
if (!is_null($dateObj)) {
$translation
->set($field_name, $dateObj
->getTimestamp());
}
}
elseif (isset($translation) && $field_name == 'revision_timestamp') {
$values[$etuf_name] = \Drupal::time()
->getCurrentTime();
}
elseif (isset($translation) && $field_name == 'changed' && $field_type == 'changed') {
$values[$etuf_name] = \Drupal::time()
->getCurrentTime();
}
}
}
if (!is_null($translation)) {
if (!empty($stateOtherLang)) {
if ($entity instanceof RevisionLogInterface) {
$lastRevisionLogMessage[$other_langcode] = $translation
->getRevisionLogMessage();
}
$translation
->getTranslation($other_langcode)
->setNewRevision(TRUE);
$entity
->getTranslation($langcode)
->set('moderation_state', $stateOtherLang);
$translation
->getTranslation($other_langcode)
->set('moderation_state', $stateOtherLang);
if ($translation instanceof RevisionLogInterface) {
$current_uid = \Drupal::currentUser()
->id();
$translation
->getTranslation($other_langcode)
->setRevisionCreationTime(\Drupal::time()
->getRequestTime());
if (empty($lastRevisionLogMessage[$other_langcode])) {
$revisionLogMsg = $lastRevisionLogMessage[$langcode];
}
else {
$revisionLogMsg = $lastRevisionLogMessage[$other_langcode];
}
$translation
->getTranslation($other_langcode)
->setRevisionLogMessage($revisionLogMsg);
$translation
->getTranslation($other_langcode)
->setRevisionUserId($current_uid);
}
$translation
->setRevisionTranslationAffected(TRUE);
$translation
->setChangedTime(time());
}
drupal_register_shutdown_function('_custom_shutdown', $translation, $other_langcode, $stateOtherLang, $langcode);
}
if (!isset($once_only)) {
$once_only = TRUE;
if (!empty($stateOtherLang)) {
$entity
->setRevisionTranslationAffected(TRUE);
$entity
->setChangedTime(time());
}
}
if (method_exists($translation, 'getTitle') && empty($translation
->getTitle())) {
$title = $entity
->getTitle();
$translation
->setTitle($title);
}
}
}
}
function _custom_shutdown_menu($entity, $langcode) {
$language = \Drupal::languageManager()
->getLanguage($langcode);
$options = [
'absolute' => TRUE,
'language' => $language,
];
$absolute_base_url = Url::fromRoute('<front>', [], $options)
->toString();
$base_url = \Drupal::request()
->getBaseUrl();
$pos_base = stripos($absolute_base_url, $base_url);
$desired_base_url = substr($absolute_base_url, $pos_base);
$base_url = $desired_base_url;
if ($entity
->hasTranslation($langcode)) {
$entity = $entity
->getTranslation($langcode);
}
$e_type_id = $entity
->getEntityTypeId();
$path = \Drupal::service('path_alias.manager')
->getAliasByPath("/{$e_type_id}/" . $entity
->id(), $langcode);
$path = $base_url . $path;
$url = $entity
->toUrl('canonical', [], $options)
->toString();
if (!is_string($url)) {
$url = $path;
}
else {
$url = $url;
}
$debug = FALSE;
EtufHelper::addToLog('path:' . $path, $debug);
EtufHelper::addToLog('url:' . $url, $debug);
$url = str_replace('/' . $langcode . '/', '/', $url);
$path = str_replace('/' . $langcode . '/', '/', $path);
$test_alias = $path == "/{$e_type_id}/" . $entity
->id() ? TRUE : FALSE;
$vid = 0;
$latest_revision = etuf_latest_revision($entity
->id(), $vid, $langcode, $entity
->getEntityTypeId());
if ($test_alias || $url != $path) {
if ($test_alias) {
\Drupal::service('pathauto.generator')
->updateEntityAlias($latest_revision, 'insert', [
'language' => $langcode,
]);
}
else {
if ($url != $path) {
\Drupal::service('pathauto.generator')
->updateEntityAlias($latest_revision, 'update', [
'language' => $langcode,
]);
}
}
}
}
function _custom_shutdown($entity, $langcode, $moderation_state, $interfacelang) {
$vid = 0;
$latest_revision = etuf_latest_revision($entity
->id(), $vid, $langcode, $entity
->getEntityTypeId());
if (!empty($moderation_state)) {
$latest_is_valid = TRUE;
if ($latest_revision == FALSE) {
$latest_is_valid = FALSE;
}
if ($latest_is_valid) {
$latest_revision
->setSyncing(TRUE);
$latest_revision
->setRevisionTranslationAffected(TRUE);
$latest_revision
->set('moderation_state', $moderation_state);
$latest_revision
->save();
}
}
drupal_register_shutdown_function('\\Drupal\\entity_translation_unified_form\\EtufHelper::postCreateOrUpdateAutoTranslate', $entity
->id(), $langcode);
if ($latest_revision == FALSE) {
drupal_register_shutdown_function('_custom_shutdown_menu', $latest_revision, $langcode);
}
else {
drupal_register_shutdown_function('_custom_shutdown_menu', $entity, $langcode);
}
}
function entity_translation_unified_form_theme() {
return [
'entity_translation_unified_form__inline__field_wrapper' => [
'render element' => 'element',
'file' => 'entity_translation_unified_form.theme.inc',
],
'entity_translation_unified_form__inline__wrapper' => [
'render element' => 'element',
'file' => 'entity_translation_unified_form.theme.inc',
],
'entity_translation_unified_form__a11y_accordion_tabs__field_wrapper' => [
'render element' => 'element',
'file' => 'entity_translation_unified_form.theme.inc',
],
'entity_translation_unified_form__a11y_accordion_tabs__wrapper' => [
'render element' => 'element',
'file' => 'entity_translation_unified_form.theme.inc',
],
];
}
function etuf_latest_revision($id, &$vid, $lang, $entity_type = 'node') {
$query = \Drupal::entityTypeManager()
->getStorage($entity_type)
->getQuery();
$query
->latestRevision();
if ($entity_type == 'node') {
$query
->condition('nid', $id, '=');
}
if ($entity_type == 'taxonomy_term') {
$query
->condition('tid', $id, '=');
}
if ($entity_type == 'paragraph') {
$query
->condition('id', $id, '=');
}
if ($entity_type == 'user') {
$query
->condition('uid', $id, '=');
}
$latestRevisionResult = $query
->execute();
if (count($latestRevisionResult)) {
$node_revision_id = key($latestRevisionResult);
$vid = $node_revision_id;
$latestRevision = \Drupal::entityTypeManager()
->getStorage($entity_type)
->loadRevision($node_revision_id);
if ($latestRevision
->hasTranslation($lang) && $latestRevision
->language()
->getId() != $lang) {
$latestRevision = $latestRevision
->getTranslation($lang);
}
return $latestRevision;
}
return FALSE;
}