materiotheme.theme 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. 'href' => '/safari-pinned-tab.svg',
  120. 'color' => "#69ccce",
  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. // <meta http-equiv="pragma" content="no-cache" />
  143. $pragma = array(
  144. '#tag' => 'meta',
  145. '#attributes' => array(
  146. 'http-equiv' => 'pragma',
  147. 'content' => "no-cache"
  148. ),
  149. );
  150. $vars['page']['#attached']['html_head'][] = [$pragma, '$pragma'];
  151. // <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate" />
  152. $cachecontrol = array(
  153. '#tag' => 'meta',
  154. '#attributes' => array(
  155. 'http-equiv' => 'cache-control',
  156. 'content' => "no-cache, no-store, must-revalidate"
  157. ),
  158. );
  159. $vars['page']['#attached']['html_head'][] = [$cachecontrol, '$cachecontrol'];
  160. }
  161. function materiotheme_preprocess_page(&$vars){
  162. // dsm($vars, 'vars');
  163. }
  164. /**
  165. * Prepares variables for email template.
  166. *
  167. * Default template: email.html.twig.
  168. *
  169. * @param array $variables
  170. * An associative array containing:
  171. * - email: An associative array containing the Email object.
  172. */
  173. function materiotheme_preprocess_email(&$variables) {
  174. $email = $variables['email'];
  175. $variables['body'] = $email->getBody();
  176. $variables = array_merge($variables, $email->getVariables());
  177. }
  178. // function materiotheme_preprocess_node(&$vars){
  179. // $node = $vars['elements']['#node'];
  180. // $options = ['absolute' => TRUE];
  181. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  182. // $system_path = $url->getInternalPath();
  183. // $vars['link_attributes'] = new Attribute(array(
  184. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path
  185. // ));
  186. // }
  187. // function materiotheme_preprocess_node_materiau_teaser(&$vars){
  188. // $vars['attributes']['class'] = 'card';
  189. // kint($vars['attributes']);
  190. // }
  191. /**
  192. * Implements THEME_preprocess_page_title()
  193. */
  194. function materiotheme_preprocess_page_title(&$variables) {
  195. $current_url = \Drupal\Core\Url::fromRoute('<current>');
  196. $url = $current_url->getInternalPath();
  197. if(preg_match('/^user\/\d+\/*/', $url)) {
  198. $userCurrent = \Drupal::currentUser();
  199. $user = \Drupal\user\Entity\User::load($userCurrent->id());
  200. $email = $user->getEmail();
  201. $variables['title'] = $email;
  202. }
  203. }
  204. /**
  205. * Implements hook_form_alter
  206. */
  207. function materiotheme_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  208. // commerce_order_item_variation_cart_form_form_commerce_product_variation_3
  209. preg_match('/commerce_order_item_variation_cart_form_form_commerce_product_variation_(\d+)/', $form_id, $matches);
  210. if ($matches && isset($matches[1]) && $variation_id = $matches[1]) {
  211. // if (isset($form['#entity_type']) && $form['#entity_type'] == "commerce_order_item") {
  212. // $product = $form_state->storage['product'];
  213. // $storage = &$form_state->getStorage();
  214. // $product = $storage['product'];
  215. // $variations = $product->fields['variations'];
  216. $form['actions']['submit']['#attributes'] += array(
  217. "@click.prevent.stop" => "checkaddtocart(\$event, $variation_id)"
  218. );
  219. }
  220. // change "add to cart" button text
  221. switch ($form_id) {
  222. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_6':
  223. $form['actions']['submit']['#value'] = t('Yeees!');
  224. break;
  225. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_7':
  226. $form['actions']['submit']['#value'] = t('Yay!');
  227. break;
  228. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_8':
  229. $form['actions']['submit']['#value'] = t('Great!');
  230. break;
  231. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_9':
  232. $form['actions']['submit']['#value'] = t('OKAY!');
  233. break;
  234. }
  235. }
  236. /**
  237. * Implements hook_form_alter
  238. */
  239. function materiotheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  240. // dpm($form_id, 'form_id');
  241. // dpm($form, 'form');
  242. $form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
  243. unset($form['name']['#title']);
  244. $form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
  245. unset($form['pass']['#title']);
  246. }
  247. // "simplenews_subscriptions_block_footersimplenewssubscriptionblock"
  248. function materiotheme_form_simplenews_subscriptions_block_footersimplenewssubscriptionblock_alter(&$form, FormStateInterface $form_state, $form_id) {
  249. $form['#cache']['max-age'] = 0;
  250. // unset($form['mail']['widget'][0]['#title']);
  251. // $form['mail']['widget']['#attributes']['placeholder'][] = "Bon alors !";
  252. // $form['mail']['widget']['#attributes']['placeholder'] = (string) $form['mail']['widget']['#title'];
  253. $form['mail']['widget'][0]['value']['#placeholder'] = (string) $form['mail']['widget'][0]['value']['#title'];
  254. $form['mail']['widget'][0]['value']['#size'] = 35;
  255. unset($form['mail']['widget'][0]['value']['#title']);
  256. // unset($form['mail']['widget']['#description']);
  257. // $form['subscriptions']['widget']['#required']
  258. // unset($form['subscriptions']['widget']['#title']);
  259. // unset($form['subscriptions']['widget']['#description']);
  260. $t="t";
  261. }
  262. function materiotheme_preprocess_block(&$variables) {
  263. if ($variables['plugin_id'] == 'simplenews_subscription_block') {
  264. $variables['#cache']['max-age'] = 0;
  265. }
  266. }
  267. /**
  268. * Implements hook_theme_suggestions_HOOK_alter().
  269. */
  270. /**
  271. * Prepares variables for image formatter templates.
  272. *
  273. * Default template: image-formatter.html.twig.
  274. *
  275. * @param array $variables
  276. * An associative array containing:
  277. * - item: An ImageItem object.
  278. * - item_attributes: An optional associative array of html attributes to be
  279. * placed in the img tag.
  280. * - image_style: An optional image style.
  281. * - url: An optional \Drupal\Core\Url object.
  282. */
  283. // function materiotheme_preprocess_image_formatter(&$vars){
  284. // if (isset($vars['url'])) {
  285. // $system_path = $vars['url']->getInternalPath();
  286. // $vars['link_attributes'] = new Attribute(array(
  287. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  288. // 'class' => array('ajax-link')
  289. // ));
  290. // // dpm($vars);
  291. // }
  292. // }
  293. // function materiotheme_preprocess_links__language_block(&$vars){
  294. // // dpm($vars);
  295. // // foreach ($vars['links'] as $lang_code => $link) {
  296. // // $vars['links'][$lang_code]['text'] = $lang_code;
  297. // // $vars['links'][$lang_code]['link']['#title'] = $lang_code;
  298. // // }
  299. // }
  300. function materiotheme_theme_suggestions_taxonomy_term_alter(&$suggestions, &$vars){
  301. // ksm($suggestions);
  302. // ksm($vars);
  303. $original = $vars['theme_hook_original'];
  304. $bundle = $vars['elements']['#taxonomy_term']->bundle();
  305. $viewmode = $vars['elements']["#view_mode"];
  306. $suggestions[] = $original.'__'.$bundle.'__'.$viewmode;
  307. // dsm($suggestions);
  308. }
  309. function materiotheme_theme_suggestions_field_alter(&$suggestions, &$vars){
  310. if($vars['element']["#entity_type"] === "commerce_product_variation"
  311. && $vars['element']["#bundle"] === "materio_product_variation_type"
  312. && $vars['element']["#field_name"] === "title"){
  313. $test = 'test';
  314. $original = $vars['theme_hook_original'];
  315. $entity_type = $vars['element']["#entity_type"];
  316. $bundle = $vars['element']["#bundle"];
  317. $field_name = $vars['element']["#field_name"];
  318. $viewmode = $vars['element']['#view_mode'];
  319. $suggestions[] = $original.'__'.$entity_type.'__'.$bundle.'__'.$field_name.'__'.$viewmode;
  320. }
  321. }
  322. /**
  323. * Implements hook_theme_suggestions_HOOK_alter().
  324. */
  325. function materiotheme_theme_suggestions_image_alter(array &$suggestions, array $variables){
  326. // $image = $variables['attributes']['class'][0];
  327. $suggestions[] = 'image__'.$variables['style_name'];
  328. }
  329. /**
  330. * Prepares variables for product templates.
  331. *
  332. * Default template: commerce-product.html.twig.
  333. *
  334. * @param array $variables
  335. * An associative array containing:
  336. * - elements: An associative array containing rendered fields.
  337. * - attributes: HTML attributes for the containing element.
  338. */
  339. function materiotheme_preprocess_commerce_product(array &$variables) {
  340. $test="test";
  341. // remove the variation as we already display it via views
  342. if($variables['elements']['#view_mode'] === 'order_summary'
  343. || $variables['elements']['#view_mode'] === 'home_summary'){
  344. unset($variables['product']['variation_title']);
  345. unset($variables['product']['variation_field_description']);
  346. unset($variables['product']['variation_price']);
  347. unset($variables['product']['variation_field_multiple']);
  348. unset($variables['product']['variation_commerce_variation_cart_form']);
  349. }
  350. $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  351. $variables['language'] = $language;
  352. }
  353. /**
  354. * Implements hook_theme_suggestions_commerce_product_variation().
  355. */
  356. // function materiotheme_theme_suggestions_commerce_product_variation_later(&$suggestions, &$vars) {
  357. // $test = 'test';
  358. // }
  359. function materiotheme_preprocess_printable(array &$variables) {
  360. $site_config = \Drupal::config('system.site');
  361. $variables['site_name'] = $site_config->get('name');
  362. $variables['slogan'] = $site_config->get('slogan');
  363. }
  364. // TODO: instead of lazy load home images, make a html light home (without images),
  365. // replaced then by rich home vuejs
  366. // function materiotheme_preprocess_image(array &$variables) {
  367. // if ($variables['style_name'] === 'card_small_home') {
  368. // $variables['attributes']['data-src'] = $variables['attributes']['src'];
  369. // $variables['attributes']['src'] = '/themes/custom/materiotheme/assets/img/blank.gif';
  370. // }
  371. // }
  372. function materiotheme_preprocess_field__node__field_a_database__frontpage(array &$variables) {
  373. if ($variables['logged_in']) {
  374. $variables['label_link'] = array(
  375. "href" => '/' . $variables['element']['#language'] . '/base'
  376. );
  377. }
  378. }
  379. function materiotheme_preprocess_field__node__field_blabla__frontpage(array &$variables) {
  380. $variables['label_link'] = array(
  381. "href" => '/' . $variables['element']['#language'] . '/blabla'
  382. );
  383. }
  384. function materiotheme_preprocess_field__node__field_showrooms__frontpage(array &$variables) {
  385. $variables['label_link'] = array(
  386. "href" => '/' . $variables['element']['#language'] . '/showrooms'
  387. );
  388. }
  389. function materiotheme_preprocess_file_link(array &$variables) {
  390. $url = $variables['link']['#url'];
  391. $attributes = $url->getOption('attributes');
  392. $attributes['target'] = '_blank';
  393. $url->setOption('attributes', $attributes);
  394. $t="t";
  395. }