ldc-ovoteam/public/modules/custom/custmod/custmod.module
2024-02-06 10:25:26 +01:00

199 lines
7.4 KiB
PHP

<?php
/**
* @file
* Contains custmod.module.
*/
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\editor\Entity\Editor;
use Drupal\node\Entity\Node;
/**
* Implements hook_help().
*/
function custmod_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the custom module.
case 'help.page.custom':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('custmod') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_ckeditor_css_alter().
*/
function custmod_ckeditor_css_alter(array &$css, Editor $editor) {
$css[] = '/assets/css/ckeditor.min.css?v='.time(); // TODO: Remove time()
}
/**
* Implements hook_theme().
*/
function custmod_theme() {
return [
'activities_block' => [
'variables' => [
'title' => '',
],
],
'recipes' => [
'variables' => [
'node' => NULL,
'recipes' => [],
'summary' => [],
'first_recipe' => []
]
],
'products' => [
'variables' => [
'activity' => NULL,
'breeding' => NULL,
'range' => NULL,
'products' => [],
'summary' => [],
'ranges_tree' => [],
'breeding_tree' => []
]
],
'articles' => [
'variables' => [
'node' => NULL,
'articles' => [],
'summary' => []
]
],
'contact' => [
'variables' => [
'node' => NULL,
]
],
'search-page' => ['variables' => ['results' => array(), 'query' => '']],
];
}
/**
* Implements hook_system_breadcrumb_alter().
*/
function custmod_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
// Append the current page title to the breadcrumb for non-admin routes.
if ($breadcrumb && !\Drupal::service('router.admin_context')->isAdminRoute()) {
$request = \Drupal::request();
$node = $route_match->getParameter('node');
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
if ($node) {
switch ($node->bundle()) {
case 'product':
$range = $taxonomy_storage->load($node->get('field_rang')->getValue()[0]['target_id']);
$activity_id = $request->cookies->get('ovoteam_activity_id') ?? 4;
$breadcrumb->addLink(Link::createFromRoute($range->name->getValue()[0]['value'], 'custmod.products', ['activite' => $activity_id, 'range' => $node->get('field_rang')->getValue()[0]['target_id']]));
$breadcrumb->addLink(Link::createFromRoute($node->getTitle(), '<nolink>'));
break;
default:
$breadcrumb->addLink(Link::createFromRoute($node->getTitle(), '<nolink>'));
}
} else if ($title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject())) {
$breadcrumb->addLink(Link::createFromRoute($title, '<nolink>'));
}
$breadcrumb->addCacheContexts(['url:path']);
}
}
/**
* Implements hook_metatags_alter
*/
function custmod_metatags_alter(array &$metatags, array $context) {
if (isset($metatags['title']) && empty($metatags['og_title'])) {
$metatags['og_title'] = $metatags['title'];
}
if (isset($metatags['description']) && empty($metatags['og_description'])) {
$metatags['og_description'] = $metatags['description'];
}
}
function custmod_taxonomy_term_presave(EntityInterface $entity){
$vid = $entity->bundle();
if ($vid == 'activite' || $vid == 'ranges') {
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$path_alias_storage = \Drupal::entityTypeManager()->getStorage('path_alias');
$ranges_terms = $taxonomy_storage->loadTree('ranges', 0, NULL, true);
$activity_terms = $taxonomy_storage->loadTree('activite', 0, NULL, true);
$breeding_terms = $taxonomy_storage->loadTree('breeding', 0, NULL, true);
foreach ($activity_terms as $activity_term){
$activity_en = $activity_term->hasTranslation('en') ? $activity_term->getTranslation('en') : $activity_term;
foreach ($ranges_terms as $ranges_term) {
$ranges_en = $ranges_term->hasTranslation('en') ? $ranges_term->getTranslation('en') : $ranges_term;
$path = '/custmod/products/'.$activity_term->id().'/'.$ranges_term->id();
$existing_alias_fr_array = $path_alias_storage->loadByProperties([
'path' => $path,
'langcode' => 'fr'
]);
$existing_alias_fr = $existing_alias_fr_array[array_key_first($existing_alias_fr_array)];
$existing_alias_en_array = $path_alias_storage->loadByProperties([
'path' => $path,
'langcode' => 'en'
]);
$existing_alias_en = $existing_alias_en_array[array_key_first($existing_alias_en_array)];
$alias_fr = '/'.$activity_term->get('field_slug')->getValue()[0]['slug'].'/nos-gammes/'.$ranges_term->get('field_slug')->getValue()[0]['slug'];
$alias_en = '/'.$activity_en->get('field_slug')->getValue()[0]['slug'].'/our-ranges/'.$ranges_en->get('field_slug')->getValue()[0]['slug'];
if (empty($existing_alias_fr) || $existing_alias_fr->get('alias')->getValue()[0]['value'] != $alias_fr){
$path_alias_storage->create([
'path' => $path,
'alias' => $alias_fr,
'langcode' => 'fr'
])->save();
}
if (empty($existing_alias_en) || $existing_alias_en->get('alias')->getValue()[0]['value'] != $alias_en){
$path_alias_storage->create([
'path' => $path,
'alias' => $alias_en,
'langcode' => 'en'
])->save();
}
}
foreach ($breeding_terms as $breeding_term){
$breeding_en = $breeding_term->hasTranslation('en') ? $breeding_term->getTranslation('en') : $breeding_term;
$path = '/custmod/products/breeding/'.$activity_term->id().'/'.$breeding_term->id();
$existing_alias_fr_array = $path_alias_storage->loadByProperties([
'path' => $path,
'langcode' => 'fr'
]);
$existing_alias_fr = $existing_alias_fr_array[array_key_first($existing_alias_fr_array)];
$existing_alias_en_array = $path_alias_storage->loadByProperties([
'path' => $path,
'langcode' => 'en'
]);
$existing_alias_en = $existing_alias_en_array[array_key_first($existing_alias_en_array)];
$alias_fr = '/'.$activity_term->get('field_slug')->getValue()[0]['slug'].'/nos-gammes/'.$breeding_term->get('field_slug')->getValue()[0]['slug'];
$alias_en = '/'.$activity_en->get('field_slug')->getValue()[0]['slug'].'/our-ranges/'.$breeding_en->get('field_slug')->getValue()[0]['slug'];
if (empty($existing_alias_fr) || $existing_alias_fr->get('alias')->getValue()[0]['value'] != $alias_fr){
$path_alias_storage->create([
'path' => $path,
'alias' => $alias_fr,
'langcode' => 'fr'
])->save();
}
if (empty($existing_alias_en) || $existing_alias_en->get('alias')->getValue()[0]['value'] != $alias_en){
$path_alias_storage->create([
'path' => $path,
'alias' => $alias_en,
'langcode' => 'en'
])->save();
}
}
}
}
}
function custmod_entity_build_defaults_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, $view_mode){
$entity->addCacheContexts(['cookies:ovoteam_activity_id']);
}