123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- use Drupal\Core\Url;
- use Drupal\Core\Link;
- use Drupal\Core\Render\Markup;
- use Drupal\Component\Utility\Html;
- use Drupal\user\Entity\User;
- use Drupal\block\Entity\Block;
- use Drupal\Core\Render\Element;
- /**
- * @file
- * Functions to support theming in the reha theme.
- */
- /**
- * Implements hook_preprocess_HOOK() for html.html.twig.
- */
- function reha_preprocess_html(&$variables) {
- $node = \Drupal::routeMatch()->getParameter('node');
- if ($node){
- $variables['attributes']['class'][] = 'node-type-' . $node->bundle();
- $variables['attributes']['class'][] = 'node-id-' . $node->id();
- }
- }
- /**
- * Implements hook_preprocess_HOOK() for node.html.twig.
- */
-
- function reha_preprocess_node(&$variables) {
- $node = &$variables['node'];
- $variables['attributes']['class'][] = 'node-type-' . $node->gettype();
-
- if ($node->getType() == 'site' || $node->getType() == 'ressource' || $node->getType() == 'actualite') {
- $fields_to_exclude = [
- 'field_image', // Remplacez par le nom machine exact de votre champ image
- ];
-
- $filtered_content = [];
- $image_field_content = '';
- $body_field_content = '';
- $files_field_content = '';
- $liens_field_content = '';
- $numero_field_content = '';
- $adresse_field_content = '';
- $lettre_field_content = '';
- $image_caption_field_content = '';
- foreach ($variables['content'] as $field_name => $field_content) {
- if ($field_name == 'body') {
- $body_field_content = $field_content;
- } elseif ($field_name == 'field_fichiers') {
- $files_field_content = $field_content;
- } elseif ($field_name == 'field_liens') {
- $liens_field_content = $field_content;
- } elseif ($field_name == 'field_adresse_site') {
- $numero_field_content = $field_content;
- } elseif ($field_name == 'field_numero_site') {
- $adresse_field_content = $field_content;
- } elseif ($field_name == 'field_lettre_de_site') {
- $adresse_field_content = $field_content;
- } elseif ($field_name == 'image_field_caption') {
- $image_caption_field_content = $field_content;
- } elseif (!in_array($field_name, $fields_to_exclude)) {
- $filtered_content[$field_name] = $field_content;
- } else {
- $image_field_content = $field_content;
- }
- }
-
- $variables['filtered_content'] = $filtered_content;
- $variables['image_field_content'] = $image_field_content;
- $variables['body_field_content'] = $body_field_content;
- $variables['files_field_content'] = $files_field_content;
- $variables['liens_field_content'] = $liens_field_content;
- $variables['adresse_field_content'] = $adresse_field_content;
- $variables['numero_field_content'] = $numero_field_content;
- $variables['field_lettre_de_site'] = $lettre_field_content;
- $variables['image_field_caption'] = $image_caption_field_content;
- }
- }
-
- function reha_preprocess_block(&$variables) {
- // Conserver les IDs existants et générer un ID unique uniquement si aucun ID n'est présent
- if (empty($variables['attributes']['id'])) {
- $block_id = 'block-' . uniqid();
- $variables['attributes']['id'] = $block_id;
- }
- if ($variables['plugin_id'] === "user_login_block") {
- $url = new Url('user.register', [], ['query' => ['destination' => '/node/add/operation']]);
- $link = new Link('proposer une opération', $url);
- $variables['content']['reha'] = array(
- '#theme' => 'item_list',
- '#items' => [
- 'operations' => [
- "add_operation" => $link->toRenderable(),
- "description" => [
- "#markup" => Markup::create("<p>Créer un compte pour charger une opération</p>")
- ]
- ]
- ]
- );
- }
- if ($variables['plugin_id'] === "page_title_block") {
- if (\Drupal::routeMatch()->getRouteName() === 'node.add') {
- $node_type = \Drupal::routeMatch()->getParameter('node_type');
- if ($node_type->id() === 'operation') {
- $variables['content'] = [
- '#type' => 'page_title',
- '#title' => 'Proposer une opération'
- ];
- }
- }
- }
- if ($variables['plugin_id'] === "views_block:current_user_block-block_1") {
- $user = User::load($variables['user']->id());
- $prenomnom = $user->get('field_prenom')->getString() . ' ' . $user->get('field_nom')->getString();
- $variables['content']['#title']['#markup'] = $prenomnom;
- $variables['label']['#markup'] = $prenomnom;
- }
- }
- function reha_preprocess_field(&$variables){
- if($variables['field_name'] === 'field_adresse_site'){
- foreach($variables['items'] as $index => $adr){
- if (isset($variables['items'][$index]['content']['postal_code'])) {
- $postal_code = $variables['items'][$index]['content']['postal_code']['#value'];
- $pattern = '/(\d{2})(\d+)/i';
- $replacement = '($1)';
- $variables['items'][$index]['content']['postal_code']['#value'] = preg_replace($pattern, $replacement, $postal_code);
- }
- }
- }
- if($variables['field_name'] === 'field_image'){
- if($variables['element']['#view_mode'] === 'home_block'){
- array_splice($variables['items'], 1);
- }
- }
- }
|