Skip to content

Instantly share code, notes, and snippets.

@Greg-Boggs
Greg-Boggs / gist:9416376
Created March 7, 2014 17:53
Save the current page as a reference on node being saved. This is useful with the formblock module.
<?php
function nike_ugc_node_presave($node) {
$journal_wrapper = entity_metadata_wrapper('node', $node);
// If this is a new journal entry set it's course reference.
if ($journal_wrapper->getBundle() == 'journal_entry') {
// If this is the first time the journal is saved, attach the course.
if (!$journal_wrapper->getIdentifier() && arg(0) == 'node' && is_numeric(arg(1))) {
@Greg-Boggs
Greg-Boggs / gist:9531836
Last active August 29, 2015 13:57
Add class and link to a teaser of a node
<?php
/**
* Render promo
*/
function nike_promo_preprocess_node(&$vars, $hook) {
$class = '';
$style = '';
$node = $vars['node'];
Losslessly compressing http://www.thenewgroup.com/…/portfolio-tile-images/_0012_samsung.jpg could save 98KiB (87% reduction).
Losslessly compressing http://www.thenewgroup.com/…s/portfolio-tile-images/sh_tile.jpg.jpeg could save 56.8KiB (63% reduction).
Losslessly compressing http://www.thenewgroup.com/…files/portfolio-tile-images/wcg_tile.jpg could save 54.8KiB (71% reduction).
Losslessly compressing http://www.thenewgroup.com/…tfolio-tile-images/_0006_r2-SR Smith.jpg could save 46.6KiB (63% reduction).
Losslessly compressing http://www.thenewgroup.com/…folio-tile-images/_0005_r2-Grammy365.jpg could save 45.7KiB (65% reduction).
Losslessly compressing http://www.thenewgroup.com/…olio-tile-images/_0008_r3-Jenny SM_0.jpg could save 43KiB (72% reduction).
Losslessly compressing http://www.thenewgroup.com/…efault/files/client-logos/jennycraig.png could save 4.1KiB (72% reduction).
Losslessly compressing http://www.thenewgroup.com/…tion-statement-images/homepage_wide2.jpg could save 3.1KiB (3% reduction).
Lossles
@Greg-Boggs
Greg-Boggs / gist:9629778
Created March 18, 2014 21:13
Sample Beehat Test
Feature: Basic anonymous site functionality
In order to prove that this site has been setup successfully
As an anonymous user
I need to see a home page
Background:
Given I am an anonymous user
Scenario: Anonymous user can see the home page
When I visit "/"
@Greg-Boggs
Greg-Boggs / gist:9629880
Created March 18, 2014 21:18
Example Drupal Unit Test
<?php
/**
* Utility function to create fields on a content type.
*
* @param string $field_name
* Name of the field, like field_something
* @param string $widget_type
* Widget type, like field_example_3text
* @param int $cardinality
* Cardinality
@Greg-Boggs
Greg-Boggs / gist:9647829
Created March 19, 2014 18:13
Goodies to modify submitted text, add a link class, node-nid and css styles from a taxonomy reference drop down.
<?php
function stargazer_preprocess_node(&$vars) {
$class = '';
$style = '';
$node = $vars['node'];
// Modify submitted by text
$vars['submitted'] = t('!datetime by !username',
array(
@Greg-Boggs
Greg-Boggs / gist:9652888
Created March 19, 2014 22:35
Change the view mode based on some logic
<?php
/**
* Implements hook_entity_view_mode_alter().
*/
function HOOK_entity_view_mode_alter(&$view_mode, $context) {
// If a formula teaser is displayed, check if multi-display and update view mode.
if ($view_mode == 'formula_teaser') {
$nid = $context['entity']->nid;
if(in_array($nid, $GLOBALS['multi_display'])) {
$view_mode = 'formula_list';
<?php
function web2theme_preprocess_user_profile(&$variables) {
if( arg(2)==null ) { //im not editing
$qty_field=count($variables['user_profile']['field_telefone']['#items']);
for($i=0 ; $i<$qty_field; $i++) {
$field_collection_id = $variables['field_telefone'][$i]['value'];
//dsm($variables['user_profile']['field_telefone'][$i]['entity']['field_collection_item'][$field_collection_id]['field_telefone_showed']);
$toshow = $variables['user_profile']['field_telefone'][$i]['entity']['field_collection_item'][$field_collection_id]['field_telefone_showed'];
@Greg-Boggs
Greg-Boggs / gist:9653350
Created March 19, 2014 23:02
useful with formblock to save the parent node to the created node's reference field
<?php
function HOOK_ugc_node_presave($node) {
$journal_wrapper = entity_metadata_wrapper('node', $node);
// If this is a new journal entry set it's parent reference.
if ($journal_wrapper->getBundle() == 'journal_entry') {
// If this is the first time the journal is saved, attach the course.
if (!$journal_wrapper->getIdentifier() && arg(0) == 'node' && is_numeric(arg(1))) {
$course_wrapper = entity_metadata_wrapper('node', arg(1));
@Greg-Boggs
Greg-Boggs / gist:9697717
Created March 21, 2014 22:23
Example of form alters
<?php
/*
* Implements of hook_form_alter().
*/
function hitachi_module_form_alter(&$form, &$form_state, $form_id) {
$domain = domain_get_domain();
switch ($form_id) {
case 'user_register_form';
$path = check_plain($_GET['destination']);