materiotheme.theme 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. }
  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_alter(&$form, FormStateInterface $form_state, $form_id) {
  163. // commerce_order_item_variation_cart_form_form_commerce_product_variation_3
  164. preg_match('/commerce_order_item_variation_cart_form_form_commerce_product_variation_(\d+)/', $form_id, $matches);
  165. if ($matches && isset($matches[1]) && $variation_id = $matches[1]) {
  166. // if (isset($form['#entity_type']) && $form['#entity_type'] == "commerce_order_item") {
  167. // $product = $form_state->storage['product'];
  168. // $storage = &$form_state->getStorage();
  169. // $product = $storage['product'];
  170. // $variations = $product->fields['variations'];
  171. $form['actions']['submit']['#attributes'] += array(
  172. "@click.prevent.stop" => "checkaddtocart(\$event, $variation_id)"
  173. );
  174. }
  175. // change "add to cart" button text
  176. switch ($form_id) {
  177. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_3':
  178. $form['actions']['submit']['#value'] = t('Yeees!');
  179. break;
  180. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_4':
  181. $form['actions']['submit']['#value'] = t('Yay!');
  182. break;
  183. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_5':
  184. $form['actions']['submit']['#value'] = t('Great!');
  185. break;
  186. case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_6':
  187. $form['actions']['submit']['#value'] = t('OKAY!');
  188. break;
  189. }
  190. }
  191. /**
  192. * Implements hook_form_alter
  193. */
  194. function materiotheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  195. // dpm($form_id, 'form_id');
  196. // dpm($form, 'form');
  197. $form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
  198. unset($form['name']['#title']);
  199. $form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
  200. unset($form['pass']['#title']);
  201. }
  202. /**
  203. * Implements hook_theme_suggestions_HOOK_alter().
  204. */
  205. /**
  206. * Prepares variables for image formatter templates.
  207. *
  208. * Default template: image-formatter.html.twig.
  209. *
  210. * @param array $variables
  211. * An associative array containing:
  212. * - item: An ImageItem object.
  213. * - item_attributes: An optional associative array of html attributes to be
  214. * placed in the img tag.
  215. * - image_style: An optional image style.
  216. * - url: An optional \Drupal\Core\Url object.
  217. */
  218. // function materiotheme_preprocess_image_formatter(&$vars){
  219. // if (isset($vars['url'])) {
  220. // $system_path = $vars['url']->getInternalPath();
  221. // $vars['link_attributes'] = new Attribute(array(
  222. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  223. // 'class' => array('ajax-link')
  224. // ));
  225. // // dpm($vars);
  226. // }
  227. // }
  228. // function materiotheme_preprocess_links__language_block(&$vars){
  229. // // dpm($vars);
  230. // // foreach ($vars['links'] as $lang_code => $link) {
  231. // // $vars['links'][$lang_code]['text'] = $lang_code;
  232. // // $vars['links'][$lang_code]['link']['#title'] = $lang_code;
  233. // // }
  234. // }
  235. function materiotheme_theme_suggestions_taxonomy_term_alter(&$suggestions, &$vars){
  236. // ksm($suggestions);
  237. // ksm($vars);
  238. $original = $vars['theme_hook_original'];
  239. $bundle = $vars['elements']['#taxonomy_term']->bundle();
  240. $viewmode = $vars['elements']["#view_mode"];
  241. $suggestions[] = $original.'__'.$bundle.'__'.$viewmode;
  242. // dsm($suggestions);
  243. }
  244. function materiotheme_theme_suggestions_field_alter(&$suggestions, &$vars){
  245. if($vars['element']["#entity_type"] === "commerce_product_variation"
  246. && $vars['element']["#bundle"] === "materio_product_variation_type"
  247. && $vars['element']["#field_name"] === "title"){
  248. $test = 'test';
  249. $original = $vars['theme_hook_original'];
  250. $entity_type = $vars['element']["#entity_type"];
  251. $bundle = $vars['element']["#bundle"];
  252. $field_name = $vars['element']["#field_name"];
  253. $viewmode = $vars['element']['#view_mode'];
  254. $suggestions[] = $original.'__'.$entity_type.'__'.$bundle.'__'.$field_name.'__'.$viewmode;
  255. }
  256. }
  257. /**
  258. * Implements hook_theme_suggestions_HOOK_alter().
  259. */
  260. function materiotheme_theme_suggestions_image_alter(array &$suggestions, array $variables){
  261. // $image = $variables['attributes']['class'][0];
  262. $suggestions[] = 'image__'.$variables['style_name'];
  263. }
  264. /**
  265. * Prepares variables for product templates.
  266. *
  267. * Default template: commerce-product.html.twig.
  268. *
  269. * @param array $variables
  270. * An associative array containing:
  271. * - elements: An associative array containing rendered fields.
  272. * - attributes: HTML attributes for the containing element.
  273. */
  274. function materiotheme_preprocess_commerce_product(array &$variables) {
  275. $test="test";
  276. // remove the variation as we already display it via views
  277. if($variables['elements']['#view_mode'] === 'order_summary'
  278. || $variables['elements']['#view_mode'] === 'home_summary'){
  279. unset($variables['product']['variation_title']);
  280. unset($variables['product']['variation_field_description']);
  281. unset($variables['product']['variation_price']);
  282. unset($variables['product']['variation_field_multiple']);
  283. unset($variables['product']['variation_commerce_variation_cart_form']);
  284. }
  285. $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  286. $variables['language'] = $language;
  287. }
  288. /**
  289. * Implements hook_theme_suggestions_commerce_product_variation().
  290. */
  291. // function materiotheme_theme_suggestions_commerce_product_variation_later(&$suggestions, &$vars) {
  292. // $test = 'test';
  293. // }
  294. function materiotheme_preprocess_printable(array &$variables) {
  295. $site_config = \Drupal::config('system.site');
  296. $variables['site_name'] = $site_config->get('name');
  297. $variables['slogan'] = $site_config->get('slogan');
  298. }
  299. // TODO: instead of lazy load home images, make a html light home (without images),
  300. // replaced then by rich home vuejs
  301. // function materiotheme_preprocess_image(array &$variables) {
  302. // if ($variables['style_name'] === 'card_small_home') {
  303. // $variables['attributes']['data-src'] = $variables['attributes']['src'];
  304. // $variables['attributes']['src'] = '/themes/custom/materiotheme/assets/img/blank.gif';
  305. // }
  306. // }
  307. function materiotheme_preprocess_field__node__field_a_database__frontpage(array &$variables) {
  308. if ($variables['logged_in']) {
  309. $variables['label_link'] = array(
  310. "href" => '/' . $variables['element']['#language'] . '/base'
  311. );
  312. }
  313. }
  314. function materiotheme_preprocess_field__node__field_blabla__frontpage(array &$variables) {
  315. $variables['label_link'] = array(
  316. "href" => '/' . $variables['element']['#language'] . '/blabla'
  317. );
  318. }
  319. function materiotheme_preprocess_field__node__field_showrooms__frontpage(array &$variables) {
  320. $variables['label_link'] = array(
  321. "href" => '/' . $variables['element']['#language'] . '/showrooms'
  322. );
  323. }