class MetatagViewsTranslationForm in Metatag 8
Defines a form for translating meta tags for views.
@package Drupal\metatag_views\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\metatag_views\Form\MetatagViewsTranslationForm uses StringTranslationTrait, MetatagViewsValuesCleanerTrait
Expanded class hierarchy of MetatagViewsTranslationForm
File
- metatag_views/
src/ Form/ MetatagViewsTranslationForm.php, line 21
Namespace
Drupal\metatag_views\FormView source
class MetatagViewsTranslationForm extends FormBase {
use MetatagViewsValuesCleanerTrait;
use StringTranslationTrait;
/**
* Drupal\metatag\MetatagManager definition.
*
* @var \Drupal\metatag\MetatagManager
*/
protected $metatagManager;
/**
* The language manager.
*
* @var \Drupal\language\ConfigurableLanguageManagerInterface
*/
protected $languageManager;
/**
* The Views manager.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $viewsManager;
/**
* The Metatag token service.
*
* @var \Drupal\metatag\MetatagToken
*/
protected $tokenService;
/**
* The Metatag tag plugin manager.
*
* @var \Drupal\metatag\MetatagTagPluginManager
*/
protected $tagPluginManager;
/**
* The View entity object.
*
* @var \Drupal\views\ViewEntityInterface
*/
protected $view;
/**
* View ID.
*
* @var string
*/
protected $viewId;
/**
* View display ID.
*
* @var string
*/
protected $displayId = 'default';
/**
* The language of the translation.
*
* @var \Drupal\Core\Language\LanguageInterface
*/
protected $language;
/**
* The language of the translation source.
*
* @var \Drupal\Core\Language\LanguageInterface
*/
protected $sourceLanguage;
/**
* An array of base language data.
*
* @var array
*/
protected $baseData = [];
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'metatag_views_translate_form';
}
/**
* Gets the translated values while storing a copy of the original values.
*/
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);
// Set the translation target language on the configuration factory.
$original_language = $this->languageManager
->getConfigOverrideLanguage();
$this->languageManager
->setConfigOverrideLanguage($this->language);
// Read in translated values.
$configuration = $this
->configFactory()
->get($config_name);
$translated_values = $configuration
->get($config_path);
// Set the configuration language back.
$this->languageManager
->setConfigOverrideLanguage($original_language);
return $translated_values;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Get the parameters from request.
$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();
// Get meta tags from the view entity.
$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;
}
/**
* Add the translation form element for meta tags available in the source.
*/
public function form(array $element, array $translated_values) {
$translated_values = $this
->clearMetatagViewsDisallowedValues($translated_values);
// Only offer form elements for tags present in the source language.
$source_values = $this
->removeEmptyTags($this->baseData);
// Add the outer fieldset.
$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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Get the values of metatags.
$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';
// Set configuration values based on form submission and source values.
$base_config = $this
->configFactory()
->getEditable($config_name);
$config_translation = $this->languageManager
->getLanguageConfigOverride($this->language
->getId(), $config_name);
// Save the configuration values, if they are different from the source
// values in the base configuration. Otherwise remove the override.
$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);
}
// If no overrides, delete language specific configuration file.
$saved_config = $config_translation
->get();
if (empty($saved_config)) {
$config_translation
->delete();
}
else {
$config_translation
->save();
}
// Redirect back to the views list.
$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(),
]));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
MetatagViewsTranslationForm:: |
protected | property | An array of base language data. | |
MetatagViewsTranslationForm:: |
protected | property | View display ID. | |
MetatagViewsTranslationForm:: |
protected | property | The language of the translation. | |
MetatagViewsTranslationForm:: |
protected | property | The language manager. | |
MetatagViewsTranslationForm:: |
protected | property | Drupal\metatag\MetatagManager definition. | |
MetatagViewsTranslationForm:: |
protected | property | The language of the translation source. | |
MetatagViewsTranslationForm:: |
protected | property | The Metatag tag plugin manager. | |
MetatagViewsTranslationForm:: |
protected | property | The Metatag token service. | |
MetatagViewsTranslationForm:: |
protected | property | The View entity object. | |
MetatagViewsTranslationForm:: |
protected | property | View ID. | |
MetatagViewsTranslationForm:: |
protected | property | The Views manager. | |
MetatagViewsTranslationForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
MetatagViewsTranslationForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
MetatagViewsTranslationForm:: |
public | function | Add the translation form element for meta tags available in the source. | |
MetatagViewsTranslationForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MetatagViewsTranslationForm:: |
protected | function | Gets the translated values while storing a copy of the original values. | |
MetatagViewsTranslationForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
MetatagViewsTranslationForm:: |
public | function | ||
MetatagViewsValuesCleanerTrait:: |
public | function | Clears the metatag form state values from illegal elements. | |
MetatagViewsValuesCleanerTrait:: |
public | function | Removes tags that are empty. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |