materio-d9/web/themes/custom/materiotheme/materiotheme.theme

452 lines
15 KiB
PHP
Raw Permalink Normal View History

2019-03-25 18:27:56 +01:00
<?php
/**
* @file
2019-06-11 18:04:46 +02:00
* Functions to support theming in the materio theme.
2019-03-25 18:27:56 +01:00
*/
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);
// }
2019-06-11 18:04:46 +02:00
global $base_url;
$theme = \Drupal::theme()->getActiveTheme();
$vars['#attached']['drupalSettings']['path']['themePath'] = $base_url .'/'. $theme->getPath();
2019-03-25 18:27:56 +01:00
$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'];
2019-03-25 18:27:56 +01:00
// $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',
2021-05-31 16:59:15 +02:00
'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'];
2019-03-25 18:27:56 +01:00
}
function materiotheme_preprocess_page(&$vars){
// dsm($vars, 'vars');
2019-03-25 18:27:56 +01:00
}
/**
* 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());
}
2019-06-11 18:04:46 +02:00
// 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
// ));
// }
2019-03-25 18:27:56 +01:00
// function materiotheme_preprocess_node_materiau_teaser(&$vars){
// $vars['attributes']['class'] = 'card';
// kint($vars['attributes']);
// }
2021-06-09 13:06:52 +02:00
2021-07-28 15:58:33 +02:00
/**
* 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;
}
}
2021-06-09 13:06:52 +02:00
/**
* 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)"
);
}
2021-06-11 10:31:22 +02:00
// change "add to cart" button text
switch ($form_id) {
2022-03-31 19:25:32 +02:00
case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_6':
2021-06-11 10:31:22 +02:00
$form['actions']['submit']['#value'] = t('Yeees!');
break;
2022-03-31 19:25:32 +02:00
case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_7':
2021-06-11 10:31:22 +02:00
$form['actions']['submit']['#value'] = t('Yay!');
break;
2022-03-31 19:25:32 +02:00
case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_8':
2021-06-11 10:31:22 +02:00
$form['actions']['submit']['#value'] = t('Great!');
break;
2022-03-31 19:25:32 +02:00
case 'commerce_order_item_variation_cart_form_form_commerce_product_variation_9':
2021-06-11 10:31:22 +02:00
$form['actions']['submit']['#value'] = t('OKAY!');
break;
}
2021-06-09 13:06:52 +02:00
}
2019-03-25 18:27:56 +01:00
/**
* 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']);
}
2022-04-05 12:39:09 +02:00
// "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;
}
}
2019-03-25 18:27:56 +01:00
/**
* 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.
*/
2019-06-11 18:04:46 +02:00
// function materiotheme_preprocess_image_formatter(&$vars){
// if (isset($vars['url'])) {
2019-06-11 18:04:46 +02:00
// $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);
// }
// }
2019-03-25 18:27:56 +01:00
2019-06-11 18:04:46 +02:00
// 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;
// // }
// }
2019-07-16 17:01:18 +02:00
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);
}
2021-01-04 14:20:04 +01:00
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']);
}
2021-01-26 10:58:09 +01:00
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$variables['language'] = $language;
}
2021-01-04 14:20:04 +01:00
/**
* Implements hook_theme_suggestions_commerce_product_variation().
*/
// function materiotheme_theme_suggestions_commerce_product_variation_later(&$suggestions, &$vars) {
// $test = 'test';
// }
2021-01-25 22:19:21 +01:00
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';
// }
// }
2021-06-10 19:45:30 +02:00
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'
);
}
2021-08-24 23:23:08 +02:00
function materiotheme_preprocess_file_link(array &$variables) {
$url = $variables['link']['#url'];
$attributes = $url->getOption('attributes');
$attributes['target'] = '_blank';
$url->setOption('attributes', $attributes);
$t="t";
}