123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- <?php
- /**
- * @file
- * Functions to support theming in the materio theme.
- */
- use Drupal\Core\Url;
- use Drupal\Core\Link;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\Template\Attribute;
- use Drupal\Component\Utility\Unicode;
- use Drupal\Core\Render\Element;
- /**
- * Implements hook_page_attachments().
- * @param array $attachments
- */
- // this does not work with themes
- // function materiotheme_page_attachments(array &$attachments) {
- // dpm('materiotheme_page_attachments', $attachments);
- // }
- /**
- * Prepares variables for HTML document templates.
- *
- * Default template: html.html.twig.
- *
- * @param array $variables
- * An associative array containing:
- * - page: A render element representing the page.
- */
- function materiotheme_preprocess_html(&$vars) {
- // $head_title = $vars['head_title'];
- // dpm($vars);
- $site_config = \Drupal::config('system.site');
- // dpm($site_config->get('slogan'));
- // array_push($head_title, [
- // 'name' => $site_config->get('name'),
- // ]);
- // $vars['head_title'] = $head_title;
- // $title = "The new title";
- // $request = \Drupal::request();
- // if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
- // $route->setDefault('_title', $title);
- // }
- global $base_url;
- $theme = \Drupal::theme()->getActiveTheme();
- $vars['#attached']['drupalSettings']['path']['themePath'] = $base_url .'/'. $theme->getPath();
- $description = [
- '#tag' => 'meta',
- '#attributes' => [
- 'name' => 'description',
- 'content' => $site_config->get('slogan'),
- ],
- ];
- $vars['page']['#attached']['html_head'][] = [$description, 'description'];
- $viewport = array(
- '#tag' => 'meta',
- '#attributes' => array(
- 'name' => 'viewport',
- 'content' => 'width=device-width, initial-scale=1, maximum-scale=1',
- ),
- );
- $vars['page']['#attached']['html_head'][] = [$viewport, 'viewport'];
- // drupal_add_html_head($viewport, 'viewport');
- // https://stackoverflow.com/a/48700852
- // <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
- $content_security_policy = array(
- '#tag' => 'meta',
- '#attributes' => array(
- 'http-equiv' => 'Content-Security-Policy',
- 'content' => 'upgrade-insecure-requests',
- ),
- );
- $vars['page']['#attached']['html_head'][] = [$content_security_policy, 'content_security_policy'];
- // $gv = [
- // '#tag' => 'meta',
- // '#attributes' => [
- // 'name' => 'google-site-verification',
- // 'content' => "Y6PSbMfj67bXtMRAT-mFTAxrIeZPzC5jWSpH3M7yhkk",
- // ],
- // ];
- // $vars['page']['#attached']['html_head'][] = [$gv, "google-site-verification"];
- // <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
- $ati = array(
- '#tag' => 'meta',
- '#attributes' => array(
- 'rel' => 'apple-touch-icon',
- 'sizes' => "180x180",
- 'href' => '/apple-touch-icon.png',
- ),
- );
- $vars['page']['#attached']['html_head'][] = [$ati, 'ati'];
- // <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
- $icon32 = array(
- '#tag' => 'meta',
- '#attributes' => array(
- 'rel' => 'icon',
- 'type' => "image/png",
- 'sizes' => "32x32",
- 'href' => '/favicon-32x32.png',
- ),
- );
- $vars['page']['#attached']['html_head'][] = [$icon32, 'icon32'];
- // <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
- $icon16 = array(
- '#tag' => 'meta',
- '#attributes' => array(
- 'rel' => 'icon',
- 'type' => "image/png",
- 'sizes' => "16x16",
- 'href' => '/favicon-16x16.png',
- ),
- );
- $vars['page']['#attached']['html_head'][] = [$icon16, 'icon16'];
- // <link rel="manifest" href="/site.webmanifest">
- // <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
- $safaripinnedtab = array(
- '#tag' => 'meta',
- '#attributes' => array(
- 'rel' => 'mask-icon',
- 'href' => '/safari-pinned-tab.svg',
- 'color' => "#69ccce",
- ),
- );
- $vars['page']['#attached']['html_head'][] = [$safaripinnedtab, '$safaripinnedtab'];
- // <meta name="msapplication-TileColor" content="#da532c">
- $ms = array(
- '#tag' => 'meta',
- '#attributes' => array(
- 'name' => 'msapplication-TileColor',
- 'content' => "#69ccce"
- ),
- );
- $vars['page']['#attached']['html_head'][] = [$ms, '$ms'];
- // <meta name="theme-color" content="#ffffff">
- // $tc = array(
- // '#tag' => 'meta',
- // '#attributes' => array(
- // 'name' => 'theme-color',
- // 'content' => "#69ccce"
- // ),
- // );
- // $vars['page']['#attached']['html_head'][] = [$tc, '$tc'];
- // <meta http-equiv="pragma" content="no-cache" />
- $pragma = array(
- '#tag' => 'meta',
- '#attributes' => array(
- 'http-equiv' => 'pragma',
- 'content' => "no-cache"
- ),
- );
- $vars['page']['#attached']['html_head'][] = [$pragma, '$pragma'];
- // <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate" />
- $cachecontrol = array(
- '#tag' => 'meta',
- '#attributes' => array(
- 'http-equiv' => 'cache-control',
- 'content' => "no-cache, no-store, must-revalidate"
- ),
- );
- $vars['page']['#attached']['html_head'][] = [$cachecontrol, '$cachecontrol'];
- }
- function materiotheme_preprocess_page(&$vars){
- // dsm($vars, 'vars');
- }
- /**
- * Prepares variables for email template.
- *
- * Default template: email.html.twig.
- *
- * @param array $variables
- * An associative array containing:
- * - email: An associative array containing the Email object.
- */
- function materiotheme_preprocess_email(&$variables) {
- $email = $variables['email'];
- $variables['body'] = $email->getBody();
- $variables = array_merge($variables, $email->getVariables());
- }
- // function materiotheme_preprocess_node(&$vars){
- // $node = $vars['elements']['#node'];
- // $options = ['absolute' => TRUE];
- // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
- // $system_path = $url->getInternalPath();
- // $vars['link_attributes'] = new Attribute(array(
- // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path
- // ));
- // }
- // function materiotheme_preprocess_node_materiau_teaser(&$vars){
- // $vars['attributes']['class'] = 'card';
- // kint($vars['attributes']);
- // }
- /**
- * Implements THEME_preprocess_page_title()
- */
- function materiotheme_preprocess_page_title(&$variables) {
- $current_url = \Drupal\Core\Url::fromRoute('<current>');
- $url = $current_url->getInternalPath();
- if(preg_match('/^user\/\d+\/*/', $url)) {
- $userCurrent = \Drupal::currentUser();
- $user = \Drupal\user\Entity\User::load($userCurrent->id());
- $email = $user->getEmail();
- $variables['title'] = $email;
- }
- }
- /**
- * Implements hook_form_alter
- */
- function materiotheme_form_alter(&$form, FormStateInterface $form_state, $form_id) {
- // commerce_order_item_variation_cart_form_form_commerce_product_variation_3
- preg_match('/commerce_order_item_variation_cart_form_form_commerce_product_variation_(\d+)/', $form_id, $matches);
- if ($matches && isset($matches[1]) && $variation_id = $matches[1]) {
- // if (isset($form['#entity_type']) && $form['#entity_type'] == "commerce_order_item") {
- // $product = $form_state->storage['product'];
- // $storage = &$form_state->getStorage();
- // $product = $storage['product'];
- // $variations = $product->fields['variations'];
- $form['actions']['submit']['#attributes'] += array(
- "@click.prevent.stop" => "checkaddtocart(\$event, $variation_id)"
- );
- }
- // change "add to cart" button text
- switch ($form_id) {
- case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_6':
- $form['actions']['submit']['#value'] = t('Yeees!');
- break;
- case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_7':
- $form['actions']['submit']['#value'] = t('Yay!');
- break;
- case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_8':
- $form['actions']['submit']['#value'] = t('Great!');
- break;
- case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_9':
- $form['actions']['submit']['#value'] = t('OKAY!');
- break;
- }
- }
- /**
- * Implements hook_form_alter
- */
- function materiotheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
- // dpm($form_id, 'form_id');
- // dpm($form, 'form');
- $form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
- unset($form['name']['#title']);
- $form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
- unset($form['pass']['#title']);
- }
- // "simplenews_subscriptions_block_footersimplenewssubscriptionblock"
- function materiotheme_form_simplenews_subscriptions_block_footersimplenewssubscriptionblock_alter(&$form, FormStateInterface $form_state, $form_id) {
- $form['#cache']['max-age'] = 0;
- // unset($form['mail']['widget'][0]['#title']);
- // $form['mail']['widget']['#attributes']['placeholder'][] = "Bon alors !";
- // $form['mail']['widget']['#attributes']['placeholder'] = (string) $form['mail']['widget']['#title'];
- $form['mail']['widget'][0]['value']['#placeholder'] = (string) $form['mail']['widget'][0]['value']['#title'];
- $form['mail']['widget'][0]['value']['#size'] = 35;
- unset($form['mail']['widget'][0]['value']['#title']);
- // unset($form['mail']['widget']['#description']);
-
- // $form['subscriptions']['widget']['#required']
- // unset($form['subscriptions']['widget']['#title']);
- // unset($form['subscriptions']['widget']['#description']);
- $t="t";
- }
- function materiotheme_preprocess_block(&$variables) {
- if ($variables['plugin_id'] == 'simplenews_subscription_block') {
- $variables['#cache']['max-age'] = 0;
- }
- }
- /**
- * Implements hook_theme_suggestions_HOOK_alter().
- */
- /**
- * Prepares variables for image formatter templates.
- *
- * Default template: image-formatter.html.twig.
- *
- * @param array $variables
- * An associative array containing:
- * - item: An ImageItem object.
- * - item_attributes: An optional associative array of html attributes to be
- * placed in the img tag.
- * - image_style: An optional image style.
- * - url: An optional \Drupal\Core\Url object.
- */
- // function materiotheme_preprocess_image_formatter(&$vars){
- // if (isset($vars['url'])) {
- // $system_path = $vars['url']->getInternalPath();
- // $vars['link_attributes'] = new Attribute(array(
- // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
- // 'class' => array('ajax-link')
- // ));
- // // dpm($vars);
- // }
- // }
- // function materiotheme_preprocess_links__language_block(&$vars){
- // // dpm($vars);
- // // foreach ($vars['links'] as $lang_code => $link) {
- // // $vars['links'][$lang_code]['text'] = $lang_code;
- // // $vars['links'][$lang_code]['link']['#title'] = $lang_code;
- // // }
- // }
- function materiotheme_theme_suggestions_taxonomy_term_alter(&$suggestions, &$vars){
- // ksm($suggestions);
- // ksm($vars);
- $original = $vars['theme_hook_original'];
- $bundle = $vars['elements']['#taxonomy_term']->bundle();
- $viewmode = $vars['elements']["#view_mode"];
- $suggestions[] = $original.'__'.$bundle.'__'.$viewmode;
- // dsm($suggestions);
- }
- function materiotheme_theme_suggestions_field_alter(&$suggestions, &$vars){
- if($vars['element']["#entity_type"] === "commerce_product_variation"
- && $vars['element']["#bundle"] === "materio_product_variation_type"
- && $vars['element']["#field_name"] === "title"){
- $test = 'test';
- $original = $vars['theme_hook_original'];
- $entity_type = $vars['element']["#entity_type"];
- $bundle = $vars['element']["#bundle"];
- $field_name = $vars['element']["#field_name"];
- $viewmode = $vars['element']['#view_mode'];
- $suggestions[] = $original.'__'.$entity_type.'__'.$bundle.'__'.$field_name.'__'.$viewmode;
- }
- }
- /**
- * Implements hook_theme_suggestions_HOOK_alter().
- */
- function materiotheme_theme_suggestions_image_alter(array &$suggestions, array $variables){
- // $image = $variables['attributes']['class'][0];
- $suggestions[] = 'image__'.$variables['style_name'];
- }
- /**
- * Prepares variables for product templates.
- *
- * Default template: commerce-product.html.twig.
- *
- * @param array $variables
- * An associative array containing:
- * - elements: An associative array containing rendered fields.
- * - attributes: HTML attributes for the containing element.
- */
- function materiotheme_preprocess_commerce_product(array &$variables) {
- $test="test";
- // remove the variation as we already display it via views
- if($variables['elements']['#view_mode'] === 'order_summary'
- || $variables['elements']['#view_mode'] === 'home_summary'){
- unset($variables['product']['variation_title']);
- unset($variables['product']['variation_field_description']);
- unset($variables['product']['variation_price']);
- unset($variables['product']['variation_field_multiple']);
- unset($variables['product']['variation_commerce_variation_cart_form']);
- }
- $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
- $variables['language'] = $language;
- }
- /**
- * Implements hook_theme_suggestions_commerce_product_variation().
- */
- // function materiotheme_theme_suggestions_commerce_product_variation_later(&$suggestions, &$vars) {
- // $test = 'test';
- // }
- function materiotheme_preprocess_printable(array &$variables) {
- $site_config = \Drupal::config('system.site');
- $variables['site_name'] = $site_config->get('name');
- $variables['slogan'] = $site_config->get('slogan');
- }
- // TODO: instead of lazy load home images, make a html light home (without images),
- // replaced then by rich home vuejs
- // function materiotheme_preprocess_image(array &$variables) {
- // if ($variables['style_name'] === 'card_small_home') {
- // $variables['attributes']['data-src'] = $variables['attributes']['src'];
- // $variables['attributes']['src'] = '/themes/custom/materiotheme/assets/img/blank.gif';
- // }
- // }
- function materiotheme_preprocess_field__node__field_a_database__frontpage(array &$variables) {
- if ($variables['logged_in']) {
- $variables['label_link'] = array(
- "href" => '/' . $variables['element']['#language'] . '/base'
- );
- }
- }
- function materiotheme_preprocess_field__node__field_blabla__frontpage(array &$variables) {
- $variables['label_link'] = array(
- "href" => '/' . $variables['element']['#language'] . '/blabla'
- );
- }
- function materiotheme_preprocess_field__node__field_showrooms__frontpage(array &$variables) {
- $variables['label_link'] = array(
- "href" => '/' . $variables['element']['#language'] . '/showrooms'
- );
- }
- function materiotheme_preprocess_file_link(array &$variables) {
- $url = $variables['link']['#url'];
- $attributes = $url->getOption('attributes');
- $attributes['target'] = '_blank';
- $url->setOption('attributes', $attributes);
- $t="t";
- }
|