Files
drupal-leshed/web/themes/custom/leshed/leshed.theme
T

238 lines
8.0 KiB
PHP

<?php
/**
* @file
* Functions to support theming in the leshed theme.
*/
function leshed_page_attachments_alter(&$page) {
if (getenv('APP_ENV') === 'development') {
// $attachments['#attached']['html_head'][] = [['src' => 'http://localhost:5173/@vite/client', 'type' => 'module'],'vite-client'];
// $page['#attached']['library'][] = 'http://localhost:5173/@vite/client';
// $attachments['#attached']['html_head'][] = [['src' => 'http://localhost:5173/assets/js/main.js', 'type' => 'module'],'vite-main'];
// $page['#attached']['library'][] = 'http://localhost:5173/assets/js/main.js';
$page['#attached']['library'][] = 'leshed/vitehmr';
$page['#attached']['library'][] = 'leshed/titles_hmr';
}else{
$page['#attached']['library'][] = 'leshed/global';
$page['#attached']['library'][] = 'leshed/titles';
// Must match exactly the url() resolved from assets/dist/main.css, or the
// font is downloaded twice.
$fonts_path = base_path() . \Drupal::service('extension.list.theme')->getPath('leshed') . '/assets/dist/fonts/';
foreach (['Epilogue-VariableFont_wght.ttf', 'Epilogue-Italic-VariableFont_wght.ttf'] as $font) {
$page['#attached']['html_head'][] = [
[
'#tag' => 'link',
'#attributes' => [
'rel' => 'preload',
'href' => $fonts_path . $font,
'as' => 'font',
'type' => 'font/ttf',
'crossorigin' => 'anonymous',
],
],
'leshed_preload_' . $font,
];
}
}
}
/**
* Implements hook_preprocess_HOOK() for html.html.twig.
*/
function leshed_preprocess_html(&$variables) {
}
/**
* Implements hook_preprocess_HOOK() for page.html.twig.
*/
function leshed_preprocess_page(&$variables) {
}
/**
* Rend le titre de base cliquable vers la vue non filtrée sur les pages
* /projets/{term} et /evenements/{term}. Le titre de la vue est "Les Projets"
* ou "Les Événements | Concert" : on sépare sur " | " et on rend la première
* partie en lien vers /projets ou /evenements.
*/
function leshed_preprocess_page_title(&$variables) {
$route_name = \Drupal::routeMatch()->getRouteName();
$base_paths = [
'view.archives.page_1' => '/projets',
'view.archives.page_2' => '/evenements',
];
if (!isset($base_paths[$route_name])) {
return;
}
$title = $variables['title'] ?? '';
if (is_array($title)) {
$title = \Drupal::service('renderer')->render($title);
}
$title = (string) $title;
if (empty($title)) {
return;
}
$parts = explode(' | ', $title, 2);
if (count($parts) < 2) {
return;
}
$base_title = $parts[0];
$term_part = $parts[1];
$base_url = \Drupal\Core\Url::fromUserInput($base_paths[$route_name]);
$variables['title'] = [
'#markup' => '<a href="' . $base_url->toString() . '">' . \Drupal\Component\Utility\Html::escape($base_title) . '</a> | ' . \Drupal\Component\Utility\Html::escape($term_part),
];
}
function leshed_preprocess_region(&$variables) {
if (!isset($variables["attributes"]['class'])) {
$variables["attributes"]['class'] = [];
}
$variables["attributes"]['class'][] = "wrapper";
}
// function leshed_preprocess_block__leshed_contenu(&$variables) {
function leshed_preprocess_block__menu_block__contenu(&$variables) {
foreach ($variables['content']['#items'] as $key => $item) {
parse_menu_item($variables['content']['#items'], $key);
}
}
function parse_menu_item(&$items, $key){
if ($items[$key]['url']->getRouteName() === "entity.node.canonical") {
$nid = $items[$key]['url']->getRouteParameters()['node'];
$node = \Drupal\node\Entity\Node::load($nid);
// create prefix (label) for chapitres
if ($node->getType() === "chapitre") {
$chapitre = $node->get('field_chapitre_num')->getValue()[0]['value'];
$items[$key]['prefix'] = "Chapitre {$chapitre}";
}
// create prefix (label) for parties
if ($node->getType() === "partie") {
$partie = $node->get('field_partie')->getValue()[0]['value'];
$items[$key]['prefix'] = "Partie {$partie}";
}
// add active attribute
if ($items[$key]['in_active_trail']){
$items[$key]['attributes']->offsetSet('class', 'in-active-trail');
}
}
if (count($items[$key]['below']) > 0) {
foreach ($items[$key]['below'] as $key_b => $item_b) {
parse_menu_item($items[$key]['below'], $key_b);
}
}
}
/**
* Implements hook_preprocess_HOOK() for node.html.twig.
*/
function leshed_preprocess_node(&$variables) {
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['node'];
$node_type = $node->getType();
if (!isset($variables["attributes"]['class'])) {
$variables["attributes"]['class'] = [];
}
$variables["attributes"]['class'][] = "node-type-{$node_type}";
$variables["attributes"]['class'][] = "view-mode-{$variables['view_mode']}";
if ($node->hasField('field_images')) {
/** @var \Drupal\file\Plugin\\Field\FieldType\FileFieldItemList $field_images */
$field_images = $node->get('field_images');
if ($field_images && !$field_images->isEmpty()) {
$variables["attributes"]['class'][] = "has-image";
}
}
}
function leshed_preprocess_container(&$variables) {
}
/**
* Implements hook_preprocess_HOOK() for node.html.twig.
*/
function leshed_preprocess_field(&$variables) {
if (!isset($variables["title_attributes"]['class'])) {
$variables["title_attributes"]['class'] = [];
}
$variables['title_attributes']['class'][] = 'field-label';
if (!isset($variables["attributes"]['class'])) {
$variables["attributes"]['class'] = [];
}
$variables['attributes']['class'][] = $variables['field_name'];
if ($variables['field_name'] === 'field_dates') {
$field_items = $variables['element']['#items'];
foreach ($variables['items'] as $delta => &$item) {
$field_item = $field_items[$delta];
foreach (['start_date', 'end_date'] as $key) {
/** @var \Drupal\Core\Datetime\DrupalDateTime|null $date */
$date = $field_item->{$key};
if ($date instanceof \Drupal\Core\Datetime\DrupalDateTime) {
$item[$key] = [
'year' => $date->format('y'),
'month' => $date->format('m'),
'month_name' => $date->format('F'),
'day' => $date->format('d'),
'iso' => $date->format('Y-m-d\TH:i:s') . 'Z',
];
}
}
}
}
// Réécrit les liens des termes type_de_projet vers la vue projets filtrée
// par filtre contextuel. Drupal résout automatiquement l'alias
// (/projets/1 -> /projets/expo) depuis la table path_alias.
if ($variables['field_name'] === 'field_type_de_projet') {
foreach ($variables['items'] as &$item) {
$content = &$item['content'];
if (isset($content['#type']) && $content['#type'] === 'link' && isset($content['#url'])) {
/** @var \Drupal\taxonomy\TermInterface $term */
$term = $content['#entity'] ?? NULL;
if ($term && $term->bundle() === 'type_de_projet') {
$content['#url'] = \Drupal\Core\Url::fromUserInput('/projets/' . $term->id());
}
}
}
}
// Réécrit les liens des termes type_d_evenement vers la vue evenements
// filtrée par filtre contextuel. Drupal résout automatiquement l'alias
// (/evenements/14 -> /evenements/concert) depuis la table path_alias.
if ($variables['field_name'] === 'field_type_d_evenement') {
foreach ($variables['items'] as &$item) {
$content = &$item['content'];
if (isset($content['#type']) && $content['#type'] === 'link' && isset($content['#url'])) {
/** @var \Drupal\taxonomy\TermInterface $term */
$term = $content['#entity'] ?? NULL;
if ($term && $term->bundle() === 'type_d_evenement') {
$content['#url'] = \Drupal\Core\Url::fromUserInput('/evenements/' . $term->id());
}
}
}
}
}
function leshed_preprocess_links__language_block(&$variables){
foreach ($variables['links'] as $langcode => $link) {
$variables['links'][$langcode]['text'] = $langcode;
$variables['links'][$langcode]['link']['#title'] = $langcode;
}
}
function leshed_preprocess_time(&$variables){
}