View source
<?php
namespace Drupal\metatag_views\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\metatag\MetatagManagerInterface;
use Drupal\metatag\MetatagTagPluginManager;
use Drupal\metatag\MetatagToken;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\metatag_views\MetatagViewsValuesCleanerTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class MetatagViewsTranslationForm extends FormBase {
use MetatagViewsValuesCleanerTrait;
use StringTranslationTrait;
protected $metatagManager;
protected $languageManager;
protected $viewsManager;
protected $tokenService;
protected $tagPluginManager;
protected $view;
protected $viewId;
protected $displayId = 'default';
protected $language;
protected $sourceLanguage;
protected $baseData = [];
public function __construct(MetatagManagerInterface $metatag_manager, EntityTypeManagerInterface $entity_type_manager, MetatagToken $token, MetatagTagPluginManager $tagPluginManager, ConfigurableLanguageManagerInterface $language_manager) {
$this->metatagManager = $metatag_manager;
$this->viewsManager = $entity_type_manager
->getStorage('view');
$this->tokenService = $token;
$this->tagPluginManager = $tagPluginManager;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('metatag.manager'), $container
->get('entity_type.manager'), $container
->get('metatag.token'), $container
->get('plugin.manager.metatag.tag'), $container
->get('language_manager'));
}
public function getFormId() {
return 'metatag_views_translate_form';
}
protected function prepareValues() {
$config_name = $this->view
->getConfigDependencyName();
$config_path = 'display.' . $this->displayId . '.display_options.display_extenders.metatag_display_extender.metatags';
$configuration = $this
->configFactory()
->get($config_name);
$this->baseData = $configuration
->getOriginal($config_path, FALSE);
$original_language = $this->languageManager
->getConfigOverrideLanguage();
$this->languageManager
->setConfigOverrideLanguage($this->language);
$configuration = $this
->configFactory()
->get($config_name);
$translated_values = $configuration
->get($config_path);
$this->languageManager
->setConfigOverrideLanguage($original_language);
return $translated_values;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->viewId = $this
->getRequest()
->get('view_id');
$this->displayId = $this
->getRequest()
->get('display_id');
$langcode = $this
->getRequest()
->get('langcode');
$this->view = $this->viewsManager
->load($this->viewId);
$this->language = $this->languageManager
->getLanguage($langcode);
$this->sourceLanguage = $this->view
->language();
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'config_translation/drupal.config_translation.admin';
$form['#title'] = $this
->t('Edit @language translation for %view: %display metatags', [
'%view' => $this->view
->label(),
'%display' => $this->view
->getDisplay($this->displayId)['display_title'],
'@language' => $this->language
->getName(),
]);
$form['metatags'] = $this
->form($form, $this
->prepareValues());
$form['metatags']['#title'] = $this
->t('Metatags');
$form['metatags']['#type'] = 'fieldset';
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function form(array $element, array $translated_values) {
$translated_values = $this
->clearMetatagViewsDisallowedValues($translated_values);
$source_values = $this
->removeEmptyTags($this->baseData);
$element += [
'#type' => 'details',
];
$element += $this->tokenService
->tokenBrowser([
'view',
]);
foreach ($source_values as $tag_id => $value) {
$tag = $this->tagPluginManager
->createInstance($tag_id);
$tag
->setValue($translated_values[$tag_id]);
$form_element = $tag
->form($element);
$element[$tag_id] = [
'#theme' => 'config_translation_manage_form_element',
'source' => [
'#type' => 'item',
'#title' => $form_element['#title'],
'#markup' => $value,
],
'translation' => $form_element,
];
}
return $element;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValue('metatags');
$translated_values = array_combine(array_keys($values), array_column($values, 'translation'));
$config_name = $this->view
->getConfigDependencyName();
$config_path = 'display.' . $this->displayId . '.display_options.display_extenders.metatag_display_extender.metatags';
$base_config = $this
->configFactory()
->getEditable($config_name);
$config_translation = $this->languageManager
->getLanguageConfigOverride($this->language
->getId(), $config_name);
$source_values = $this
->removeEmptyTags($base_config
->get($config_path));
if ($source_values !== $translated_values) {
$config_translation
->set($config_path, $translated_values);
}
else {
$config_translation
->clear($config_path);
}
$saved_config = $config_translation
->get();
if (empty($saved_config)) {
$config_translation
->delete();
}
else {
$config_translation
->save();
}
$form_state
->setRedirect('metatag_views.metatags.translate_overview', [
'view_id' => $this->viewId,
'display_id' => $this->displayId,
]);
$this
->messenger()
->addMessage($this
->t('Successfully updated @language translation.', [
'@language' => $this->language
->getName(),
]));
}
}