Skip to content

Instantly share code, notes, and snippets.

View vyspiansky's full-sized avatar

Ihor Vyspiansky vyspiansky

View GitHub Profile
@vyspiansky
vyspiansky / file_extension_by_contents.md
Last active February 21, 2025 13:40
PHP: retrieve a file extension by its contents

Using Symfony's MimeTypes

use Symfony\Component\Mime\MimeTypes;

function getFileExtension(string $fileContent): ?string
{
    $mimeType = (new \finfo(FILEINFO_MIME_TYPE))->buffer($fileContent);

 $mimeTypes = new MimeTypes();
@vyspiansky
vyspiansky / move_file_to_directory_in_drupal.php
Last active March 18, 2025 10:52
Move file to directory in Drupal 10
<?php
use Drupal\Core\File\FileSystemInterface;
$directory = '/path/to/processed';
\Drupal::service('file_system')
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
\Drupal::service('file_system')
@vyspiansky
vyspiansky / extend_user_registration_form_in_drupal_10.md
Last active March 19, 2025 07:36
Extend a standard user registration form using classes in Drupal 10

To extend the standard user registration form, use

namespace Drupal\your_module\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\user\RegisterForm;

class YourRegisterForm extends RegisterForm {
 public function buildForm(array $form, FormStateInterface $form_state) {
@vyspiansky
vyspiansky / password_validation_message_in_drupal_10.md
Last active March 19, 2025 07:36
Change client-side password validation message in Drupal 10

We are going to replace "Make it at least 8 characters" from the user core modules (core/modules/user/user.module) with "Make it at least 12 characters".

/**
 * Implements hook_element_info_alter().
 */
function <your_module>_element_info_alter(array &$types): void
{
    if (isset($types['password_confirm'])) {
 $types['password_confirm']['#process'][] = 'users_form_process_password_confirm';
@vyspiansky
vyspiansky / ngrok_notes.md
Last active March 19, 2025 07:37
ngrok: create a tunnel from the public internet to your local machine

ngrok notes

ngrok creates a tunnel from the public internet to your local machine.

Install

Sign up for an ngrok account https://dashboard.ngrok.com/

brew install ngrok/ngrok/ngrok
@vyspiansky
vyspiansky / error_messages_visibility_in_drupal_10.md
Last active March 20, 2025 09:09
Visibility of error messages in Drupal 10

To configure the error messages visibility, set an appropriate configuration in the settings.php file

// Show all error messages, including notices and coding standards warnings.
$config['system.logging']['error_level'] = 'verbose';

File: /path/to/docroot/sites/<SITE_NAME>/settings.php.

Then clear the Drupal cache.

@vyspiansky
vyspiansky / common_iife_pattern.md
Last active March 20, 2025 09:10
Common Immediately Invoked Function Expression (IIFE) pattern

One of the ways to create an immediately executed anonymous function

(function() {
    'use strict';
    
    // ...
})();
@vyspiansky
vyspiansky / remove_nodes_via_drush.md
Last active November 28, 2024 13:08
Use Drush to remove nodes in Drupal 10

Drush version: 12.3.0.0.

Remove nodes with a specific bundle

If you have a large number of nodes and want to process them in batches, you can use the --chunk option:

drush entity:delete node --bundle=article --chunks=50
@vyspiansky
vyspiansky / uuid-crc-test.md
Last active November 24, 2024 11:03
UUID Field vs CRC Index Querying

Performance Comparison: UUID Field vs CRC Index Querying

  • MySQL version: 5.7.29
SELECT * FROM uuid_crc_test WHERE uuid = 'c3830c70-a8e6-11ef-b990-0242ac180005';
Number of records Execution times (ms)
@vyspiansky
vyspiansky / mysql-queries-using-timestamp-field.md
Last active October 25, 2024 09:43
MySQL queries using timestamp integer field

MySQL queries for selecting records based on different time criteria using a timestamp integer field named created.

Get records newer than today's 10 AM

SELECT
  *
FROM
  your_table
WHERE