Skip to content

Instantly share code, notes, and snippets.

View anxp's full-sized avatar

Andrii anxp

View GitHub Profile
function getCurrentDateTime () {
var objToday = new Date();
var domEnder = '';
if (/1/.test(String(parseInt((objToday + "").charAt(0), 10)))) {
domEnder = 'th';
} else if (parseInt((objToday + "").charAt(1), 10) === 1) {
domEnder = 'st';
} else if (parseInt((objToday + "").charAt(1), 10) === 2) {
domEnder = 'nd';
@anxp
anxp / form_state_syntactic_ambiguities.php
Created May 1, 2019 08:41
Drupal 8 features: Get a form element value VS set form error by element name
<?php
$form['#tree'] = TRUE;
$form['part_of_the_form'] = [
'#type' => 'container',
];
$form['part_of_the_form']['field_1'] = [
'#type' => 'textfield',
'#title' => 'Field-1',
@anxp
anxp / sanitizeText.php
Last active March 26, 2019 16:04
Function returns only allowed charachters from input string. Allowed charachters passes as array with ranges. Ranges are strings with start and end UFT-8 codes (as preg_match expects them).
<?php
/**
* Function filters out not-allowed symbols from string and return 'cleaned' string.
* Allowed symbols specified as array of UTF-8 address ranges.
* When it needs to change allowed range, consider this resource for all UTF-8 characters code sets: https://unicode-table.com/en/blocks/
* @param string $text input string to filter
* @param bool $stripTags remove HTML tags or not
* @param array $allowed_utf8_ranges array with ranges of allowed symbols
* @return mixed|null|string
* @throws Exception
@anxp
anxp / rand_string.php
Created February 21, 2019 19:52
PHP Random String Generator
<?php
/**
* Generate a random string, using a cryptographically secure
* pseudorandom number generator (random_int)
*
* For PHP 7, random_int is a PHP core function
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
*
* @param int $length How many characters do we want?
* @param string $keyspace A string of all possible characters
@anxp
anxp / external_db_check.php
Created January 30, 2019 20:28
Checking for EXTERNAL database and table existing by Drupal 7 API. First function check both, while second and third are just wrappers for initial one.
<?php
$db_connection_data = = [
'host' => 'localhost',
'database' => 'example_db',
'username' => 'root',
'password' => '',
'driver' => 'mysql',
];
@anxp
anxp / drupal_external_db_connect.php
Created January 30, 2019 20:20
Working with EXTERNAL database with Drupal 7 API - example.
<?php
//Array with connection credentials. This is the same structured array like in "settings.local.php" file in your Drupal set up.
$db_connection_data = [
'host' => 'localhost',
'database' => 'example_db',
'username' => 'root',
'password' => '',
'driver' => 'mysql',
];
@anxp
anxp / datetime_check.php
Last active January 28, 2019 20:44
A function to check if date/time is in acceptable interval.
<?php
//This function checks if specified date IS IN acceptable interval from NOW to some moment in the past.
//$date_to_check need to be in classic datetime format, like in mySQL;
//$interval sets like here http://php.net/manual/en/dateinterval.construct.php, but without 'P',
//'P' will be added automatically.
function is_date_acceptable($date_to_check, $interval) {
$now = new DateTime();
$interval = 'P'.$interval;
$min_acceptable_date = $now->sub(new DateInterval($interval));
@anxp
anxp / generate_table.js
Created January 27, 2019 17:37
Javascript table generation function
/**
* This function generates simple table with header row from specified data (tableContent, headerLabels).
* tableContent is 2-D array with DOM Elements as values:
* [
* 0 => [0 => DOM_Obj_1cell_1row, 1 => DOM_Obj_2cell_1row, 2 => DOM_Obj_3cell_1row, ...], <- first row
* 1 => [0 => DOM_Obj_1cell_2row, 1 => DOM_Obj_2cell_2row, 2 => DOM_Obj_3cell_2row, ...], <- second row
* ];
* headerLabels is 1-D array, also with DOM Elements as values (not just strings!);
* [
* 0 => DOM_Obj_headerlabel_1, 1 => DOM_Obj_headerlabel_2, ...
@anxp
anxp / email_validation_simple.php
Last active September 28, 2020 06:35
Simple email address validation - check syntax and MX record existing.
<?php
function _nxte_redeem_is_email_valid($email) {
if (empty ($email)) {return false;}
list($user_name, $mail_domain) = explode('@', $email);
if (empty($user_name) || empty($mail_domain)) {return false;}
$is_syntax_ok = (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
$is_MX_exists = checkdnsrr($mail_domain, 'MX');
@anxp
anxp / using_limit_validation_errors.php
Last active August 23, 2022 13:35
How to correctly use #limit_validation_errors element of Form API in Drupal 7
<?php
//Sometimes we need to submit only part of the form. Sure, if form has #required fields, all they will be highlighted with red,
//and form will not be submitted if these elements have no value.
//To workaround this, we can use '#limit_validation_errors' option AND custom submit function for every 'Partial Submit' button.
//Interesting note: when we use '#limit_validation_errors', we will see ALL form fields in _validate function,
//but in _submit function will be visible ONLY elements, included in '#limit_validation_errors' array!!!
//Case 1. The most simple.
//We want to make a 'reset' button, or 'previous step' button, so it must work even if some REQUIRED form elements are empty: