2019-01-13 06:47:18 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2019-01-22 18:05:38 +00:00
|
|
|
|
use Drupal\block\Entity\Block;
|
|
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
|
|
|
|
use Drupal\Core\Url;
|
2019-01-13 06:47:18 +00:00
|
|
|
|
|
2022-12-02 03:25:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* Implements hook_preprocess_node() for NODE document templates.
|
|
|
|
|
*/
|
|
|
|
|
function agarica_preprocess_node(&$variables) {
|
|
|
|
|
// Allowed view modes
|
|
|
|
|
$view_mode = $variables['view_mode']; // Retrieve view mode
|
|
|
|
|
$allowed_view_modes = ['full']; // Array of allowed view modes (for performance so as to not execute on unneeded nodes)
|
|
|
|
|
|
|
|
|
|
// If view mode is in allowed view modes list, pass to agarica_add_regions_to_node()
|
|
|
|
|
if(in_array($view_mode, $allowed_view_modes)) {
|
|
|
|
|
// Allowed regions (for performance so as to not execute for unneeded region)
|
|
|
|
|
$allowed_regions = ['secondary_menu'];
|
|
|
|
|
agarica_add_regions_to_node($allowed_regions, $variables);
|
|
|
|
|
}
|
2023-10-20 21:40:45 +00:00
|
|
|
|
$field_body_paragraph = $variables['elements']['field_body_paragraph'] ?? NULL;
|
|
|
|
|
if (isset($field_body_paragraph)) {
|
|
|
|
|
$max_key = max(array_map('intval', array_keys($field_body_paragraph)));
|
|
|
|
|
for ($i = 0; $i <= $max_key; $i++) {
|
|
|
|
|
if ($field_body_paragraph[$i]['#paragraph']->getType() === 'title') {
|
|
|
|
|
|
|
|
|
|
$renderer = \Drupal::service('renderer');
|
|
|
|
|
|
|
|
|
|
$authors_html = $renderer->render($variables['content']['field_authors']);
|
|
|
|
|
$date_html = $variables['date'];
|
|
|
|
|
|
|
|
|
|
$field_body_paragraph[$i]['#paragraph']->authors_html = $authors_html;
|
|
|
|
|
$field_body_paragraph[$i]['#paragraph']->date_html = $date_html;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-02 03:25:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* THEME_add_regions_to_node
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function agarica_add_regions_to_node($allowed_regions, &$variables) {
|
|
|
|
|
// Retrieve active theme
|
|
|
|
|
$theme = \Drupal::theme()->getActiveTheme()->getName();
|
|
|
|
|
|
|
|
|
|
// Retrieve theme regions
|
|
|
|
|
$available_regions = system_region_list($theme, 'REGIONS_ALL');
|
|
|
|
|
|
|
|
|
|
// Validate allowed regions with available regions
|
|
|
|
|
$regions = array_intersect(array_keys($available_regions), $allowed_regions);
|
|
|
|
|
|
|
|
|
|
// For each region
|
|
|
|
|
foreach ($regions as $key => $region) {
|
|
|
|
|
|
|
|
|
|
// Load region blocks
|
|
|
|
|
$blocks = Drupal::entityTypeManager()
|
|
|
|
|
->getStorage('block')
|
|
|
|
|
->loadByProperties(['theme' => $theme, 'region' => $region]);
|
|
|
|
|
|
|
|
|
|
// Sort ‘em
|
|
|
|
|
uasort($blocks, 'Drupal\block\Entity\Block::sort');
|
|
|
|
|
|
|
|
|
|
// Capture viewable blocks and their settings to $build
|
|
|
|
|
$build = [];
|
|
|
|
|
foreach ($blocks as $key => $block) {
|
|
|
|
|
if ($block->access('view')) {
|
|
|
|
|
$view_builder = \Drupal::entityTypeManager()
|
|
|
|
|
->getViewBuilder($block
|
|
|
|
|
->getEntityTypeId());
|
|
|
|
|
$build[$key] = $view_builder
|
|
|
|
|
->view($block, 'block');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add build to region
|
|
|
|
|
$variables[$region] = $build;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-13 06:47:18 +00:00
|
|
|
|
/**
|
2019-01-22 05:48:06 +00:00
|
|
|
|
* Implements hook_preprocess_region__footer().
|
2019-01-13 06:47:18 +00:00
|
|
|
|
*/
|
2019-01-22 05:48:06 +00:00
|
|
|
|
function agarica_preprocess_region__footer(&$variables) {
|
|
|
|
|
$block = Block::load('languageswitcher');
|
|
|
|
|
$block_content = \Drupal::entityTypeManager()
|
|
|
|
|
->getViewBuilder('block')
|
|
|
|
|
->view($block);
|
|
|
|
|
$variables['language_switcher'] = $block_content;
|
2019-01-22 18:05:38 +00:00
|
|
|
|
$variables['language_switcher_link'] = agarica_languague_switch_link();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provides a link to switch to another language.
|
|
|
|
|
*/
|
|
|
|
|
function agarica_languague_switch_link() {
|
|
|
|
|
$derivative_id = 'language_interface';
|
|
|
|
|
$route = \Drupal::getContainer()->get('path.matcher')->isFrontPage() ? '<front>' : '<current>';
|
|
|
|
|
$current_language = \Drupal::languageManager()->getCurrentLanguage($derivative_id)->getId();
|
|
|
|
|
$links = \Drupal::languageManager()->getLanguageSwitchLinks($derivative_id, Url::fromRoute($route))->links;
|
2019-01-22 18:27:57 +00:00
|
|
|
|
$links['en']['title'] = 'Browse this site in English';
|
|
|
|
|
$links['es']['title'] = 'Consulta este sitio en Español';
|
2019-01-22 18:05:38 +00:00
|
|
|
|
$lid = ($current_language == 'en') ? 'es' : 'en';
|
|
|
|
|
// Remove current language from the links
|
|
|
|
|
unset($links[$current_language]);
|
|
|
|
|
|
|
|
|
|
// Check if the current path display an entity.
|
2023-08-30 19:26:39 +00:00
|
|
|
|
$entity = [];
|
2019-01-22 18:05:38 +00:00
|
|
|
|
foreach (\Drupal::routeMatch()->getParameters() as $param) {
|
|
|
|
|
if ($param instanceof EntityInterface) {
|
|
|
|
|
$entity[] = $param;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove links if the content hasnt' been translated yet.
|
|
|
|
|
if ($entity) {
|
|
|
|
|
$has_translation = (method_exists($entity[0], 'getTranslationStatus')) ? $entity[0]->getTranslationStatus($lid) : FALSE;
|
|
|
|
|
$this_translation = ($has_translation && method_exists($entity[0], 'getTranslation')) ? $entity[0]->getTranslation($lid) : FALSE;
|
|
|
|
|
$access_translation = ($this_translation && method_exists($this_translation, 'access') && $this_translation->access('view')) ? TRUE : FALSE;
|
|
|
|
|
if (!$this_translation || !$access_translation || $current_language == $lid) {
|
|
|
|
|
unset($links[$lid]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$links_render_array = [];
|
|
|
|
|
// Transform the links to arrays that can be renderer.
|
|
|
|
|
foreach ($links as $language => $link) {
|
|
|
|
|
$links_render_array[] = [
|
|
|
|
|
'#type' => 'link',
|
|
|
|
|
'#url' => $link['url'],
|
|
|
|
|
'#title' => $link['title'],
|
|
|
|
|
'#options' => [
|
|
|
|
|
'language' => $link['language'],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $links_render_array;
|
2019-01-13 06:47:18 +00:00
|
|
|
|
}
|