Skip to content

Instantly share code, notes, and snippets.

View msankhala's full-sized avatar
🎯
Focusing

Mahesh Sankhala msankhala

🎯
Focusing
View GitHub Profile
@msankhala
msankhala / useScript-react-hooo.js
Created February 2, 2022 00:48
React hook useScript
// hooks/useScript.js
import { useEffect } from 'react';
const useScript = url => {
useEffect(() => {
const script = document.createElement('script');
script.src = url;
script.async = true;
@msankhala
msankhala / prototype-design-pattern.js
Created December 19, 2021 08:16
Prototype design pattern javascript
const atv = {
make: 'Honda',
model: 'Rincon 650',
year: 2018,
mud: () => {
console.log('Mudding');
}
};
const secondATV = Object.create(atv);
@msankhala
msankhala / builder-design-pattern-js.js
Created December 19, 2021 08:15
Builder design pattern javascript
class Car {
constructor(make, model, year, isForSale = true, isInStock = false) {
this.make = make;
this.model = model;
this.year = year;
this.isForSale = isForSale;
this.isInStock = isInStock;
}
toString() {
@msankhala
msankhala / factory-design-pattern.js
Last active December 19, 2021 08:16
Factor pattern js pattern
import Motorvehicle from './Motorvehicle';
import Aircraft from './Aircraf';
import Railvehicle from './Railvehicle';
const VehicleFactory = (type, make, model, year) => {
if (type === car) {
return new Motorvehicle('car', make, model, year);
} else if (type === airplane) {
return new Aircraft('airplane', make, model, year);
} else if (type === helicopter) {
// The 4 Creational Design Patterns In Node.js
// https://daily.dev/blog/the-4-creational-design-patterns-in-node-js-you-should-know
// The Singletone Pattern
class DatabaseConnection {
constructor() {
this.databaseConnection = 'dummytext';
}
@msankhala
msankhala / CustomSerializer.php
Created June 25, 2021 11:50 — forked from berramou/CustomSerializer.php
Drupal 8 : Custom serializer example to render result as array instead of objects.
<?php
namespace Drupal\MY_MODULE\Plugin\views\style;
use Drupal\rest\Plugin\views\style\Serializer;
/**
* The style plugin for serialized output formats.
*
* @ingroup views_style_plugins
*
@msankhala
msankhala / custom-form-mode-to-entities.php
Created June 21, 2021 18:32
custom mode form to entities
<?php
// Source https://www.flocondetoile.fr/blog/provide-custom-mode-form-entities-drupal-8
/**
* Implements hook_entity_type_build().
*/
function my_module_entity_type_build(array &$entity_types) {
$entity_types['user']->setFormClass('profil', 'Drupal\user\ProfileForm');
}
@msankhala
msankhala / custom-entity-display-form-mode.php
Last active June 21, 2021 18:29
Creating custom Entity Display form mode
<?php
// Source https://www.droptica.com/blog/custom-entity-forms-example-user-editing-and-registration/
/**
* Implements hook_entity_type_build().
*/
function my_module_entity_type_build(array &$entity_types) {
$form_modes = ['custom_form_mode_1', 'custom_form_mode_2'];
@msankhala
msankhala / TopicsSelection.php
Created June 20, 2021 06:29 — forked from hussainweb/TopicsSelection.php
/web/modules/custom/axl_ks_topics/src/Plugin/EntityReferenceSelection/TopicsSelection.php
<?php
namespace Drupal\axl_ks_topics\Plugin\EntityReferenceSelection;
use Drupal\Component\Utility\Html;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@msankhala
msankhala / ComboForm.php
Created June 20, 2021 05:02 — forked from Eyal-Shalev/ComboForm.php
An example form object (Drupal 8) that combines multiple forms into itself.
<?php
namespace Drupal\sandbox\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;