materiotheme.theme 6.8 KB

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