Files
edlp_d8/web/themes/custom/edlptheme/edlptheme.theme
T

331 lines
9.6 KiB
PHP

<?php
/**
* @file
* Functions to support theming in the edlp theme.
*/
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Render\Element;
/**
* Implements hook_page_attachments().
* @param array $attachments
*/
// this does not work with themes
// function edlptheme_page_attachments(array &$attachments) {
// dpm('edlptheme_page_attachments', $attachments);
// }
/**
* Prepares variables for HTML document templates.
*
* Default template: html.html.twig.
*
* @param array $variables
* An associative array containing:
* - page: A render element representing the page.
*/
function edlptheme_preprocess_html(&$vars) {
// $head_title = $vars['head_title'];
// dpm($vars);
$site_config = \Drupal::config('system.site');
// dpm($site_config->get('slogan'));
// array_push($head_title, [
// 'name' => $site_config->get('name'),
// ]);
// $vars['head_title'] = $head_title;
$description = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'description',
'content' => $site_config->get('slogan'),
],
];
$vars['page']['#attached']['html_head'][] = [$description, 'description'];
$gv = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'google-site-verification',
'content' => "Y6PSbMfj67bXtMRAT-mFTAxrIeZPzC5jWSpH3M7yhkk",
],
];
$vars['page']['#attached']['html_head'][] = [$gv, "google-site-verification"];
}
function edlptheme_preprocess_page(&$vars){
// dpm($vars, 'vars');
}
function edlptheme_preprocess_node(&$vars){
$node = $vars['elements']['#node'];
$options = ['absolute' => TRUE];
$url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
$system_path = $url->getInternalPath();
$vars['link_attributes'] = new Attribute(array(
'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path
));
if($node->bundle() == 'enregistrement' && in_array($vars['view_mode'], ['article','transcript'])){
$vars['page'] = true;
$vars['sur_title'] = $vars['view_mode'];
}
}
/**
* Implements hook_form_alter
*/
function edlptheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// dpm($form_id, 'form_id');
// dpm($form, 'form');
$form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
unset($form['name']['#title']);
$form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
unset($form['pass']['#title']);
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function edlptheme_theme_suggestions_taxonomy_term_alter(&$suggestions, $variables) {
$elements = $variables['elements'];
if (isset($elements['#taxonomy_term']) && isset($elements['#theme']) && isset($elements['#view_mode'])) {
$term = $elements['#taxonomy_term'];
if (is_object($term)) {
$suggestions[] = $elements['#theme'] . '__' . $term->getVocabularyId() . '__' . $elements['#view_mode'];
$suggestions[] = $elements['#theme'] . '__' . $term->id() . '__' . $elements['#view_mode'];
}
}
}
function edlptheme_preprocess_edlp_home(&$vars){
// render the promoted_statics and promoted_prods
// $promoteds = ['statics', 'prods'];
// dpm($vars);
// foreach ($promoteds as $index => $p) {
foreach($vars['nodes_statics'] as &$node){
switch($node['vm']){
case "image_2_columns":
$cols = 4;
break;
case "image_1_columns":
$cols = 2;
break;
case "text_1_column":
$cols = 2;
break;
default:
$cols = 3;
};
$node['cols'] = $cols;
}
// }
foreach($vars['nodes_prods'] as &$node){
$node['cols'] = 4;
}
}
function edlptheme_preprocess_taxonomy_term__entrees__mobile_home(&$vars){
// dpm($vars, 'vars');
}
function edlptheme_preprocess_edlp_productions(&$vars){
foreach($vars['nodes'] as &$node){
switch($node['vm']){
case "image_2_columns":
$cols = 4;
break;
case "image_1_columns":
$cols = 2;
break;
case "text_1_column":
$cols = 2;
break;
};
$node['cols'] = $cols;
}
// dpm($vars);
}
function edlptheme_preprocess_node__enregistrement__index(&$vars){
edlptheme_prepare_audio_link($vars);
}
// forhome mobile index
function edlptheme_preprocess_node__enregistrement__index_home(&$vars){
edlptheme_prepare_audio_link($vars);
}
function edlptheme_preprocess_node__enregistrement__search_index(&$vars){
edlptheme_prepare_audio_link($vars);
}
function edlptheme_preprocess_node__enregistrement__lastdocs(&$vars){
edlptheme_prepare_audio_link($vars);
}
function edlptheme_preprocess_node__enregistrement__docsindex(&$vars){
edlptheme_prepare_audio_link($vars);
}
function edlptheme_preprocess_node__enregistrement__compo(&$vars){
edlptheme_prepare_audio_link($vars);
// dpm($vars);
$title = $vars['label'][0]['#context']['value'];
$vars['label'][0]['#context']['value'] = Unicode::truncate($title, 25, true, true);
// dpm($vars['link_attributes']);
}
function edlptheme_prepare_audio_link(&$vars){
$node = $vars['elements']['#node'];
$options = ['absolute' => TRUE];
$url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
$system_path = $url->getInternalPath();
// get the audio file url
$field_son_values = $node->get('field_son')->getValue();
$son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
$son_file = \Drupal\file\Entity\File::load($son_fid);
$son_url = null;
if($son_file){
$son_uri = $son_file->getFileUri();
$son_url = \Drupal::service('file_url_generator')->generateAbsoluteString($son_uri);
}
$vars['link_attributes'] = new Attribute(array(
'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
'audio_url' => $son_url,
'nid' => $node->id(),
'class' => array('audio-link', 'ajax-link')
));
}
function edlptheme_preprocess_node__enregistrement__player_cartel(&$vars){
// dpm($vars);
$node = $vars['node'];
$vars['col_left'] = false;
$vars['col_right'] = false;
// if transcript not empty
$transcript = $node->get('field_transcript_vo');
if(!$transcript->isEmpty()){
$vars['col_left'] = true;
$url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
$url->setOptions(array(
'attributes' => array(
'class' => ['link-transcript', 'ajax-link'],
'viewmode'=>'transcript',
'data-drupal-link-system-path' => $url->getInternalPath()
)
));
$vars['link_transcript'] = Link::fromTextAndUrl(t("Read the text"), $url);
}
// if article not empty
$article = $node->get('body');
if(!$article->isEmpty()){
$vars['col_left'] = true;
$url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
$url->setOptions(array(
'attributes' => array(
'class' => ['link-article', 'ajax-link'],
'viewmode'=>'article',
'data-drupal-link-system-path' => $url->getInternalPath()
)
));
$vars['link_article'] = Link::fromTextAndUrl(t("Read the article"), $url);
}
// relations (defined in edlp_corpus.module)
if(isset($vars['content']['relations'])){
// dpm($relations);
$vars['col_right'] = true;
}
if($vars['col_left'] || $vars['col_right']){
$vars['second_cartel'] = true;
}
// get the audio file url
$field_son_values = $node->get('field_son')->getValue();
$son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
$son_file = \Drupal\file\Entity\File::load($son_fid);
if($son_file){
$son_uri = $son_file->getFileUri();
$son_url = \Drupal::service('file_url_generator')->generateAbsoluteString($son_uri);
$base = log($son_file->getSize(), 1024);
$suffixes = array('', 'K', 'M', 'G', 'T');
$file_size = round(pow(1024, $base - floor($base)), 0) .' '. $suffixes[floor($base)];
preg_match('/^.*(\.\S+)$/', $son_uri, $matches);
$extension = $matches[1];
$node_title = $node->getTitle();
$new_file_name = str_replace(' ', '_', $node_title) . $extension;
$config = \Drupal::config('system.site');
$site_title = str_replace(' ', '_', $config->get('name'));
$url = Url::fromUri($son_url);
$url->setOptions(array(
'attributes' => array(
'class' => ['link-audio-download'],
'download' => $site_title .'__'. $new_file_name,
'title' => $new_file_name . ' ('.$file_size.')'
)
));
$vars['link_audio_download'] = Link::fromTextAndUrl(t("Download"), $url);
}
}
/**
* Prepares variables for image formatter templates.
*
* Default template: image-formatter.html.twig.
*
* @param array $variables
* An associative array containing:
* - item: An ImageItem object.
* - item_attributes: An optional associative array of html attributes to be
* placed in the img tag.
* - image_style: An optional image style.
* - url: An optional \Drupal\Core\Url object.
*/
function edlptheme_preprocess_image_formatter(&$vars){
if(isset($vars['url'])){
$system_path = $vars['url']->getInternalPath();
$vars['link_attributes'] = new Attribute(array(
'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
'class' => array('ajax-link')
));
// dpm($vars);
}
}
function edlptheme_preprocess_links__language_block(&$vars){
// dpm($vars);
foreach ($vars['links'] as $lang_code => $link) {
$vars['links'][$lang_code]['text'] = $lang_code;
$vars['links'][$lang_code]['link']['#title'] = $lang_code;
}
}
function edlptheme_preprocess_file_link(&$vars){
// dpm($vars);
}