View source
<?php
namespace Drupal\content_translation\Plugin\migrate\source\d7;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
class EntityTranslationSettings extends DrupalSqlBase {
public function query() {
$query = $this
->select('variable', 'v')
->fields('v', [
'name',
'value',
]);
$condition = $query
->orConditionGroup()
->condition('name', 'entity_translation_entity_types')
->condition('name', 'entity_translation_taxonomy')
->condition('name', 'entity_translation_settings_%', 'LIKE')
->condition('name', 'language_content_type_%', 'LIKE');
$query
->condition($condition);
return $query;
}
protected function initializeIterator() {
$results = array_map('unserialize', $this
->prepareQuery()
->execute()
->fetchAllKeyed());
$rows = [];
$entity_types = array_filter($results['entity_translation_entity_types']);
if (empty($entity_types)) {
return new \ArrayIterator($rows);
}
$node_types = [];
foreach ($results as $name => $value) {
if (preg_match('/^language_content_type_(.+)$/', $name, $matches) && (int) $value === 4) {
$node_types[] = $matches[1];
}
}
$vocabularies = [];
if (isset($results['entity_translation_taxonomy']) && is_array($results['entity_translation_taxonomy'])) {
$vocabularies = array_keys(array_filter($results['entity_translation_taxonomy']));
}
if (in_array('node', $entity_types, TRUE) && !empty($node_types)) {
foreach ($node_types as $node_type) {
$settings = $results['entity_translation_settings_node__' . $node_type] ?? [];
$rows[] = [
'id' => 'node.' . $node_type,
'target_entity_type_id' => 'node',
'target_bundle' => $node_type,
'default_langcode' => $settings['default_language'] ?? 'und',
'language_alterable' => isset($settings['hide_language_selector']) ? (bool) (!$settings['hide_language_selector']) : TRUE,
'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
];
}
}
if (in_array('comment', $entity_types, TRUE) && !empty($node_types)) {
foreach ($node_types as $node_type) {
$settings = $results['entity_translation_settings_comment__comment_node_' . $node_type] ?? [];
$bundle = $node_type == 'forum' ? 'comment_forum' : 'comment_node_' . $node_type;
$rows[] = [
'id' => 'comment.' . $bundle,
'target_entity_type_id' => 'comment',
'target_bundle' => $bundle,
'default_langcode' => $settings['default_language'] ?? 'xx-et-current',
'language_alterable' => isset($settings['hide_language_selector']) ? (bool) (!$settings['hide_language_selector']) : FALSE,
'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
];
}
}
if (in_array('taxonomy_term', $entity_types, TRUE) && !empty($vocabularies)) {
foreach ($vocabularies as $vocabulary) {
$settings = $results['entity_translation_settings_taxonomy_term__' . $vocabulary] ?? [];
$rows[] = [
'id' => 'taxonomy_term.' . $vocabulary,
'target_entity_type_id' => 'taxonomy_term',
'target_bundle' => $vocabulary,
'default_langcode' => $settings['default_language'] ?? 'xx-et-default',
'language_alterable' => isset($settings['hide_language_selector']) ? (bool) (!$settings['hide_language_selector']) : FALSE,
'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
];
}
}
if (in_array('user', $entity_types, TRUE)) {
$settings = $results['entity_translation_settings_user__user'] ?? [];
$rows[] = [
'id' => 'user.user',
'target_entity_type_id' => 'user',
'target_bundle' => 'user',
'default_langcode' => $settings['default_language'] ?? 'xx-et-default',
'language_alterable' => isset($settings['hide_language_selector']) ? (bool) (!$settings['hide_language_selector']) : FALSE,
'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
];
}
return new \ArrayIterator($rows);
}
public function fields() {
return [
'id' => $this
->t('The configuration ID'),
'target_entity_type_id' => $this
->t('The target entity type ID'),
'target_bundle' => $this
->t('The target bundle'),
'default_langcode' => $this
->t('The default language'),
'language_alterable' => $this
->t('Whether to show language selector on create and edit pages'),
'untranslatable_fields_hide' => $this
->t('Whether to hide non translatable fields on translation forms'),
];
}
public function getIds() {
$ids['id']['type'] = 'string';
return $ids;
}
protected function doCount() {
return (int) $this
->initializeIterator()
->count();
}
}