Skip to content

Instantly share code, notes, and snippets.

View lhuria94's full-sized avatar
🏠
Working from home

Love Huria lhuria94

🏠
Working from home
View GitHub Profile
@lhuria94
lhuria94 / remove_image_markup.php
Last active February 17, 2017 06:04
Remove image object markup from rendering in Drupal 7 (Helpful for teaser display)
<?php
$body_text_alter_params = array(
'max_length' => 300,
'ellipsis' => FALSE,
'word_boundary' => TRUE,
'html' => FALSE,
);
// Removes unnecessary markup.
$trimmed_body = check_plain(strip_tags($value->body_value, '<p>'));
// Trimming the body text as per the parameters defined in
<?php
$panelizer_nodes = db_select('node', 'n');
$panelizer_nodes->leftjoin('panelizer_entity', 'e', 'e.entity_id = n.nid');
$panelizer_nodes->fields('e', array('entity_id'));
$panelizer_nodes->condition('entity_type', 'node');
$panelizer_nodes->condition('did', 0, '<>');
$panelizer_nodes->condition('n.status ', 1, '=');
$panelizer_nodes->condition('n.type', 'content_type');
$result = $panelizer_nodes->groupBy('e.entity_id')->execute()->fetchCol('entity_id');
@lhuria94
lhuria94 / entity-metadata-wrapper-isset.php
Created February 17, 2017 06:11 — forked from msankhala/entity-metadata-wrapper-isset.php
entity-metadata-wrapper isset check.
<?php
$field_collection_item = field_collection_item_load($id);
$item_wrapper = entity_metadata_wrapper('field_collection_item', $field_collection_item);
// this results in an error if the field_contrib_headshot field is empty
$headshot = $item_wrapper->field_contributor->field_contrib_headshot->value();
Solution:
// EntityStructureWrapper implements the __isset() function according the principe of Overloading.
$headshot = array();
<?php
/**
* Implements hook_element_info_alter().
*
* Sets the text format processor to a custom callback function.
* This code is taken from the Better Formats module.
*/
function module_element_info_alter(&$type) {
if (isset($type['text_format']['#process'])) {
foreach ($type['text_format']['#process'] as &$callback) {
@lhuria94
lhuria94 / git-restore-file.sh
Created September 22, 2017 06:08 — forked from msankhala/git-restore-file.sh
Find and restore a deleted file in a Git
# Find last commit for the deleted file
git rev-list -n 1 HEAD -- $path
# Checkout the commit before the the delete happened
git checkout $commit^ -- $path
@lhuria94
lhuria94 / better_tabledrag.php
Created November 22, 2017 10:59
Drupal table drag example in form An example of using a Drupal tabledrag table without the need of creating a theme function for the complete form. This makes it a lot easier to build a form which contains multiple form elements/fieldsets.
<?php
/**
* @file
* A tabledrag example - without theming the whole form.
*/
/**
* Implements hook_menu().
*/
@lhuria94
lhuria94 / xbonacci.js
Created January 2, 2018 13:10
Build a Xbonacci function that takes a signature of X elements - and remember each next element is the sum of the last X elements - and returns the first n elements of the so seeded sequence.
// This solves the problem with last and previous digit addition.
function Xbonacci(signature, n){
var totalCallsToMake = n - signature.length;
var tempArr = signature.slice(0);
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// Looping through.
for (var i=1; i<=totalCallsToMake; i++) {
var lastTwoEl = tempArr.slice(-2);
tempArr.push(lastTwoEl.reduce(reducer));
}
@lhuria94
lhuria94 / middle.js
Created January 2, 2018 13:12
As a part of this Kata, you need to create a function that when provided with a triplet, returns the index of the numerical element that lies between the other two elements.
var gimme = function (inputArray) {
// Clone the input array.
var clonedArray = inputArray.slice(0);
// Sort it in ascending order.
clonedArray.sort(function(a, b){
return a-b
});
return inputArray.indexOf(clonedArray[1]);
};
@lhuria94
lhuria94 / vowel.js
Created January 2, 2018 13:13
For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".
function disemvowel(str) {
return str.replace(/[aeiou]/gi, '');
}
@lhuria94
lhuria94 / maskify.js
Created January 2, 2018 13:14
Usually when you buy something, you're asked whether your credit card number, phone number or answer to your most secret question is still correct. However, since someone could look over your shoulder, you don't want that shown on your screen. Instead, we mask it.
// Your task is to write a function maskify, which changes all but the last four characters into '#'.
// Examples
// maskify("4556364607935616") == "############5616"
// maskify( "64607935616") == "#######5616"
// maskify( "1") == "1"
// maskify( "") == ""
// "What was the name of your first pet?"
// maskify("Skippy") == "##ippy"