Skip to content

Instantly share code, notes, and snippets.

View vyspiansky's full-sized avatar

Ihor Vyspiansky vyspiansky

View GitHub Profile
@vyspiansky
vyspiansky / clear-older-recent-log-messages-in-drupal.sql
Last active September 9, 2024 15:00
Clear older recent log messages in Drupal 10, retaining only the last 500
-- Remove all watchdog's records from a MySQL table except for the last 500.
DELETE FROM `watchdog`
WHERE wid NOT IN (
SELECT wid
FROM (
SELECT wid
FROM `watchdog`
ORDER BY wid DESC
LIMIT 500
@vyspiansky
vyspiansky / plain_sql_query_in_drupal.php
Last active September 11, 2024 15:57
Retrieve a plain SQL query from a Select query object in Drupal 10
<?php
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\SelectInterface;
/**
* Returns a plain SQL query from a Select query object.
*/
function get_plain_sql_query(SelectInterface $query): string
{
@vyspiansky
vyspiansky / enable-xhprof-in-lando.md
Last active August 27, 2024 19:57
Enable XHProf in Lando

.lando.yml

name: "..."
recipe: lamp
config:
  php: "8.1"
  xdebug: false
services:
 appserver:
@vyspiansky
vyspiansky / drupal-clear-cache-programmatically.md
Last active August 22, 2024 08:19
Clear cache programmatically in Drupal 8+

Clear cache by specific tags

\Drupal::service('cache_tags.invalidator')->invalidateTags([$tag1, $tag2]);

Clear specific cache like render cache

\Drupal::service('cache.render')-&gt;invalidateAll()
@vyspiansky
vyspiansky / set-env-variable-in-htaccess.md
Created July 7, 2024 13:30
How to set environment variables in a .htaccess file
@vyspiansky
vyspiansky / validate_password_with_preg_match.php
Last active May 13, 2024 11:55
Validate a password using preg_match with regular expression
<?php
declare(strict_types=1);
/*
A pattern with multiple conditions:
- at least one digit.
- at least one lowercase letter (including special Scandinavian characters).
- at least one uppercase letter (including special Scandinavian characters).
- at least one character that is not a letter or a number (including special Scandinavian characters).
@vyspiansky
vyspiansky / response_403_in_controller.php
Created April 18, 2024 13:08
Drupal: return a 403 response from a controller
<?php
namespace Drupal\your_module\Controller;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class YourController {
public function yourPage() {
// Some logic here...
@vyspiansky
vyspiansky / timezone-for-ukraine-in-php-8.md
Created April 12, 2024 07:05
Get a timezone for Ukraine in PHP 8+

To retrieve a timezone for Ukraine in PHP 8+

php -r "var_export(DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'UA'));"

Possible output:

array (
@vyspiansky
vyspiansky / simple-unsubscribe-successful-template.html
Created April 11, 2024 19:55
Unsubscribe successful HTML template
<html>
<head>
<title>Unsubscribe successful</title>
</head>
<body>
<h1>Unsubscribe successful</h1>
<p>Your email has been successfully unsubscribed from this list.</p>
</body>
</html>
@vyspiansky
vyspiansky / zip_folder_ignoring_redundant_files_on_mac.md
Last active February 11, 2024 09:36
Archive folder without redundant files using command line on macOS

Archive a folder without redundant files using the command line on macOS.

cd <folder_path>

where <folder_path> — a folder which should be archived.

zip -r .zip . -x "*.git*" -x "*.DS_Store"