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. |
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 | |
/** | |
* 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 |
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 | |
/** | |
* 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. |