133 lines
4.5 KiB
PHP
133 lines
4.5 KiB
PHP
<?php
|
|
|
|
# @Author: Bachir Soussi Chiadmi <bach>
|
|
# @Email: bachir@figureslibres.io
|
|
# @Filename: edlp_ajax.module
|
|
# @License: GPL-V3
|
|
|
|
use Drupal\Core\Url;
|
|
|
|
|
|
/**
|
|
* Implements hook_page_attachments().
|
|
* @param array $attachments
|
|
*/
|
|
function edlp_ajax_page_attachments(array &$attachments) {
|
|
// provied info about available urls needed for ajax purpose
|
|
$url = Url::fromRoute('edlp_ajax.entityjson');
|
|
$attachments['#attached']['drupalSettings']['edlp_ajax']['entityjson_path'] = $url->getInternalPath();
|
|
$url = Url::fromRoute('edlp_ajax.blocksjson');
|
|
$attachments['#attached']['drupalSettings']['edlp_ajax']['blocksjson_path'] = $url->getInternalPath();
|
|
|
|
// provied infos about current entity (node or taxonomy term) and audio files
|
|
$current_path = \Drupal::service('path.current')->getPath();
|
|
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
|
$is_front = \Drupal::service('path.matcher')->isFrontPage();
|
|
|
|
$entity_type = null;
|
|
$entity_bundle = null;
|
|
$entity_id = null;
|
|
$audio_url = null;
|
|
foreach (['node', 'taxonomy_term'] as $type) {
|
|
$entity = \Drupal::routeMatch()->getParameter($type);
|
|
if($entity){
|
|
$entity_type = $type;
|
|
$entity_bundle = $entity->bundle();
|
|
$entity_id = $entity->id();
|
|
if($entity_bundle == 'enregistrement'){
|
|
$field_son_values = $entity->get('field_son')->getValue();
|
|
$audio_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : null;
|
|
if($audio_fid){
|
|
$audio_file = \Drupal\file\Entity\File::load($audio_fid);
|
|
$son_uri = $audio_file->getFileUri();
|
|
$audio_url = \Drupal::service('file_url_generator')->generateAbsoluteString($son_uri);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
// do not redirect if not node, term, or custom routing
|
|
// FIXME: check with routes instead of path as path can change !!!
|
|
$redirect = false;
|
|
if(preg_match('/^\/?node\/\d+/', $current_path)
|
|
|| preg_match('/^\/?taxonomy\/term\/\d+/', $current_path)
|
|
|| preg_match('/^\/?productions/', $current_path)
|
|
|| preg_match('/^\/?agenda/', $current_path)
|
|
|| preg_match('/^\/?studio/', $current_path)
|
|
|| preg_match('/^\/?search/', $current_path)
|
|
|| preg_match('/^\/?docsindex/', $current_path)
|
|
|| preg_match('/^\/?lastdocs/', $current_path)
|
|
|| preg_match('/^\/?articles/', $current_path)){
|
|
$redirect = true;
|
|
}
|
|
|
|
$site_name = \Drupal::config('system.site')->get('name');
|
|
|
|
$js_str = "var edlp = {\n
|
|
sys_path:'".$current_path."',\n
|
|
is_front:".($is_front ? 'true':'false').",\n
|
|
redirect:".($redirect ? 'true':'false').",\n
|
|
lang_code:'".$current_language."',\n
|
|
entity_type:'".$entity_type."',\n
|
|
entity_bundle:'".$entity_bundle."',\n
|
|
entity_id:'".$entity_id."',\n
|
|
audio_url:'".$audio_url."',\n
|
|
site_name:'".$site_name."',\n
|
|
};";
|
|
|
|
$attachments['#attached']['html_head'][] = [
|
|
[
|
|
'#type' => 'html_tag',
|
|
'#tag' => 'script',
|
|
'#value' => $js_str,
|
|
'#weight' => -999,
|
|
'#group' => 'edlp'
|
|
],
|
|
// A key, to make it possible to recognize this HTML element when altering.
|
|
'edlp',
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* Implements hook_theme().
|
|
*/
|
|
function edlp_ajax_theme($existing, $type, $theme, $path) {
|
|
// @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
|
|
return array(
|
|
'edlp_ajax' => array(
|
|
'file' => 'includes/edlp_ajax.inc',
|
|
'variables' => array(
|
|
'entity_type' => null,
|
|
'bundle' => null,
|
|
'entity' => null,
|
|
'view_mode' => 'default',
|
|
'aside' => array(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_theme_suggestions_HOOK().
|
|
*/
|
|
function edlp_ajax_theme_suggestions_edlp_ajax(array $vars) {
|
|
// dpm($vars);
|
|
$suggestions = [];
|
|
// $node = $variables['elements']['#node'];
|
|
$sanitized_view_mode = strtr($vars['view_mode'], '.', '_');
|
|
// $entity = $vars['entity'];
|
|
//
|
|
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'];
|
|
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $sanitized_view_mode;
|
|
// $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
|
|
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id();
|
|
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id() . '__' . $sanitized_view_mode;
|
|
|
|
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'];
|
|
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'] . '__' . $sanitized_view_mode;
|
|
|
|
return $suggestions;
|
|
}
|