Skip to content

Instantly share code, notes, and snippets.

View msankhala's full-sized avatar
🎯
Focusing

mahesh sankhala msankhala

🎯
Focusing
View GitHub Profile
<?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.
@msankhala
msankhala / hook_views_data_alter.php
Created August 3, 2014 06:57
Hook_views_field_data_alter(). implement this hook if you want to modify the handler for any field provided by field api. Otherwise use Hook_views_data_() if you want to expose new table to view.
<?php
/**
* Implements hook_field_views_data_alter().
*/
function MYMODULE_field_views_data_alter(&$result, $field, $module) {
if ($field['field_name'] == 'FIELD_NAME') {
$name = $field['field_name'];
foreach ($result as $table_name => $table_data) {
if (isset($table_data[$field['field_name']]['field'])) {
// HERE modifying filter handler. But we can modify sort, field, contextual filter and argument handler in same way
@msankhala
msankhala / combine exposed filter.php
Created August 3, 2014 07:00
Chose the exposed filter from a list of dropdown exposed filter and search within that selected filter with the search term entered in the same textfield. https://www.dropbox.com/s/jgenr05lo8r3173/Voila_Capture%202014-08-01_11-01-15_pm.png 1) Create a view with title and body field. 2) Create two filter with title and body with operator 'Contain…
<?php
/**
* Implements hook_FORM_ID_alter().
*/
function custom_form_views_exposed_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'VIEWS_NAME') {
// Unset the title and body filter info so that it doesn't print any label.
unset($form['#info']['filter-title']);
unset($form['#info']['filter-body_value']);
// Set the #printed = TRUE on title and body render element to escape them from being print.