Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
- }
- }
Add Comment
Please, Sign In to add comment