Skip to content

Instantly share code, notes, and snippets.

<?php
use Drupal\Core\Database\Database;
// Method one.
$result = \Drupal::database()->query('SELECT * FROM my_additional_db.my_table')->fetchAllAssoc('my_column');
dump($result);
// Method two.
$conn = Database::setActiveConnection('my_extra_connection');
@drugan
drugan / aggregate_functions_example.sql
Created September 28, 2023 17:08
Understand sql DISTINCT, GROUP BY, COUNT, SUM, AVG, MIN, MAX functions.
CREATE TABLE IF NOT EXISTS employee (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birthdate DATE,
hire_date DATE,
job_title VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
@drugan
drugan / join_examples.sql
Created September 28, 2023 09:45
Understand sql SELF, FULL, LEFT, RIGHT, INNER, NATURAL, CROSS joins.
CREATE TABLE IF NOT EXISTS continents ( continent_code VARCHAR(2) PRIMARY KEY, continent_name VARCHAR(25) NOT NULL );
INSERT IGNORE INTO continents VALUES ('AS', 'Asia'), ('AF', 'Africa'), ('NA', 'North America'), ('SA', 'South America'), ('EU', 'Europe'), ('AU', 'Australia')
ON DUPLICATE KEY UPDATE continent_name = VALUES(continent_name);
CREATE TABLE IF NOT EXISTS countries ( country_code VARCHAR(2) PRIMARY KEY, country_name VARCHAR(25), continent_code VARCHAR(2) );
INSERT IGNORE INTO countries VALUES ('IN', 'India', 'AS'), ('ZA', 'South Africa', 'AF'), ('US', 'United States', 'NA'), ('BR', 'Brazil', 'SA'), ('AU', 'Australia', 'AU'), ('AQ', 'Antarctica', 'AN')
ON DUPLICATE KEY UPDATE country_name = VALUES(country_name);
/* "SELF" JOIN trick. */
SELECT cr1.country_name "Name Self", cr1.country_code "Country Self", cr1.continent_code "Continent Self"

Set up extMerge as the merging tool

Open your .gitconfig and put this into the file:

[merge]  
tool = extMerge  
[mergetool "extMerge"]  
cmd = extMerge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"  
trustExitCode = false
@drugan
drugan / Drupal 8: alter field widget form element
Created August 31, 2017 16:20
The filed widget defines how a field or collections of fields are displayed on the form. As an example is taken ProductVariationAttributesWidget of the Commerce 2.x module.
<?php
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Place this code into YOUR_MODULE.module file replacing YOUR_MODULE placeholder
* with your modules' name.
*/
function YOUR_MODULE_field_widget_commerce_product_variation_attributes_form_alter(&$element, FormStateInterface $form_state, $context) {
// Make ajax responsive changes on attribute fields' properties.
@drugan
drugan / guide.md
Last active August 30, 2019 20:15
Commit the entire Drupal Commerce 2.x project including nested git repositories

First, as an alternative install this version of the Drupal Commerce:

composer create-project --repository=https://raw.githubusercontent.com/drugan/project-base/8.x/packages.json drugan/project-base some-dir --stability dev

If desired, you might not install it as a Drupal site, instead use it as a repository for all other of your projects (local or remote) to pull from. After composer installing open .gitignore file in the root of a repo and replace its content

@drugan
drugan / packages.json
Last active October 8, 2017 05:08
A kinda your own Packagist. Usage: composer create-project --repository=https://raw.githubusercontent.com/USER/PROJECT/BRANCH/packages.json USER/PROJECT YOUR-DIR
{
"packages": {
"drugan/project-base": {
"dev-8.x": {
"name": "drugan/project-base",
"version": "8.4",
"dist": {
"type": "zip",
"url": "https://github.com/drugan/project-base/archive/8.x.zip"
}
@drugan
drugan / Drupal Commerce 2.x: Customize Add toCart message
Last active September 1, 2017 02:37
Change the message seen by a customer after product variation is added to the Shopping Cart.
<?php
/**
* Implements hook_commerce_add_to_cart_message_alter().
*
* Apply this patch: https://www.drupal.org/node/2905624#comment-12242300
* Place this code into YOUR_MODULE.module file replacing YOUR_MODULE placeholder
* with your modules' name.
*/
function YOUR_MODULE_commerce_add_to_cart_message_alter(&$message, &$order, &$order_item) {