Skip to content

Instantly share code, notes, and snippets.

@svetlio
Created January 16, 2019 13:16
Show Gist options
  • Save svetlio/c602b6ff23053fba3cc71ed586cf1305 to your computer and use it in GitHub Desktop.
Save svetlio/c602b6ff23053fba3cc71ed586cf1305 to your computer and use it in GitHub Desktop.
Manage vocabularies page to show vocabularies translation in other languages too.
<?php
namespace Drupal\tools_ext\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
/**
* Class DefaultController.
*/
class ManageVocabulariesController extends ControllerBase {
/**
* Drupal\language\ConfigurableLanguageManagerInterface definition.
*
* @var \Drupal\language\ConfigurableLanguageManagerInterface
*/
protected $languageManager;
/**
* Drupal\Core\Session\AccountProxyInterface definition.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* Drupal\Core\Entity\EntityRepositoryInterface definition.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new DefaultController object.
*/
public function __construct(ConfigurableLanguageManagerInterface $language_manager, AccountProxyInterface $current_user, EntityRepositoryInterface $entity_repository, EntityTypeManagerInterface $entity_type_manager) {
$this->languageManager = $language_manager;
$this->currentUser = $current_user;
$this->entityRepository = $entity_repository;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('language_manager'),
$container->get('current_user'),
$container->get('entity.repository'),
$container->get('entity_type.manager')
);
}
/**
* @return renderable array of elements
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function content() {
$rows = [];
$empty = $this->t('No vocabularies found.');
$current_lang_code = $this->languageManager->getCurrentLanguage()->getId();
$vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->loadMultiple();
$header = [
$this->t('Vocabulary name'),
$this->t('Description'),
$this->t('List terms')
];
if (count($vocabularies)) {
foreach ($vocabularies AS $vocab) {
// vocabulary get translated
$vocab_translated = $this->entityRepository->getTranslationFromContext($vocab, $current_lang_code);
// ToDo: check current user for permissions to access current vocab.
// then show the link
$url = Url::fromRoute('entity.taxonomy_vocabulary.overview_form', ['taxonomy_vocabulary' => $vocab->id()]);
$link = Link::fromTextAndUrl($this->t('List'), $url);
$rows[] = [
'data' => [
$vocab_translated->get('name'),
$vocab_translated->get('description'),
$link
]
];
}
}
$elements['vocabularies_table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $empty
];
return $elements;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment