materiotheme.theme 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. }
  82. function materiotheme_preprocess_page(&$vars){
  83. // dsm($vars, 'vars');
  84. }
  85. // function materiotheme_preprocess_node(&$vars){
  86. // $node = $vars['elements']['#node'];
  87. // $options = ['absolute' => TRUE];
  88. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  89. // $system_path = $url->getInternalPath();
  90. // $vars['link_attributes'] = new Attribute(array(
  91. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path
  92. // ));
  93. // }
  94. // function materiotheme_preprocess_node_materiau_teaser(&$vars){
  95. // $vars['attributes']['class'] = 'card';
  96. // kint($vars['attributes']);
  97. // }
  98. /**
  99. * Implements hook_form_alter
  100. */
  101. function materiotheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  102. // dpm($form_id, 'form_id');
  103. // dpm($form, 'form');
  104. $form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
  105. unset($form['name']['#title']);
  106. $form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
  107. unset($form['pass']['#title']);
  108. }
  109. /**
  110. * Implements hook_theme_suggestions_HOOK_alter().
  111. */
  112. /**
  113. * Prepares variables for image formatter templates.
  114. *
  115. * Default template: image-formatter.html.twig.
  116. *
  117. * @param array $variables
  118. * An associative array containing:
  119. * - item: An ImageItem object.
  120. * - item_attributes: An optional associative array of html attributes to be
  121. * placed in the img tag.
  122. * - image_style: An optional image style.
  123. * - url: An optional \Drupal\Core\Url object.
  124. */
  125. // function materiotheme_preprocess_image_formatter(&$vars){
  126. // if (isset($vars['url'])) {
  127. // $system_path = $vars['url']->getInternalPath();
  128. // $vars['link_attributes'] = new Attribute(array(
  129. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  130. // 'class' => array('ajax-link')
  131. // ));
  132. // // dpm($vars);
  133. // }
  134. // }
  135. // function materiotheme_preprocess_links__language_block(&$vars){
  136. // // dpm($vars);
  137. // // foreach ($vars['links'] as $lang_code => $link) {
  138. // // $vars['links'][$lang_code]['text'] = $lang_code;
  139. // // $vars['links'][$lang_code]['link']['#title'] = $lang_code;
  140. // // }
  141. // }
  142. function materiotheme_theme_suggestions_taxonomy_term_alter(&$suggestions, &$vars){
  143. // ksm($suggestions);
  144. // ksm($vars);
  145. $original = $vars['theme_hook_original'];
  146. $bundle = $vars['elements']['#taxonomy_term']->bundle();
  147. $viewmode = $vars['elements']["#view_mode"];
  148. $suggestions[] = $original.'__'.$bundle.'__'.$viewmode;
  149. // dsm($suggestions);
  150. }
  151. function materiotheme_theme_suggestions_field_alter(&$suggestions, &$vars){
  152. if($vars['element']["#entity_type"] === "commerce_product_variation"
  153. && $vars['element']["#bundle"] === "materio_product_variation_type"
  154. && $vars['element']["#field_name"] === "title"){
  155. $test = 'test';
  156. $original = $vars['theme_hook_original'];
  157. $entity_type = $vars['element']["#entity_type"];
  158. $bundle = $vars['element']["#bundle"];
  159. $field_name = $vars['element']["#field_name"];
  160. $viewmode = $vars['element']['#view_mode'];
  161. $suggestions[] = $original.'__'.$entity_type.'__'.$bundle.'__'.$field_name.'__'.$viewmode;
  162. }
  163. }
  164. /**
  165. * Prepares variables for product templates.
  166. *
  167. * Default template: commerce-product.html.twig.
  168. *
  169. * @param array $variables
  170. * An associative array containing:
  171. * - elements: An associative array containing rendered fields.
  172. * - attributes: HTML attributes for the containing element.
  173. */
  174. function materiotheme_preprocess_commerce_product(array &$variables) {
  175. $test="test";
  176. // remove the variation as we already display it via views
  177. if($variables['elements']['#view_mode'] === 'order_summary'
  178. || $variables['elements']['#view_mode'] === 'home_summary'){
  179. unset($variables['product']['variation_title']);
  180. unset($variables['product']['variation_field_description']);
  181. unset($variables['product']['variation_price']);
  182. unset($variables['product']['variation_field_multiple']);
  183. unset($variables['product']['variation_commerce_variation_cart_form']);
  184. }
  185. $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  186. $variables['language'] = $language;
  187. }
  188. /**
  189. * Implements hook_theme_suggestions_commerce_product_variation().
  190. */
  191. // function materiotheme_theme_suggestions_commerce_product_variation_later(&$suggestions, &$vars) {
  192. // $test = 'test';
  193. // }
  194. function materiotheme_preprocess_printable(array &$variables) {
  195. $site_config = \Drupal::config('system.site');
  196. $variables['site_name'] = $site_config->get('name');
  197. $variables['slogan'] = $site_config->get('slogan');
  198. }