materiotheme.theme 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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_1':
  209. $form['actions']['submit']['#value'] = t('Yeees!');
  210. break;
  211. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_2':
  212. $form['actions']['submit']['#value'] = t('Yay!');
  213. break;
  214. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_3':
  215. $form['actions']['submit']['#value'] = t('Great!');
  216. break;
  217. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_4':
  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. /**
  234. * Implements hook_theme_suggestions_HOOK_alter().
  235. */
  236. /**
  237. * Prepares variables for image formatter templates.
  238. *
  239. * Default template: image-formatter.html.twig.
  240. *
  241. * @param array $variables
  242. * An associative array containing:
  243. * - item: An ImageItem object.
  244. * - item_attributes: An optional associative array of html attributes to be
  245. * placed in the img tag.
  246. * - image_style: An optional image style.
  247. * - url: An optional \Drupal\Core\Url object.
  248. */
  249. // function materiotheme_preprocess_image_formatter(&$vars){
  250. // if (isset($vars['url'])) {
  251. // $system_path = $vars['url']->getInternalPath();
  252. // $vars['link_attributes'] = new Attribute(array(
  253. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  254. // 'class' => array('ajax-link')
  255. // ));
  256. // // dpm($vars);
  257. // }
  258. // }
  259. // function materiotheme_preprocess_links__language_block(&$vars){
  260. // // dpm($vars);
  261. // // foreach ($vars['links'] as $lang_code => $link) {
  262. // // $vars['links'][$lang_code]['text'] = $lang_code;
  263. // // $vars['links'][$lang_code]['link']['#title'] = $lang_code;
  264. // // }
  265. // }
  266. function materiotheme_theme_suggestions_taxonomy_term_alter(&$suggestions, &$vars){
  267. // ksm($suggestions);
  268. // ksm($vars);
  269. $original = $vars['theme_hook_original'];
  270. $bundle = $vars['elements']['#taxonomy_term']->bundle();
  271. $viewmode = $vars['elements']["#view_mode"];
  272. $suggestions[] = $original.'__'.$bundle.'__'.$viewmode;
  273. // dsm($suggestions);
  274. }
  275. function materiotheme_theme_suggestions_field_alter(&$suggestions, &$vars){
  276. if($vars['element']["#entity_type"] === "commerce_product_variation"
  277. && $vars['element']["#bundle"] === "materio_product_variation_type"
  278. && $vars['element']["#field_name"] === "title"){
  279. $test = 'test';
  280. $original = $vars['theme_hook_original'];
  281. $entity_type = $vars['element']["#entity_type"];
  282. $bundle = $vars['element']["#bundle"];
  283. $field_name = $vars['element']["#field_name"];
  284. $viewmode = $vars['element']['#view_mode'];
  285. $suggestions[] = $original.'__'.$entity_type.'__'.$bundle.'__'.$field_name.'__'.$viewmode;
  286. }
  287. }
  288. /**
  289. * Implements hook_theme_suggestions_HOOK_alter().
  290. */
  291. function materiotheme_theme_suggestions_image_alter(array &$suggestions, array $variables){
  292. // $image = $variables['attributes']['class'][0];
  293. $suggestions[] = 'image__'.$variables['style_name'];
  294. }
  295. /**
  296. * Prepares variables for product templates.
  297. *
  298. * Default template: commerce-product.html.twig.
  299. *
  300. * @param array $variables
  301. * An associative array containing:
  302. * - elements: An associative array containing rendered fields.
  303. * - attributes: HTML attributes for the containing element.
  304. */
  305. function materiotheme_preprocess_commerce_product(array &$variables) {
  306. $test="test";
  307. // remove the variation as we already display it via views
  308. if($variables['elements']['#view_mode'] === 'order_summary'
  309. || $variables['elements']['#view_mode'] === 'home_summary'){
  310. unset($variables['product']['variation_title']);
  311. unset($variables['product']['variation_field_description']);
  312. unset($variables['product']['variation_price']);
  313. unset($variables['product']['variation_field_multiple']);
  314. unset($variables['product']['variation_commerce_variation_cart_form']);
  315. }
  316. $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  317. $variables['language'] = $language;
  318. }
  319. /**
  320. * Implements hook_theme_suggestions_commerce_product_variation().
  321. */
  322. // function materiotheme_theme_suggestions_commerce_product_variation_later(&$suggestions, &$vars) {
  323. // $test = 'test';
  324. // }
  325. function materiotheme_preprocess_printable(array &$variables) {
  326. $site_config = \Drupal::config('system.site');
  327. $variables['site_name'] = $site_config->get('name');
  328. $variables['slogan'] = $site_config->get('slogan');
  329. }
  330. // TODO: instead of lazy load home images, make a html light home (without images),
  331. // replaced then by rich home vuejs
  332. // function materiotheme_preprocess_image(array &$variables) {
  333. // if ($variables['style_name'] === 'card_small_home') {
  334. // $variables['attributes']['data-src'] = $variables['attributes']['src'];
  335. // $variables['attributes']['src'] = '/themes/custom/materiotheme/assets/img/blank.gif';
  336. // }
  337. // }
  338. function materiotheme_preprocess_field__node__field_a_database__frontpage(array &$variables) {
  339. if ($variables['logged_in']) {
  340. $variables['label_link'] = array(
  341. "href" => '/' . $variables['element']['#language'] . '/base'
  342. );
  343. }
  344. }
  345. function materiotheme_preprocess_field__node__field_blabla__frontpage(array &$variables) {
  346. $variables['label_link'] = array(
  347. "href" => '/' . $variables['element']['#language'] . '/blabla'
  348. );
  349. }
  350. function materiotheme_preprocess_field__node__field_showrooms__frontpage(array &$variables) {
  351. $variables['label_link'] = array(
  352. "href" => '/' . $variables['element']['#language'] . '/showrooms'
  353. );
  354. }