Skip to content

Instantly share code, notes, and snippets.

@blainelang
blainelang / Hide Money Field's fieldset
Created July 16, 2013 19:39
Hide fieldset around the money field. The money field has an amount and currency field and these are wrapped inside a fieldset with a label which is redundant if your using the money field in a field collection and using the field_collection_table module - as I am for the PI expense form
// Add a theme_wrapper definition so thats it now treated like a standard form element
$form['field_DATEFIELDNAME']['und'][0]['#theme_wrappers'] = array('form_element');
@blainelang
blainelang / hook_views_data
Created August 25, 2013 00:09
Example HOOK_views_data for 2 "related" custom tables and includes a relationship back to the users table. http://internetdevels.com/blog/describe-custom-table-for-views
/* Define Views interface for custom data tables */
function as_dashboard_views_data() {
$data = array();
$data['communication_builder_contact_list'] = array(
'table' => array(
'group' => t('AdvisorStream'),
'title' => t('contact list'),
'help' => t('Broker Contact List Names'),
'base' => array(
'field' => 'id',
@blainelang
blainelang / array_walk + anonymous function
Last active February 14, 2021 18:40
Using an anonymous function with array_walk and passing in a variable so that it's in scope for the function to use.
/* Objective: Loop over an array of Drupal user objects and build a string of email addresses for the $to variable.
* At first there was an issue because the variable $to_email_array was not being accessible to the anonymous function
* and then I saw answer2 to a similar question:
* > http://stackoverflow.com/questions/11420520/php-variables-in-anonymous-functions
*/
$to_email_array = array();
array_walk($users_group, function(&$user, &$uid) use (&$to_email_array) {
$to_email_array[] = $user->mail;
});
@blainelang
blainelang / Interacting with tasks
Created August 29, 2013 20:05
Using the maestro API to work with tasks Sample PHP code to interact with the Drupal maestro module (workflow engine) https://drupal.org/project/maestro
$task = MaestroTask::createTaskObject($queue_id);
$task_data = $task->prepareTask();
$notification_type = $task_data['optional_parm']['notification_type'];
// If you need to complete a task, you can use the engine()->completeTask() method
$maestro->engine()->completeTask($queue_id, $status);
@blainelang
blainelang / Maestro API for processes
Created August 29, 2013 20:06
Using the maestro API to launch a new workflow instance, crank the engine and get process detail. Sample PHP code to interact with the Drupal maestro module (workflow engine) https://drupal.org/project/maestro
// Helper function to instantiate maestro engine and step it forward
maestro_orchestrator();
// Alternative method to interact with the maestro engine API
$maestro = Maestro::createMaestroObject(1);
/*
That's it! Wherever you want to run the orchestrator, you will place that code.
If you look in maestro.module, you will find the function maestro_orchestrator.
Inside of it we are trying to acquire a lock based on a defined lock delay (in seconds) configuration parameter.
The lock is essentially a simple lock semaphore. If for some reason the lock is not relinquished in a default of 512 seconds,
@blainelang
blainelang / Array Manipulation
Created September 16, 2013 17:52
Move an array element to the beginning of the array
$implementations = array('pi_commerce' => $implementations['pi_commerce']) + $implementations;
@blainelang
blainelang / Comment Trick
Created October 19, 2013 14:36
This trick below can be used to toggle between two sets of code, with one symbol in the code with no additional short-keys to learn, and no tool dependencies!
//*
Often, in development or debugging, we need to toggle on/off a section of code or want to swap a few lines.
We often comment out those lines but here is a neat trick where we have the comment annotation around two
sections of code - in my case I want to test some code with a value of 100 or 200 but this could be a tw
large sections of code that I want to toggle or swap.
Add this to you editor and just delete the first / and watch as your editor disables the first assignment
and now enables the 2nd assignment. It's the little things at times that amaze you as I sit here
toggling on/off that first / to see my editor's reaction :)
@blainelang
blainelang / Commbuilder template info hook.php
Created February 19, 2014 17:29
Commbuilder template info hook
/**
* Implement hook_theme()
*/
function as_newsletter_template_2014v1_commbuilder_template_info() {
return array(
'theme_set_2014v1' => array(
'default' => array(
'email' => array(
'description' => 'Single column layout tested for Outlook',
'template' => 'theme/email_single_column',
@blainelang
blainelang / maestro launch workflow
Created October 10, 2014 19:00
Code snippet to launch a maestro workflow
/* Code to launch a maestro workflow.
* Pass in the template ID and security token
* Once the workflow instance is launched, it
* returns the new process id
*/
$template = 1; // Set to the maestro template you want to launch
$sec_token = drupal_get_token('maestro_user');
$new_process_id = maestro_launch_workflow($template, $sec_token);
@blainelang
blainelang / guzzletest.php
Last active July 17, 2017 12:35
Drupal 8 standalone PHP script using Guzzle to request a node with a JSON response and HTML response.
<?php
/* Script located in the docroot for your Drupal 8 site */
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once __DIR__ . '/core/vendor/autoload.php';
use GuzzleHttp\Client;