123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- /**
- * @file
- * Functions to support theming in the edlp theme.
- */
- use Drupal\Core\Url;
- 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);
- // }
- function edlptheme_preprocess_page(&$vars){
- // dsm($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
- ));
- }
- /**
- * 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']);
- }
- function edlptheme_preprocess_edlp_home(&$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;
- default:
- $cols = 2;
- };
- $node['cols'] = $cols;
- }
- }
- 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){
- $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 = file_create_url($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')
- ));
- // dpm($vars['link_attributes']);
- }
- function edlptheme_preprocess_node__enregistrement__search_index(&$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 = file_create_url($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')
- ));
- // dpm($vars['link_attributes']);
- }
- function edlptheme_preprocess_node__enregistrement__compo(&$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 = file_create_url($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')
- ));
- // 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_preprocess_node__enregistrement__player_cartel(&$vars){
- // dpm($vars);
- // if transcript not empty
- $url = Url::fromRoute('entity.node.canonical', ['node'=>$vars['node']->id()], array(
- 'attributes' => array(
- 'class' => ['link-transcript', 'ajax-link'],
- 'viewmode'=>'transcript'
- )
- ));
- $vars['link_transcript'] = array(
- '#title' => t("Lire le text."),
- '#type' => 'link',
- '#url' => $url,
- '#options'=>array(
- 'attributes' => array(
- 'data-drupal-link-system-path' => $url->getInternalPath()
- )
- )
- );
- // TODO: refacorize the link generator as following :
- // $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term'=>$tid]);
- // $url->setOptions(array(
- // 'attributes' => array(
- // 'class' => ['index-link', 'ajax-link'],
- // 'viewmode'=>'index',
- // 'tid'=>$tid,
- // 'data-drupal-link-system-path' => $url->getInternalPath()
- // )
- // ));
- // $entree['index_link'] = Link::fromTextAndUrl('index', $url);
- // if article not empty
- $url = Url::fromRoute('entity.node.canonical', ['node'=>$vars['node']->id()], array(
- 'attributes' => array(
- 'class' => ['link-article', 'ajax-link'],
- 'viewmode'=>'article'
- )
- ));
- $vars['link_article'] = array(
- '#title' => t("Lire l'article."),
- '#type' => 'link',
- '#url' => $url,
- '#options' => array(
- 'attributes'=>array(
- 'data-drupal-link-system-path' => $url->getInternalPath()
- )
- )
- );
- // if article or transcript
- $vars['col_left'] = true;
- // if
- $vars['col_right'] = true;
- }
- /**
- * 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);
- }
- }
|