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 / 0_reuse_code.js
Created June 2, 2014 07:06
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
<?php
/**
* Theme_menu_tree doesn't provide any context information
* THIS SUCKS
* But you can use hook_block_view_alter to change the theme wrapper
* OUF!
*/
function MYTHEME_menu_tree(&$variables) {
<?php
function themename_menu_link(&$variables) {
$element = $variables['element'];
$sub_menu = '';
$element['#attributes']['data-menu-parent'] = $element['#original_link']['menu_name'] . '-' . $element['#original_link']['depth'];
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
@msankhala
msankhala / Drupal cache.php
Created July 30, 2014 07:55
When values don’t change between requests (unlike static variables), Drupal’s caching system is a great, simple way to reduce the workload on your site and provide a flexible way to configure the storage duration of this data. Using just our handy cache_get() and cache_set() functions, we can implement an elegant solution to increasing performan…
//First, let’s analyze the cache_set() parameters -
<?php
function cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) {
/* ... */
}
?>
//Example #1 (using a simple cache)
<?php
@msankhala
msankhala / Drupal storing object in db.php
Created July 30, 2014 07:55
Storing object in database
//Both of the examples use the following database schema -
<?php
// use the following table schema
$schema['my_table'] = array(
'description' => 'My table.',
'fields' => array(
'id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE,),
'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '',),
'options' => array('type' => 'text', 'serialize' => TRUE, 'default' => '',),
@msankhala
msankhala / custom module with export.php
Created July 30, 2014 07:55
Continue with Drupal menu system.php
//Example: Exportables
//Here’s a simple example illustrating collecting exportables using aggregate hooks and static variables -
<?php
function my_module_data_get_all() {
$data = &drupal_static(__FUNCTION__);
// check for existing $data from previous function call
if (!isset($data)) {
// pull all $data from database
@msankhala
msankhala / Drupal menu system.php
Created July 30, 2014 07:55
The menu system in Drupal is known to most people as “that thing in the cache I have to constantly clear to find my links!” That is true. Also, it’s a vastly powerful framework for, not just managing your URLs, but adding context and power around the URLs themselves.
//Load functions
//The following menu item receives a single argument to the URL, an ID for some data -
<?php
$item['my-module/%my_module_data'] = array(
'title' => 'My path',
'page callback' => 'my_module_path',
'page arguments' => array(1),
);
@msankhala
msankhala / Drupal static variable.php
Created July 30, 2014 07:56
Often you’ll create some utility functions that are called incessantly to only return the same result every time during a request. Although not a feature of Drupal itself, harnessing the power of PHP static variables can do for your code’s performance.
//Example #1 (simple, PHP method)
<?php
function i_get_called_way_too_much() {
static $static_var;
if (!isset($static_var)) {
// generate contents of static variable
$static_var = 'some value';
}
return $static_var;
@msankhala
msankhala / Drupal Design Patterns.php
Created July 30, 2014 07:56
Drupal’s hook system allows for modules to interact and alter the workings of other modules or even Drupal core itself. It is a very simple system that allows modules to even create their own very easily. In common practice, there are two types of hooks that you would want to create - alter hooks and intercepting hooks. Alter hooks provide a com…
// Example #1 (simple invoking)
<?php
// will call all modules implementing hook_hook_name
module_invoke_all('hook_name');
?>
//Example #2 (aggregate results)
<?php
$result = array();
@msankhala
msankhala / Entity metadata wrapper.php
Created July 30, 2014 08:08
The Entity API provides wrapper classes you may use to make dealing with the values of an entity's properties and fields easier. Wrappers make it easier to get and set the values of fields and properties as well as to programmatically retrieve additional information about these elements and iterate over lists of values in a consistent manner. Fo…
<?php
$node->field_number[LANGUAGE_NONE][0]['value']
?>
//Using metadata wrappers from the entity module we can access this information like so:
<?php
$node_wrapper->field_number->value();
?>
// How about an example of making things consistent? All Drupal entities have a label of some sort. A string that can be treated as the canonical human readable name of an entity. All nodes have a title property and all user accounts have a name property. Given a standard Drupal entity it can be hard to know which property should be treated as the label. Metadata wrappers provide us with a consistent way of getting at this kind of information for any entity.