edlp_corpus module : created twig template and preprocessor for entrees bloc output

This commit is contained in:
Bachir Soussi Chiadmi
2018-01-07 19:15:11 +01:00
parent eeb060a31e
commit 7186c1648c
4 changed files with 113 additions and 65 deletions
@@ -0,0 +1,73 @@
<?php
function template_preprocess_blockentrees(&$vars){
$entrees = array (
'#theme' => 'item_list',
'#items' => array(),
);
foreach ($vars['entrees_items'] as $entree) {
$tid = $entree['tid'];
$term_link = $entree['term_link']->toRenderable();
$term_link['#prefix'] = '<span class="oblique-wrapper">';
$term_link['#suffix'] = "</span>";
$term_link["#attributes"] = array(
"class" => array('term-'.$tid, 'term-link'),
"tid" => $tid
);
$index_link = $entree['index_link']->toRenderable();
$index_link['#prefix'] = '<span class="oblique-wrapper">';
$index_link['#suffix'] = "</span>";
$index_link['#attributes'] = array(
'class' => array('index-link'),
"tid" => $tid
);
$notice_link = $entree['notice_link']->toRenderable();
$notice_link['#prefix'] = '<span class="oblique-wrapper">';
$notice_link['#suffix'] = "</span>";
$notice_link['#attributes'] = array(
'class' => array('notice-link'),
"tid" => $tid
);
$entree = array(
'#wrapper_attributes' => array(
'class' => array('term-'.$tid, 'entree'),
'tid' => $tid
),
'term_link' => $term_link,
'entree_content' => array(
'#type' => 'container',
'#attributes' => array(
'class' => 'entree-content'
),
'index_link' => $index_link,
'notice_link' => $notice_link,
'description' => array(
'#type' => 'container',
'#markup' => $entree['description'],
'#attributes' => array('class'=>'term-description')
)
)
);
// if (isset($entrees) && count($entrees) == count($terms)-3) {
// $entree['#wrapper_attributes']['class'][] = 'opened';
// }
// dpm($entree);
$entrees['#items'][] = $entree;
}
$vars['entrees'] = render($entrees);
}
@@ -7,16 +7,35 @@
# @Last modified time: 20-12-2017
# @License: GPL-V3
/**
* Implements hook_theme().
*/
function edlp_corpus_theme($existing, $type, $theme, $path) {
// @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
return array(
'blockentrees' => array(
// 'render element' => '',
'file' => 'blockentrees.inc',
'variables' => array(
'entrees_items' => array(),
),
),
);
}
/**
* Implements hook_page_attachments().
* @param array $attachments
*/
function edlp_corpus_page_attachments(array &$attachments) {
//add here any conditions if you need to limit the pages
if (\Drupal::service('path.matcher')->isFrontPage()) {
// $attachments['#attached']['library'][] = 'edlp_corpus/corpus';
// $attachments['#attached']['drupalSettings']['basepath'] = base_path();
// $attachments['#attached']['drupalSettings']['pathtoedlpcorpus'] = base_path() . drupal_get_path('module', 'edlp_corpus');
// $attachments['#attached']['drupalSettings']['pathtotfiles'] = PublicStream::basePath();
}
}
// function edlp_corpus_page_attachments(array &$attachments) {
// //add here any conditions if you need to limit the pages
// if (\Drupal::service('path.matcher')->isFrontPage()) {
// // $attachments['#attached']['library'][] = 'edlp_corpus/corpus';
// // $attachments['#attached']['drupalSettings']['basepath'] = base_path();
// // $attachments['#attached']['drupalSettings']['pathtoedlpcorpus'] = base_path() . drupal_get_path('module', 'edlp_corpus');
// // $attachments['#attached']['drupalSettings']['pathtotfiles'] = PublicStream::basePath();
// }
// }
@@ -31,76 +31,30 @@ class BlockEntrees extends BlockBase {
// dsm($terms);
foreach ($terms as $term) {
// dpm($term->toArray());
$tid = $term->id();
$name = $term->getName();
$term_url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]);
$term_link = Link::fromTextAndUrl($name, $term_url)->toRenderable();
$term_link['#prefix'] = '<span class="oblique-wrapper">';
$term_link['#suffix'] = "</span>";
$term_link["#attributes"] = array(
"class" => array('term-'.$term->id(), 'term-link'),
"tid" => $term->id()
$entree = array(
'tid'=>$tid
);
$term_url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]);
$entree['term_link'] = Link::fromTextAndUrl($name, $term_url);
$index_url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]);
$index_link = Link::fromTextAndUrl('index', $index_url)->toRenderable();
$index_link['#prefix'] = '<span class="oblique-wrapper">';
$index_link['#suffix'] = "</span>";
$index_link['#attributes'] = array(
'class' => array('index-link'),
"tid" => $term->id()
);
$entree['index_link'] = Link::fromTextAndUrl('index', $index_url);
$notice_url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]);
$notice_link = Link::fromTextAndUrl('notice', $notice_url)->toRenderable();
$notice_link['#prefix'] = '<span class="oblique-wrapper">';
$notice_link['#suffix'] = "</span>";
$notice_link['#attributes'] = array(
'class' => array('notice-link'),
"tid" => $term->id()
);
$entree['notice_link'] = Link::fromTextAndUrl('notice', $notice_url);
$description = $term->get('description')->value;
// dpm($description);
// TODO: output block through twig and preprocess function
// TODO: load corpus map libraries only with this block
$entree = array(
'#wrapper_attributes' => array(
'class' => array('term-'.$term->id(), 'entree'),
'tid' => $term->id()
),
'term_link' => $term_link,
'entree_content' => array(
'#type' => 'container',
'#attributes' => array(
'class' => 'entree-content'
),
'index_link' => $index_link,
'notice_link' => $notice_link,
'description' => array(
'#type' => 'container',
'#markup' => $description,
'#attributes' => array('class'=>'term-description')
)
)
);
// if (isset($entrees) && count($entrees) == count($terms)-3) {
// $entree['#wrapper_attributes']['class'][] = 'opened';
// }
// dpm($entree);
$entree['description'] = $term->get('description')->value;
$entrees[] = $entree;
}
return array (
'#theme' => 'item_list',
'#items' => $entrees,
'#theme' => 'blockentrees',
'#entrees_items' => $entrees,
'#attached'=>array(
'library' => array('edlp_corpus/corpus'),
'drupalSettings' => array(
@@ -0,0 +1,2 @@
{{ entrees }}