View source
<?php
namespace Drupal\metatag_views\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Url;
use Drupal\metatag\MetatagManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Config\ConfigFactoryInterface;
class MetatagViewsTranslationController extends ControllerBase {
protected $viewStorage;
protected $metatagManager;
protected $languageManager;
protected $requestStack;
protected $configFactory;
public function __construct(EntityStorageInterface $viewStorage, MetatagManagerInterface $metatagManager, LanguageManagerInterface $languageManager, RequestStack $request_stack, ConfigFactoryInterface $config_factory) {
$this->viewStorage = $viewStorage;
$this->metatagManager = $metatagManager;
$this->languageManager = $languageManager;
$this->requestStack = $request_stack;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('view'), $container
->get('metatag.manager'), $container
->get('language_manager'), $container
->get('request_stack'), $container
->get('config.factory'));
}
public function itemPage() {
$view_id = $this->requestStack
->get('view_id');
$display_id = $this->requestStack
->get('display_id');
$view = $this->viewStorage
->load($view_id);
$original_langcode = $view
->language()
->getId();
$config_name = $view
->getConfigDependencyName();
$config_path = 'display.' . $display_id . '.display_options.display_extenders.metatag_display_extender.metatags';
$configuration = $this->configFactory
->get($config_name);
$config_source = $configuration
->getOriginal($config_path, FALSE);
$page['languages'] = [
'#type' => 'table',
'#header' => [
$this
->t('Language'),
$this
->t('Operations'),
],
];
$languages = $this->languageManager
->getLanguages();
foreach ($languages as $language) {
$langcode = $language
->getId();
$language_name = $language
->getName();
$operations = [];
if ($langcode == $original_langcode) {
$language_name = '<strong>' . $this
->t('@language (original)', [
'@language' => $language_name,
]) . '</strong>';
$operations['edit'] = [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute('metatag_views.metatags.edit', [
'view_id' => $view_id,
'display_id' => $display_id,
]),
];
}
else {
$config_translation = $this->languageManager
->getLanguageConfigOverride($langcode, $config_name)
->get($config_path);
if (!$config_translation || $config_translation == $config_source) {
$operations['add'] = [
'title' => $this
->t('Add'),
'url' => Url::fromRoute('metatag_views.metatags.translate', [
'view_id' => $view_id,
'display_id' => $display_id,
'langcode' => $langcode,
]),
];
}
else {
$operations['edit'] = [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute('metatag_views.metatags.translate', [
'view_id' => $view_id,
'display_id' => $display_id,
'langcode' => $langcode,
]),
];
}
}
$page['languages'][$langcode]['language'] = [
'#markup' => $language_name,
];
$page['languages'][$langcode]['operations'] = [
'#type' => 'operations',
'#links' => $operations,
];
}
return $page;
}
}