Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save knibals/2414571 to your computer and use it in GitHub Desktop.
Save knibals/2414571 to your computer and use it in GitHub Desktop.
Drupal snippets
<?php
// Proper way to get the value of a field i18n safe.
if ($field = field_get_items('node', $variables['node'], 'field_my_field')) {
$variables['custom_var'] = $field[0]['value'];
}
<?php
/**
* Implements hook_entity_info_alter().
*
* Add new view modes to file entities.
*/
function HOOK_entity_info_alter(&$entity_info) {
$entity_info['file']['view modes']['media_slideshow'] = array(
'label' => t('Slideshow'),
'custom settings' => TRUE,
);
$entity_info['file']['view modes']['media_colorbox'] = array(
'label' => t('Colorbox'),
'custom settings' => TRUE,
);
$entity_info['file']['view modes']['media_wide'] = array(
'label' => t('Wide'),
'custom settings' => TRUE,
);
$entity_info['file']['view modes']['media_gallery'] = array(
'label' => t('Gallery'),
'custom settings' => TRUE,
);
}
<?php
/**
* Add a stylesheet to the admin theme
*/
function HOOK_preprocess_page(&$variables) {
global $theme_info;
// If the page load is for the admin theme
if ($theme_info->name == variable_get('admin_theme')) {
drupal_add_css(drupal_get_path('theme', 'MYTHEME') . '/stylesheets/seven.css', array(
'group' => CSS_THEME,
));
}
}
<?php
/**
* Implements hook_page_alter().
*
* Hides the administration toolbar if the administration menu is available.
*/
function HOOK_page_alter(&$page) {
// Hide Toolbar for those who can access the admin menu.
if (user_access('access administration menu') && !empty($page['page_top']['toolbar'])) {
$page['page_top']['toolbar']['#access'] = FALSE;
}
}
<?php
/**
* Implements hook_css_alter().
*
* Remove core stylesheets
*/
function HOOK_css_alter(&$css) {
$exclude = array(
#'misc/vertical-tabs.css' => FALSE,
#'modules/aggregator/aggregator.css' => FALSE,
#'modules/block/block.css' => FALSE,
#'modules/book/book.css' => FALSE,
#'modules/comment/comment.css' => FALSE,
#'modules/dblog/dblog.css' => FALSE,
#'modules/file/file.css' => FALSE,
#'modules/filter/filter.css' => FALSE,
#'modules/forum/forum.css' => FALSE,
#'modules/help/help.css' => FALSE,
#'modules/menu/menu.css' => FALSE,
#'modules/node/node.css' => FALSE,
#'modules/openid/openid.css' => FALSE,
#'modules/poll/poll.css' => FALSE,
#'modules/profile/profile.css' => FALSE,
#'modules/search/search.css' => FALSE,
#'modules/statistics/statistics.css' => FALSE,
#'modules/syslog/syslog.css' => FALSE,
#'modules/system/admin.css' => FALSE,
#'modules/system/maintenance.css' => FALSE,
#'modules/system/system.admin.css' => FALSE,
'modules/system/system.base.css' => FALSE,
#'modules/system/system.maintenance.css' => FALSE,
'modules/system/system.menus.css' => FALSE,
'modules/system/system.messages.css' => FALSE,
'modules/system/system.theme.css' => FALSE,
#'modules/taxonomy/taxonomy.css' => FALSE,
#'modules/tracker/tracker.css' => FALSE,
#'modules/update/update.css' => FALSE,
#'modules/user/user.css' => FALSE,
);
$css = array_diff_key($css, $exclude);
}
<?php
/**
* Implements hook_form_alter().
*
* Redirects user logins to the front page.
*/
function HOOK_form_user_login_alter(&$form, &$form_state) {
$form['#action'] = url('user', array('query' => array('destination' => '<front>')));
}
<?php
/**
* Implements hook_js_alter().
*
* Remove core js files
*/
function HOOK_js_alter(&$javascript) {
$exclude = array(
'misc/textarea.js' => FALSE,
);
$javascript = array_diff_key($javascript, $exclude);
}
<?php
// Encode email address with invisimail module
$email = variable_get('site_mail', '');
if (module_exists('invisimail')) {
$email = invisimail_encode_email($email, 'html_entities', array('link' => TRUE));
}
<?php
/**
* Implements hook_form_alter().
*
* Allow for 128 length block titles.
* You also need to edit the SQL table `blocks` column `title` to VARCHAR( 128 ).
*/
function HOOK_form_alter(&$form, &$form_state, $form_id) {
// Allow long titles
if (isset($form['module']['#value']) && $form['module']['#value'] == "block") {
$form['settings']['title']['#maxlength'] = 128;
}
}
<?php
function THEME_preprocess_node(&$variables) {
// Add theme suggestion for teasers
if ($variables['teaser']) {
$variables['theme_hook_suggestions'][] = 'node__teaser';
}
// Add content type specific suggestions for teasers
if ($variables['teaser'] && $variables['type']){
$variables['theme_hook_suggestions'][] = 'node__'. $variables['type'] .'__teaser';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment