<?php


use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Render\Markup;


/**
 * @file
 * Functions to support theming in the quartiers_de_demain theme.
 */

/**
 * Implements hook_preprocess_HOOK() for html.html.twig.
 */
function quartiers_de_demain_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 page.html.twig.
 */

function quartiers_de_demain_preprocess_page(&$variables) {

}


/**
 * Implements hook_preprocess_HOOK() for node.html.twig.
 */
function quartiers_de_demain_preprocess_node(&$variables) {
    $node = &$variables['node'];
    $variables['attributes']['class'][] = 'node-type-' . $node->gettype();
}



/* implements template_preprocess_region() */

function quartiers_de_demain_preprocess_region(&$variables) {

  if (isset($variables['region'])) {
    $variables['attributes']['class'][] = $variables['region'].'_container'; 
  }

}


/* implements template_preprocess_field() */

function quartiers_de_demain_preprocess_field(&$variables) {
  $node = \Drupal::routeMatch()->getParameter('node');

  if (isset($variables['field_name'])) {
    $variables['attributes']['class'][] = 'field_'.$variables['field_name']; 
  }

}

/**
 * Prepares variables for block templates.
 *
 * Default template: block.html.twig.
 *
 * Prepares the values passed to the theme_block function to be passed
 * into a pluggable template engine. Uses block properties to generate a
 * series of template file suggestions. If none are found, the default
 * block.html.twig is used.
 *
 * Most themes use their own copy of block.html.twig. The default is located
 * inside "core/modules/block/templates/block.html.twig". Look in there for the
 * full list of available variables.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the properties of the element.
 *     Properties used: #block, #configuration, #children, #plugin_id.
 */

//https://www.hashbangcode.com/article/drupal-9-programmatically-creating-and-using-urls-and-links

function quartiers_de_demain_preprocess_block(&$variables) {

}

// function quartiers_de_demain_preprocess_views_view_field(&$variables){
//   /** @var Drupal\views\Entity\View */
//   $view = $variables['view'];
//   $viewid = $view->id();
//   if ($viewid === "statics") {
//     /** @var Drupal\views\ResultRow */
//     $row = $variables['row'];
//     /** @var Drupal\views\Plugin\views\field\EntityField */
//     $field = $variables['field'];

//   }
// }

function quartiers_de_demain_preprocess_links__language_block(&$variables){
  foreach ($variables['links'] as $langcode => $link) {
    $variables['links'][$langcode]['text'] = $langcode;
    $variables['links'][$langcode]['link']['#title'] = $langcode;
  }
}


function quartiers_de_demain_preprocess_block__block_region_second(&$variables) {
  // Vérifie si les champs existent avant de les regrouper.
  if (isset($variables['content']['field_field_numero']) && isset($variables['content']['field_title']) && isset($variables['content']['field_field_sous_titre'])) {

    // Crée une nouvelle div parent pour les 3 premières div
    $grouped_content = [
      '#type' => 'container',
      '#attributes' => ['class' => ['grouped-content']],
      'children' => [
        $variables['content']['field_field_numero'],
        $variables['content']['field_title'],
        $variables['content']['field_field_sous_titre'],
      ],
    ];

    // Ajoute la nouvelle div parent au début du contenu
    $variables['content']['grouped_content'] = $grouped_content;

    // Supprime les éléments individuels de la structure principale
    unset($variables['content']['field_field_numero']);
    unset($variables['content']['field_title']);
    unset($variables['content']['field_field_sous_titre']);
  }
}



