Skip to content

Instantly share code, notes, and snippets.

View devudit's full-sized avatar
🎓
Focusing

Udit Rawat devudit

🎓
Focusing
View GitHub Profile
@devudit
devudit / isset.js
Last active February 8, 2024 13:24
JavaScript isset() equivalent
// I generally use the typeof operator:
if (typeof obj.foo !== 'undefined') {
// your code here
}
// It will return "undefined" either if the property doesn't exist or its value is undefined.
// There are other ways to figure out if a property exists on an object, like the hasOwnProperty method:
@devudit
devudit / closest-lower-higher-number.php
Last active January 6, 2024 18:22
Get closest, lower and higher number as a associative array from a array
<?php
function closestLowerHigherNr($array, $nr) {
sort($array);
$re_arr = array('lower'=>min(current($array), $nr), 'higher'=>max(end($array), $nr), 'closest'=>$nr);
foreach($array AS $num){
if($nr > $num) $re_arr['lower'] = $num;
else if($nr <= $num){
$re_arr['higher'] = $num;
break;
}
@devudit
devudit / drupal.dialog.js
Created November 27, 2017 12:32
Create modal window in drupal 8 using Drupal.dialog function
/** EXAMPLE 1 ***/
var $myDialog = $('<div>My dialog text</div>').appendTo('body');
Drupal.dialog($myDialog, {
title: 'A title',
buttons: [{
text: 'Close',
click: function() {
$(this).dialog('close');
}
}]
@devudit
devudit / vocabulary.php
Last active April 14, 2023 15:39
Create vocabulary and term programmatically in drupal 8 Assing vocabulary to a referenced field storage automatically
<?php
/** If vocabulary id not exist */
if(!\Drupal\taxonomy\Entity\Vocabulary::load($vocab_id)){
/**
* Create vocabulary
* @var $vocabulary
*/
$vocabulary = \Drupal\taxonomy\Entity\Vocabulary::create([
'vid' => $vocab_id,
@devudit
devudit / SftpFetcher.php
Created August 12, 2022 13:38
Feeds Fetcher — Drupal
<?php
namespace Drupal\feeds_sftp_fetcher\Feeds\Fetcher;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Token;
use Drupal\feeds\Exception\EmptyFeedException;
use Drupal\feeds\FeedInterface;
@devudit
devudit / XML Country List
Created February 6, 2020 16:47 — forked from nathanhornby/XML Country List
XML Country list, with country code, ISO number, continent, handle and alternative spelling attributes.
<?xml version='1.0' encoding='UTF-8'?>
<countries>
<country code='af' handle='afghanistan' continent='asia' iso='4'>Afghanistan</country>
<country code='al' handle='albania' continent='europe' iso='8'>Albania</country>
<country code='dz' handle='algeria' continent='africa' iso='12'>Algeria</country>
<country code='as' handle='american-samoa' continent='polynesia' iso='16'>American Samoa</country>
<country code='ad' handle='andorra' continent='europe' iso='20'>Andorra</country>
<country code='ao' handle='angola' continent='africa' iso='24'>Angola</country>
<country code='ai' handle='anguilla' continent='north america' iso='660'>Anguilla</country>
<country code='aq' handle='antarctica' continent='antarctica' iso='10'>Antarctica</country>
@devudit
devudit / get-image-by-tag.js
Created July 7, 2016 09:56
Get Instagram image data using javascript/jquery
var token = '<access_token>',
hashtag='<tag>', // hashtag without # symbol
num_photos = 4;
$.ajax({
url: 'https://api.instagram.com/v1/tags/' + hashtag + '/media/recent',
dataType: 'jsonp',
type: 'GET',
data: {access_token: token, count: num_photos},
success: function(data){
@devudit
devudit / center-bootstrap-modal-script.js
Last active May 2, 2018 10:23
Center bootstrap modal, Vertically and Horizontally
(function ($) {
"use strict";
function $fixBootstrapModalPosition() {
$(this).css('display', 'block');
var $dialog = $(this).find(".modal-dialog"),
offset = ($(window).height() - $dialog.height()) / 2,
bottomMargin = parseInt($dialog.css('marginBottom'), 10);
// Make sure you don't hide the top part of the modal w/ a negative margin if it's longer than the screen height, and keep the margin equal to the bottom margin of the modal
if(offset < bottomMargin) offset = bottomMargin;
@devudit
devudit / taxonomy-term.php
Created November 29, 2017 10:16
Find tid by name in drupal 8
<?php
/**
* Utility: find term by name and vid.
* @param null $name
* Term name
* @param null $vid
* Term vid
* @return int
* Term id or 0 if none.
@devudit
devudit / index.html
Last active December 3, 2017 18:25
Run a javascript function after user has stopped typing
<input type='text' id='text'>