Guest User

Untitled

a guest
Jan 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\tools_ext\Controller;
  4.  
  5. use Drupal\Core\Controller\ControllerBase;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Drupal\language\ConfigurableLanguageManagerInterface;
  8. use Drupal\Core\Session\AccountProxyInterface;
  9. use Drupal\Core\Entity\EntityRepositoryInterface;
  10. use Drupal\Core\Entity\EntityTypeManagerInterface;
  11. use Drupal\Core\Url;
  12. use Drupal\Core\Link;
  13.  
  14. /**
  15. * Class DefaultController.
  16. */
  17. class ManageVocabulariesController extends ControllerBase {
  18.  
  19. /**
  20. * Drupal\language\ConfigurableLanguageManagerInterface definition.
  21. *
  22. * @var \Drupal\language\ConfigurableLanguageManagerInterface
  23. */
  24. protected $languageManager;
  25.  
  26. /**
  27. * Drupal\Core\Session\AccountProxyInterface definition.
  28. *
  29. * @var \Drupal\Core\Session\AccountProxyInterface
  30. */
  31. protected $currentUser;
  32.  
  33. /**
  34. * Drupal\Core\Entity\EntityRepositoryInterface definition.
  35. *
  36. * @var \Drupal\Core\Entity\EntityRepositoryInterface
  37. */
  38. protected $entityRepository;
  39.  
  40. /**
  41. * Drupal\Core\Entity\EntityTypeManagerInterface definition.
  42. *
  43. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  44. */
  45. protected $entityTypeManager;
  46.  
  47. /**
  48. * Constructs a new DefaultController object.
  49. */
  50. public function __construct(ConfigurableLanguageManagerInterface $language_manager, AccountProxyInterface $current_user, EntityRepositoryInterface $entity_repository, EntityTypeManagerInterface $entity_type_manager) {
  51. $this->languageManager = $language_manager;
  52. $this->currentUser = $current_user;
  53. $this->entityRepository = $entity_repository;
  54. $this->entityTypeManager = $entity_type_manager;
  55. }
  56.  
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public static function create(ContainerInterface $container) {
  61. return new static(
  62. $container->get('language_manager'),
  63. $container->get('current_user'),
  64. $container->get('entity.repository'),
  65. $container->get('entity_type.manager')
  66. );
  67. }
  68.  
  69. /**
  70. * @return renderable array of elements
  71. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  72. * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
  73. */
  74. public function content() {
  75. $rows = [];
  76. $empty = $this->t('No vocabularies found.');
  77.  
  78. $current_lang_code = $this->languageManager->getCurrentLanguage()->getId();
  79. $vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->loadMultiple();
  80.  
  81. $header = [
  82. $this->t('Vocabulary name'),
  83. $this->t('Description'),
  84. $this->t('List terms')
  85. ];
  86.  
  87. if (count($vocabularies)) {
  88. foreach ($vocabularies AS $vocab) {
  89. // vocabulary get translated
  90. $vocab_translated = $this->entityRepository->getTranslationFromContext($vocab, $current_lang_code);
  91.  
  92. // ToDo: check current user for permissions to access current vocab.
  93. // then show the link
  94. $url = Url::fromRoute('entity.taxonomy_vocabulary.overview_form', ['taxonomy_vocabulary' => $vocab->id()]);
  95. $link = Link::fromTextAndUrl($this->t('List'), $url);
  96.  
  97. $rows[] = [
  98. 'data' => [
  99. $vocab_translated->get('name'),
  100. $vocab_translated->get('description'),
  101. $link
  102. ]
  103. ];
  104. }
  105. }
  106.  
  107. $elements['vocabularies_table'] = [
  108. '#type' => 'table',
  109. '#header' => $header,
  110. '#rows' => $rows,
  111. '#empty' => $empty
  112. ];
  113.  
  114. return $elements;
  115. }
  116.  
  117. }
Add Comment
Please, Sign In to add comment