Skip to content

Instantly share code, notes, and snippets.

View sumanthkumarc's full-sized avatar

Sumanth Reddy sumanthkumarc

View GitHub Profile
@sumanthkumarc
sumanthkumarc / extension-patterns.md
Created May 25, 2017 10:20 — forked from bojanz/extension-patterns.md
Extension patterns: events, tagged services, plugins

This documentation is destined for drupal.org. Created first as a gist to make initial comments easier. Rewrites and clarifications welcome. Code samples are simplified for clarity. Perhaps a bit too much?

When talking about extensibility, there are several distinct use cases:

  1. Reacting to an action that has already happened.

The reaction can be anything; outputting a message, sending an email, modifying a related object, etc. Examples:

  • "A node has been saved"
  • "A product has been added to the cart".
@sumanthkumarc
sumanthkumarc / routing.yml
Last active May 25, 2017 15:21
Entity id passed as slug, not loaded as entity in routematch Drupal 8
#When a slug passed in route doesn't load entity on getParameter, you need to pass the entity type as options
#in routing.yml so that the entityconverter knows what it is.
#@link https://www.drupal.org/docs/8/api/routing-system/how-a-node-id-gets-converted-to-its-actual-node-object
#in your routing.yml
route_name:
path: /path/{foo}
options:
parameters:
@sumanthkumarc
sumanthkumarc / OrderCurrency.php
Created July 7, 2017 11:53
Drupal 8: An example plugin for conditions API Commerce 2.x
<?php
namespace Drupal\commerce_price\Plugin\Commerce\Condition;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@sumanthkumarc
sumanthkumarc / disable_comments_on_nodes.php
Last active July 14, 2017 11:05
Drupal 7 : Disable commenting on existing nodes of particular content type
<?php
$nodes = node_load_multiple(array(), array('type' => $type));
foreach($nodes as $nid => $node){
// Comment status :
// 2 - open
// 1 - closed
// 0 - hidden
$node->comment = 1;
node_save($node);
@sumanthkumarc
sumanthkumarc / largest_table.sql
Created July 18, 2017 12:02
SQL : Get list of largest tables in your database
SELECT
table_schema AS `Database`,
table_name AS `Table`,
round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB`
FROM information_schema.TABLES WHERE table_schema = 'exampledb'
ORDER BY (data_length + index_length) DESC
LIMIT 10;
@sumanthkumarc
sumanthkumarc / alter_node_form.php
Created September 20, 2017 06:13
Drupal 8: Altering the actions available for a node form, either add or edit.
<?php
function hook_MY_MODULE_form_alter(){
// Altering the actions available for a node form, either add or edit.
$build_info = $form_state->getBuildInfo();
if (isset($build_info['base_form_id']) && $build_info['base_form_id'] == 'node_form') {
$form_object = $form_state->getFormObject();
$entity = $form_object->getEntity();
$bundle = $entity->bundle();
@sumanthkumarc
sumanthkumarc / hook_update_N.php
Created September 20, 2017 11:53
Drupal 8: Create a new table in database using hook_update_N()
<?php
/**
*The numbers are normally composed of three parts:
* - 1 or 2 digits for Drupal core compatibility (Drupal 8, 9, 10, etc.). This
* convention must be followed.
* - 1 digit for your module's major release version; for example, for 8.x-1.*
* use 1, for 8.x-2.* use 2, for Core 8.0.x use 0, and for Core 8.1.x use 1.
* This convention is optional but suggested for clarity.
@sumanthkumarc
sumanthkumarc / check_and_set_schema_version.php
Created September 20, 2017 15:16
Drupal 8: get and set schema version numbers in database.
<?php
// All these are found in core/includes/schema.inc
// Get schema version for your module.
drupal_get_installed_schema_version('MY_MODULE');
// schema version number is N in hook_update_N
drupal_set_installed_schema_version('MY_MODULE', 'Schema_version_number');
// Get all schema versions for all modules.
@sumanthkumarc
sumanthkumarc / eu_country_list.php
Last active June 3, 2023 22:13
EU (European country code and names list) in PHP array format
<?php
function get_eu_countries() {
$countries = [];
$countries['AT'] = ['name' => 'Austria',];
$countries['BE'] = ['name' => 'Belgium',];
$countries['BG'] = ['name' => 'Bulgaria',];
$countries['CY'] = ['name' => 'Cyprus',];
$countries['CZ'] = ['name' => 'Czech Republic',];
$countries['DE'] = ['name' => 'Germany',];
@sumanthkumarc
sumanthkumarc / cluster.yml
Created April 19, 2019 06:20
sample cluster file
nodes:
- address: master.my-domain.com
user: my-user
role:
- controlplane
- etcd
- address: worker1.my-domain.com
user: my-user
role:
- worker