Skip to content

Instantly share code, notes, and snippets.

@vkechagias
vkechagias / hook.theme.php
Created October 21, 2019 12:32
Drupal 8 get page title from route and set this to page_title block
function HOOK_preprocess_block__page_title_block(&$variables) {
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
if (!empty($page_title) && empty($variables['content']['#title'])) {
$variables['content']['#title'] = $page_title;
}
}
@vkechagias
vkechagias / break-word.css
Created May 5, 2021 15:33
Break long overflowing strings in Chrome
* {
overflow-wrap: break-word;
}
@vkechagias
vkechagias / rename.sh
Last active February 1, 2021 14:43
Mass rename .yml files in shell from cleanplus to corporateplus
rename 's/cleanplus/corporateplus/g' *.yml
@vkechagias
vkechagias / tourism-mobile.json
Last active July 27, 2020 16:45
Tourism+ lighthouse report
{
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
"environment": {
"networkUserAgent": "Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3963.0 Mobile Safari/537.36 Chrome-Lighthouse",
"hostUserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
"benchmarkIndex": 1214
},
"lighthouseVersion": "6.0.0",
"fetchTime": "2020-07-27T16:14:55.670Z",
"requestedUrl": "http://localhost/mtt-tourismplus/site/d8/",
@vkechagias
vkechagias / git-tree.sh
Created June 3, 2020 13:53
Git tree in cli
#!/bin/bash
git log --oneline --graph --color --all --decorate
@vkechagias
vkechagias / phpstan.neon
Last active February 7, 2019 15:42
Sample phpstan.neon file to use when checking for Drupal 8 deprecated functions
parameters:
customRulesetUsed: true
reportUnmatchedIgnoredErrors: false
# Exclude directories that you may need to
excludes_analyse:
- themes/custom/scholarly/js/revolution
# Ignore phpstan-drupal extension's rules.
ignoreErrors:
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
- '#Plugin definitions cannot be altered.#'
@vkechagias
vkechagias / commands.sh
Last active February 7, 2019 15:38
Drupal 8: Check theme for deprecated functions
composer require mglaman/phpstan-drupal phpstan/phpstan-deprecation-rules --dev
cp phpstan.neon # place in drupal root / as in https://gist.github.com/vkechagias/05e8388734c78503cff9d5ea4e1acc19
./vendor/bin/phpstan analyse themes/custom/THEMENAME
@vkechagias
vkechagias / themename.theme.php
Created January 29, 2019 14:07
Drupal 8: Get crop types of image if any
<?php
function minimalplus_preprocess_node(&$variables) {
$field_image = $variables['elements']['field_image'];
if (!empty($field_image) && !empty($field_image['#items']->first()) && !empty($field_image['#items']->first()->target_id)) {
$image_tid = $field_image['#items']->first()->target_id;
$image = File::load($image_tid);
$moduleHandler = \Drupal::service('module_handler');
if (!empty($image) && !empty($image->getFileUri()) && $moduleHandler->moduleExists('crop')) {
$crops = \Drupal::entityTypeManager()->getStorage('crop')->loadByProperties(['uri' => $image->getFileUri()]);
@vkechagias
vkechagias / themename.theme.php
Last active January 15, 2019 14:23
Drupal 8: Proper way to consistently get field entity according to active language in themename.theme
function THEME_preprocess_page__node(&$variables) {
$translated_node = \Drupal::service('entity.repository')->getTranslationFromContext($variables['node']);
if (!empty($translated_node)) {
$field_data = $translated_node->get('FIELD_MACHINE_NAME')->getValue();
}
}
@vkechagias
vkechagias / themename.theme.php
Last active January 15, 2019 14:23
Drupal 8: Get current page route name
function THEME_preprocess_page(&$variables) {
$variables['route_name'] = \Drupal::routeMatch()->getRouteName();
}