MetatagsFieldProcessor.php in Translation Management Tool 8
File
sources/content/src/MetatagsFieldProcessor.php
View source
<?php
namespace Drupal\tmgmt_content;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Render\Element;
class MetatagsFieldProcessor extends DefaultFieldProcessor {
public function extractTranslatableData(FieldItemListInterface $field) {
$metatag_manager = \Drupal::service('metatag.manager');
$meta_tag_values = unserialize($field->value);
if (empty($meta_tag_values) || !is_array($meta_tag_values)) {
return [];
}
$groups_and_tags = $metatag_manager
->sortedGroupsWithTags();
$blacklist_tags = [
'robots',
'referrer',
'twitter_cards_type',
];
$data = [];
foreach ($groups_and_tags as $group_name => $group) {
if (!isset($group['tags'])) {
continue;
}
$tags = array_intersect_key($group['tags'], $meta_tag_values);
if ($tags) {
$data[$group_name] = [
'#label' => $group['label'],
];
foreach ($tags as $tag_name => $tag) {
$data[$group_name][$tag_name] = [
'#translate' => !in_array($tag_name, $blacklist_tags),
'#text' => $meta_tag_values[$tag_name],
'#label' => $tag['label'],
];
}
}
}
if (!empty($data)) {
$data['#label'] = $field
->getFieldDefinition()
->getLabel();
}
return $data;
}
public function setTranslations($field_data, FieldItemListInterface $field) {
$meta_tags_values = [];
foreach (Element::children($field_data) as $group_name) {
foreach (Element::children($field_data[$group_name]) as $tag_name) {
$property_data = $field_data[$group_name][$tag_name];
if (isset($property_data['#translation']['#text']) && $property_data['#translate']) {
$meta_tags_values[$tag_name] = $property_data['#translation']['#text'];
}
else {
$meta_tags_values[$tag_name] = $property_data['#text'];
}
}
}
$field->value = serialize($meta_tags_values);
}
}