123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- /**
- * @file
- */
- namespace Drupal\edlp_corpus\Plugin\Block;
- use Drupal\Core\Block\BlockBase;
- use Drupal\Core\Link;
- use Drupal\Core\Url;
- use Drupal\taxonomy\Entity\Term;
- use Drupal\workflow\Entity\WorkflowManager;
- /**
- * Creates a 'Entrees' Block
- * @Block(
- * id = "block_edlp_entrees",
- * admin_label = @Translation("Edlp entrees block"),
- * )
- */
- class BlockEntrees extends BlockBase {
- /**
- * {@inheritdoc}
- */
- public function build() {
- $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
- $query = \Drupal::entityQuery('taxonomy_term')
- // ->sort('weight', 'DESC')
- // ->sort('name', 'DESC')
- ->condition('vid', 'entrees');
- $tids = $query->execute();
- // $terms = entity_load_multiple('taxonomy_term', $tids);
- // $terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadMultiple($terms);
- $terms = Term::loadMultiple($tids);
- $ordered_terms = [];
- foreach ($terms as $term) {
- // remove masqué
- $sid = WorkflowManager::getCurrentStateId($term, 'field_workflow');
- if($sid == 'generique_masque') continue;
- // translate the term
- $term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language);
- $name = $this->stripAccent($term->getName());
- $ordered_trans_terms[$name] = $term;
- }
- ksort($ordered_trans_terms);
- // dsm($terms);
- foreach ($ordered_trans_terms as $name => $term) {
- $tid = $term->id();
- $entree = array(
- 'tid'=>$tid
- );
- // term link
- $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term'=>$tid]);
- $url->setOptions(array(
- 'attributes' => array(
- 'class' => ['term-'.$tid, 'term-link'],
- 'tid'=>$tid,
- 'selector' => 'entree-term-link-'.$tid,
- 'data-drupal-link-system-path' => $url->getInternalPath()
- )
- ));
- $entree['term_link'] = Link::fromTextAndUrl($term->getName(), $url);
- // index link
- $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term'=>$tid]);
- $url->setOptions(array(
- 'attributes' => array(
- 'class' => ['index-link', 'ajax-link'],
- 'viewmode'=>'index',
- 'tid'=>$tid,
- 'selector' => 'entree-index-link-'.$tid,
- 'data-drupal-link-system-path' => $url->getInternalPath()
- )
- ));
- $entree['index_link'] = Link::fromTextAndUrl('index', $url);
- // notice-link
- $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term'=>$tid]);
- $url->setOptions(array(
- 'attributes' => array(
- 'class' => ['notice-link', 'ajax-link'],
- 'viewmode'=>'notice',
- 'tid'=>$tid,
- 'selector' => 'entree-notice-link-'.$tid,
- 'data-drupal-link-system-path' => $url->getInternalPath()
- )
- ));
- $entree['notice_link'] = Link::fromTextAndUrl('notice', $url);
- $entree['description'] = $term->get('description')->value;
- $entrees[] = $entree;
- }
- return array (
- '#theme' => 'blockentrees',
- '#entrees_items' => $entrees,
- '#attached'=>array(
- 'library' => array('edlp_corpus/corpus'),
- 'drupalSettings' => array(
- 'basepath' => base_path()
- )
- )
- );
- }
- private function stripAccent($str){
- return strtr(
- utf8_decode($str),
- utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'),
- 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
- }
- }
|