materiotheme.theme 6.1 KB

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