<?php

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

use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Render\Element;


/**
 * Implements hook_page_attachments().
 * @param array $attachments
 */
 // this does not work with themes
// function materiotheme_page_attachments(array &$attachments) {
//   dpm('materiotheme_page_attachments', $attachments);
// }

/**
 * Prepares variables for HTML document templates.
 *
 * Default template: html.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - page: A render element representing the page.
 */
function materiotheme_preprocess_html(&$vars) {
  // $head_title = $vars['head_title'];
  // dpm($vars);
  $site_config = \Drupal::config('system.site');
  // dpm($site_config->get('slogan'));
  // array_push($head_title, [
  //   'name' => $site_config->get('name'),
  // ]);
  // $vars['head_title'] = $head_title;

  // $title = "The new title";
  // $request = \Drupal::request();
  // if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
  //   $route->setDefault('_title', $title);
  // }

  global $base_url;
  $theme = \Drupal::theme()->getActiveTheme();
  $vars['#attached']['drupalSettings']['path']['themePath'] = $base_url .'/'. $theme->getPath();

  $description = [
    '#tag' => 'meta',
    '#attributes' => [
      'name' => 'description',
      'content' => $site_config->get('slogan'),
    ],
  ];

  $vars['page']['#attached']['html_head'][] = [$description, 'description'];

  // $gv = [
  //   '#tag' => 'meta',
  //   '#attributes' => [
  //     'name' => 'google-site-verification',
  //     'content' => "Y6PSbMfj67bXtMRAT-mFTAxrIeZPzC5jWSpH3M7yhkk",
  //   ],
  // ];
  // $vars['page']['#attached']['html_head'][] = [$gv, "google-site-verification"];
}

function materiotheme_preprocess_page(&$vars){
  // dsm($vars, 'vars');

}

// function materiotheme_preprocess_node(&$vars){
//   $node = $vars['elements']['#node'];
//   $options = ['absolute' => TRUE];
//   $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
//   $system_path = $url->getInternalPath();
//   $vars['link_attributes'] = new Attribute(array(
//     'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path
//   ));
// }

// function materiotheme_preprocess_node_materiau_teaser(&$vars){
  // $vars['attributes']['class'] = 'card';
  // kint($vars['attributes']);
// }

/**
 * Implements hook_form_alter
 */
function materiotheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // dpm($form_id, 'form_id');
  // dpm($form, 'form');
  $form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
  unset($form['name']['#title']);
  $form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
  unset($form['pass']['#title']);
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */

/**
 * Prepares variables for image formatter templates.
 *
 * Default template: image-formatter.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - item: An ImageItem object.
 *   - item_attributes: An optional associative array of html attributes to be
 *     placed in the img tag.
 *   - image_style: An optional image style.
 *   - url: An optional \Drupal\Core\Url object.
 */
// function materiotheme_preprocess_image_formatter(&$vars){
//   if(isset($vars['url'])){
//     $system_path = $vars['url']->getInternalPath();
//     $vars['link_attributes'] = new Attribute(array(
//       'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
//       'class' => array('ajax-link')
//     ));
//     // dpm($vars);
//   }
// }

// function materiotheme_preprocess_links__language_block(&$vars){
//   // dpm($vars);
//   // foreach ($vars['links'] as $lang_code => $link) {
//   //   $vars['links'][$lang_code]['text'] = $lang_code;
//   //   $vars['links'][$lang_code]['link']['#title'] = $lang_code;
//   // }
// }