Skip to content

Instantly share code, notes, and snippets.

@Greg-Boggs
Greg-Boggs / gist:8869659
Created February 7, 2014 19:10
Group Drupal CSS
<?php
/**
* Implements hook_css_alter().
*/
function mymodule_css_alter(&$css) {
uasort($css, 'drupal_sort_css_js');
$i = 0;
foreach ($css as $name => $style) {
$css[$name]['weight'] = $i++;
@Greg-Boggs
Greg-Boggs / gist:9096695
Created February 19, 2014 17:16
// Add the linked node title just after the company info.
<?php
$vars['content']['title'] = array(
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => l(decode_entities($vars['title']), $base_url . $vars['node_url']),
'#attributes' => $vars['title_attributes_array'],
'#weight' => $vars['content']['field_company']['#weight'] + .1,
);
?>
@Greg-Boggs
Greg-Boggs / gist:9096857
Created February 19, 2014 17:24
Add class to a view mode in a preprocess
<?php
if ($vars['view_mode'] != 'teaser') {
$view_mode = str_replace('_', '-', $vars['view_mode']);
$vars['attributes_array']['class'][] = 'node-' . $view_mode;
}
?>
@Greg-Boggs
Greg-Boggs / gist:9098754
Created February 19, 2014 18:46
Inline jquery in Drupal 7 dropped into a template.php
<?php
drupal_add_js('(function($) {$(document).ready(function() {
$("body").click(function() {
alert("Hello World");
});
});}(jQuery));', 'inline');
?>
<?php
/**
* Render promo
*/
function promo_preprocess_node(&$vars, $hook) {
$vars['custom_share_title'] = '';
$vars['custom_promo'] ='';
$class = '';
$style = '';
$node = $vars['node'];
@Greg-Boggs
Greg-Boggs / node.tpl.php
Last active August 29, 2015 13:56
Modify submitted by text in two different ways. The better way is in the preprocess function below.
<span class="submitted"><?php print $submitted?></span>
<?php if ($submitted) { ?>
<span class="submitted">
<?php
print 'by ' . theme('username', $node) . ' | ' . format_date($node->created, 'custom', 'd/m/Y');
if ($terms) {
print ' | in ';
}
?>
@Greg-Boggs
Greg-Boggs / gist:9260408
Last active August 29, 2015 13:56
Must refactor this
<?php
/* This code created an alphabetized index, when I combined them, I created a mess.
foreach ($build as $key => $value) {
$companies[$value['#markup'][0]][] = $value['#markup'];
}
*/
asort($build);
foreach ($build as $key => $value) {
$markup = strtoupper($value['#markup'][0]);
@Greg-Boggs
Greg-Boggs / gist:9275906
Last active August 29, 2015 13:56
Using jQuery with Drupal 7
(function($) {
Drupal.behaviors.myBehavior = {
attach: function (context, settings) {
$("body").click(function() {
alert("Hello World");
});
}
};
})(jQuery);
@Greg-Boggs
Greg-Boggs / gist:9357106
Created March 4, 2014 22:27
Programmatically set a Drupal view argument based on the field of the current user.
/**
* @implements hook_views_pre_view().
* @param $view
* @param $display_id
* @param $args
*
* Programmatically set a Drupal view argument based on the field of the current user.
*/
function tng_weekly_view_views_pre_view(&$view, &$display_id, &$args) {
@Greg-Boggs
Greg-Boggs / gist:9395900
Created March 6, 2014 18:11
The code to change the quantity label in a Drupal Commerce product
* Implements hook_form_alter().
*
* Modify quantity text
*/
function HOOKNAME_form_alter(&$form, &$form_state, $form_id) {
if (strstr($form_id, 'commerce_cart_add_to_cart_form')) {
// Rename add-to-cart form fields label (quantity).
$form['quantity']['#title'] = t('Quantity');
}