materiotheme.theme 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * @file
  4. * Functions to support theming in the materio theme.
  5. */
  6. use Drupal\Core\Url;
  7. use Drupal\Core\Link;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Template\Attribute;
  10. use Drupal\Component\Utility\Unicode;
  11. use Drupal\Core\Render\Element;
  12. /**
  13. * Implements hook_page_attachments().
  14. * @param array $attachments
  15. */
  16. // this does not work with themes
  17. // function materiotheme_page_attachments(array &$attachments) {
  18. // dpm('materiotheme_page_attachments', $attachments);
  19. // }
  20. /**
  21. * Prepares variables for HTML document templates.
  22. *
  23. * Default template: html.html.twig.
  24. *
  25. * @param array $variables
  26. * An associative array containing:
  27. * - page: A render element representing the page.
  28. */
  29. function materiotheme_preprocess_html(&$vars) {
  30. // $head_title = $vars['head_title'];
  31. // dpm($vars);
  32. $site_config = \Drupal::config('system.site');
  33. // dpm($site_config->get('slogan'));
  34. // array_push($head_title, [
  35. // 'name' => $site_config->get('name'),
  36. // ]);
  37. // $vars['head_title'] = $head_title;
  38. // $title = "The new title";
  39. // $request = \Drupal::request();
  40. // if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
  41. // $route->setDefault('_title', $title);
  42. // }
  43. global $base_url;
  44. $theme = \Drupal::theme()->getActiveTheme();
  45. $vars['#attached']['drupalSettings']['path']['themePath'] = $base_url .'/'. $theme->getPath();
  46. $description = [
  47. '#tag' => 'meta',
  48. '#attributes' => [
  49. 'name' => 'description',
  50. 'content' => $site_config->get('slogan'),
  51. ],
  52. ];
  53. $vars['page']['#attached']['html_head'][] = [$description, 'description'];
  54. $viewport = array(
  55. '#tag' => 'meta',
  56. '#attributes' => array(
  57. 'name' => 'viewport',
  58. 'content' => 'width=device-width, initial-scale=1, maximum-scale=1',
  59. ),
  60. );
  61. $vars['page']['#attached']['html_head'][] = [$viewport, 'viewport'];
  62. // drupal_add_html_head($viewport, 'viewport');
  63. // https://stackoverflow.com/a/48700852
  64. // <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  65. $content_security_policy = array(
  66. '#tag' => 'meta',
  67. '#attributes' => array(
  68. 'http-equiv' => 'Content-Security-Policy',
  69. 'content' => 'upgrade-insecure-requests',
  70. ),
  71. );
  72. $vars['page']['#attached']['html_head'][] = [$content_security_policy, 'content_security_policy'];
  73. // $gv = [
  74. // '#tag' => 'meta',
  75. // '#attributes' => [
  76. // 'name' => 'google-site-verification',
  77. // 'content' => "Y6PSbMfj67bXtMRAT-mFTAxrIeZPzC5jWSpH3M7yhkk",
  78. // ],
  79. // ];
  80. // $vars['page']['#attached']['html_head'][] = [$gv, "google-site-verification"];
  81. // <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
  82. $ati = array(
  83. '#tag' => 'meta',
  84. '#attributes' => array(
  85. 'rel' => 'apple-touch-icon',
  86. 'sizes' => "180x180",
  87. 'href' => '/apple-touch-icon.png',
  88. ),
  89. );
  90. $vars['page']['#attached']['html_head'][] = [$ati, 'ati'];
  91. // <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
  92. $icon32 = array(
  93. '#tag' => 'meta',
  94. '#attributes' => array(
  95. 'rel' => 'icon',
  96. 'type' => "image/png",
  97. 'sizes' => "32x32",
  98. 'href' => '/favicon-32x32.png',
  99. ),
  100. );
  101. $vars['page']['#attached']['html_head'][] = [$icon32, 'icon32'];
  102. // <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
  103. $icon16 = array(
  104. '#tag' => 'meta',
  105. '#attributes' => array(
  106. 'rel' => 'icon',
  107. 'type' => "image/png",
  108. 'sizes' => "16x16",
  109. 'href' => '/favicon-16x16.png',
  110. ),
  111. );
  112. $vars['page']['#attached']['html_head'][] = [$icon16, 'icon16'];
  113. // <link rel="manifest" href="/site.webmanifest">
  114. // <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
  115. $safaripinnedtab = array(
  116. '#tag' => 'meta',
  117. '#attributes' => array(
  118. 'rel' => 'mask-icon',
  119. 'color' => "#69ccce",
  120. 'href' => '/safari-pinned-tab.svg',
  121. ),
  122. );
  123. $vars['page']['#attached']['html_head'][] = [$safaripinnedtab, '$safaripinnedtab'];
  124. // <meta name="msapplication-TileColor" content="#da532c">
  125. $ms = array(
  126. '#tag' => 'meta',
  127. '#attributes' => array(
  128. 'name' => 'msapplication-TileColor',
  129. 'content' => "#69ccce"
  130. ),
  131. );
  132. $vars['page']['#attached']['html_head'][] = [$ms, '$ms'];
  133. // <meta name="theme-color" content="#ffffff">
  134. $tc = array(
  135. '#tag' => 'meta',
  136. '#attributes' => array(
  137. 'name' => 'theme-color',
  138. 'content' => "#69ccce"
  139. ),
  140. );
  141. $vars['page']['#attached']['html_head'][] = [$tc, '$tc'];
  142. }
  143. function materiotheme_preprocess_page(&$vars){
  144. // dsm($vars, 'vars');
  145. }
  146. // function materiotheme_preprocess_node(&$vars){
  147. // $node = $vars['elements']['#node'];
  148. // $options = ['absolute' => TRUE];
  149. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  150. // $system_path = $url->getInternalPath();
  151. // $vars['link_attributes'] = new Attribute(array(
  152. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path
  153. // ));
  154. // }
  155. // function materiotheme_preprocess_node_materiau_teaser(&$vars){
  156. // $vars['attributes']['class'] = 'card';
  157. // kint($vars['attributes']);
  158. // }
  159. /**
  160. * Implements hook_form_alter
  161. */
  162. function materiotheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  163. // dpm($form_id, 'form_id');
  164. // dpm($form, 'form');
  165. $form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
  166. unset($form['name']['#title']);
  167. $form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
  168. unset($form['pass']['#title']);
  169. }
  170. /**
  171. * Implements hook_theme_suggestions_HOOK_alter().
  172. */
  173. /**
  174. * Prepares variables for image formatter templates.
  175. *
  176. * Default template: image-formatter.html.twig.
  177. *
  178. * @param array $variables
  179. * An associative array containing:
  180. * - item: An ImageItem object.
  181. * - item_attributes: An optional associative array of html attributes to be
  182. * placed in the img tag.
  183. * - image_style: An optional image style.
  184. * - url: An optional \Drupal\Core\Url object.
  185. */
  186. // function materiotheme_preprocess_image_formatter(&$vars){
  187. // if (isset($vars['url'])) {
  188. // $system_path = $vars['url']->getInternalPath();
  189. // $vars['link_attributes'] = new Attribute(array(
  190. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  191. // 'class' => array('ajax-link')
  192. // ));
  193. // // dpm($vars);
  194. // }
  195. // }
  196. // function materiotheme_preprocess_links__language_block(&$vars){
  197. // // dpm($vars);
  198. // // foreach ($vars['links'] as $lang_code => $link) {
  199. // // $vars['links'][$lang_code]['text'] = $lang_code;
  200. // // $vars['links'][$lang_code]['link']['#title'] = $lang_code;
  201. // // }
  202. // }
  203. function materiotheme_theme_suggestions_taxonomy_term_alter(&$suggestions, &$vars){
  204. // ksm($suggestions);
  205. // ksm($vars);
  206. $original = $vars['theme_hook_original'];
  207. $bundle = $vars['elements']['#taxonomy_term']->bundle();
  208. $viewmode = $vars['elements']["#view_mode"];
  209. $suggestions[] = $original.'__'.$bundle.'__'.$viewmode;
  210. // dsm($suggestions);
  211. }
  212. function materiotheme_theme_suggestions_field_alter(&$suggestions, &$vars){
  213. if($vars['element']["#entity_type"] === "commerce_product_variation"
  214. && $vars['element']["#bundle"] === "materio_product_variation_type"
  215. && $vars['element']["#field_name"] === "title"){
  216. $test = 'test';
  217. $original = $vars['theme_hook_original'];
  218. $entity_type = $vars['element']["#entity_type"];
  219. $bundle = $vars['element']["#bundle"];
  220. $field_name = $vars['element']["#field_name"];
  221. $viewmode = $vars['element']['#view_mode'];
  222. $suggestions[] = $original.'__'.$entity_type.'__'.$bundle.'__'.$field_name.'__'.$viewmode;
  223. }
  224. }
  225. /**
  226. * Implements hook_theme_suggestions_HOOK_alter().
  227. */
  228. function materiotheme_theme_suggestions_image_alter(array &$suggestions, array $variables){
  229. // $image = $variables['attributes']['class'][0];
  230. $suggestions[] = 'image__'.$variables['style_name'];
  231. }
  232. /**
  233. * Prepares variables for product templates.
  234. *
  235. * Default template: commerce-product.html.twig.
  236. *
  237. * @param array $variables
  238. * An associative array containing:
  239. * - elements: An associative array containing rendered fields.
  240. * - attributes: HTML attributes for the containing element.
  241. */
  242. function materiotheme_preprocess_commerce_product(array &$variables) {
  243. $test="test";
  244. // remove the variation as we already display it via views
  245. if($variables['elements']['#view_mode'] === 'order_summary'
  246. || $variables['elements']['#view_mode'] === 'home_summary'){
  247. unset($variables['product']['variation_title']);
  248. unset($variables['product']['variation_field_description']);
  249. unset($variables['product']['variation_price']);
  250. unset($variables['product']['variation_field_multiple']);
  251. unset($variables['product']['variation_commerce_variation_cart_form']);
  252. }
  253. $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  254. $variables['language'] = $language;
  255. }
  256. /**
  257. * Implements hook_theme_suggestions_commerce_product_variation().
  258. */
  259. // function materiotheme_theme_suggestions_commerce_product_variation_later(&$suggestions, &$vars) {
  260. // $test = 'test';
  261. // }
  262. function materiotheme_preprocess_printable(array &$variables) {
  263. $site_config = \Drupal::config('system.site');
  264. $variables['site_name'] = $site_config->get('name');
  265. $variables['slogan'] = $site_config->get('slogan');
  266. }
  267. // TODO: instead of lazy load home images, make a html light home (without images),
  268. // replaced then by rich home vuejs
  269. // function materiotheme_preprocess_image(array &$variables) {
  270. // if ($variables['style_name'] === 'card_small_home') {
  271. // $variables['attributes']['data-src'] = $variables['attributes']['src'];
  272. // $variables['attributes']['src'] = '/themes/custom/materiotheme/assets/img/blank.gif';
  273. // }
  274. // }