BlockEntrees.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @file
  4. */
  5. namespace Drupal\edlp_corpus\Plugin\Block;
  6. use Drupal\Core\Block\BlockBase;
  7. use Drupal\Core\Link;
  8. use Drupal\Core\Url;
  9. use Drupal\taxonomy\Entity\Term;
  10. use Drupal\workflow\Entity\WorkflowManager;
  11. /**
  12. * Creates a 'Entrees' Block
  13. * @Block(
  14. * id = "block_edlp_entrees",
  15. * admin_label = @Translation("Edlp entrees block"),
  16. * )
  17. */
  18. class BlockEntrees extends BlockBase {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function build() {
  23. $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  24. $query = \Drupal::entityQuery('taxonomy_term')
  25. // ->sort('weight', 'DESC')
  26. // ->sort('name', 'DESC')
  27. ->condition('vid', 'entrees');
  28. $tids = $query->execute();
  29. // $terms = entity_load_multiple('taxonomy_term', $tids);
  30. // $terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadMultiple($t‌​erms);
  31. $terms = Term::loadMultiple($tids);
  32. $ordered_terms = [];
  33. foreach ($terms as $term) {
  34. // remove masqué
  35. $sid = WorkflowManager::getCurrentStateId($term, 'field_workflow');
  36. if($sid == 'generique_masque') continue;
  37. // translate the term
  38. $term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language);
  39. $name = $this->stripAccent($term->getName());
  40. $ordered_trans_terms[$name] = $term;
  41. }
  42. ksort($ordered_trans_terms);
  43. // dsm($terms);
  44. foreach ($ordered_trans_terms as $name => $term) {
  45. $tid = $term->id();
  46. $entree = array(
  47. 'tid'=>$tid
  48. );
  49. // term link
  50. $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term'=>$tid]);
  51. $url->setOptions(array(
  52. 'attributes' => array(
  53. 'class' => ['term-'.$tid, 'term-link'],
  54. 'tid'=>$tid,
  55. 'selector' => 'entree-term-link-'.$tid,
  56. 'data-drupal-link-system-path' => $url->getInternalPath()
  57. )
  58. ));
  59. $entree['term_link'] = Link::fromTextAndUrl($term->getName(), $url);
  60. // index link
  61. $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term'=>$tid]);
  62. $url->setOptions(array(
  63. 'attributes' => array(
  64. 'class' => ['index-link', 'ajax-link'],
  65. 'viewmode'=>'index',
  66. 'tid'=>$tid,
  67. 'selector' => 'entree-index-link-'.$tid,
  68. 'data-drupal-link-system-path' => $url->getInternalPath()
  69. )
  70. ));
  71. $entree['index_link'] = Link::fromTextAndUrl('index', $url);
  72. // notice-link
  73. $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term'=>$tid]);
  74. $url->setOptions(array(
  75. 'attributes' => array(
  76. 'class' => ['notice-link', 'ajax-link'],
  77. 'viewmode'=>'notice',
  78. 'tid'=>$tid,
  79. 'selector' => 'entree-notice-link-'.$tid,
  80. 'data-drupal-link-system-path' => $url->getInternalPath()
  81. )
  82. ));
  83. $entree['notice_link'] = Link::fromTextAndUrl('notice', $url);
  84. $entree['description'] = $term->get('description')->value;
  85. $entrees[] = $entree;
  86. }
  87. return array (
  88. '#theme' => 'blockentrees',
  89. '#entrees_items' => $entrees,
  90. '#attached'=>array(
  91. 'library' => array('edlp_corpus/corpus'),
  92. 'drupalSettings' => array(
  93. 'basepath' => base_path()
  94. )
  95. )
  96. );
  97. }
  98. private function stripAccent($str){
  99. return strtr(
  100. utf8_decode($str),
  101. utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'),
  102. 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
  103. }
  104. }