materiotheme.theme 14 KB

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