This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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']); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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' => '',), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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. |
OlderNewer