Skip to content

Instantly share code, notes, and snippets.

@kvnm
kvnm / UntoldDrupalValetDriver.php
Created September 7, 2016 02:32
Valet driver for Drupal that allows a custom web directory.
<?php
class UntoldDrupalValetDriver extends DrupalValetDriver {
private function documentRoot($sitePath) {
return $sitePath . '/drupal';
}
public function serves($sitePath, $siteName, $uri) {
return parent::serves($this->documentRoot($sitePath), $siteName, $uri);
}
@kvnm
kvnm / custom_workbench.module
Created May 30, 2016 21:32
Restrict IMCE directory access based on a taxonomy term value associated with workbench_access taxonomy access control. DIRECTORY_FIELD constant is a new text field on the term entity used for control.
/**
* Hijack IMCE Profile Loading to add a check for directory access coming from
* a field on the access vocabulary term.
*
* @param $imce
* @param $user
*/
function custom_workbench_imce_alter_profiles(&$imce, $user) {
// Find access rules/terms for current user.
$access_tree = workbench_access_get_user_tree($user);
@kvnm
kvnm / custom_workbench.module
Created May 30, 2016 21:29
Retroactively bulk assign all existing nodes with taxonomy terms to their respective workbench_access sections. Useful if you've got a workbench_access scheme based on taxonomy terms that have existed for some time before launching access control.
/**
* Retroactively bulk assigns all existing nodes w/ taxonomy terms
* to their respective workbench_access sections.
*/
function custom_workbench_retroactive_assign_all() {
$active = workbench_access_get_active_tree();
if (!$active) {
return;
}
$tree = $active['tree'];
@kvnm
kvnm / custom.module
Created May 30, 2016 21:24
Alter Drupal pathauto settings on the fly using custom variable values.
/**
* Implements hook_pathauto_pattern_alter().
* Hack up blog paths to use custom variable assignment.
*
* @param $pattern
* @param array $context
*/
function custom_pathauto_pattern_alter(&$pattern, array $context) {
switch ($context['op']) {
case 'update':
@kvnm
kvnm / custom.module
Last active August 29, 2015 14:02
Drupal snippet to validate whether a room (defined by a taxonomy term) has already been booked during the dates selected on a node form.
<?php
/**
* Implements hook_form_alter().
*
* Adds validation to Booking node forms.
*/
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'booking_node_form') {
$form['#validate'][] = 'custom_booking_validate';
@kvnm
kvnm / custom.module
Last active August 29, 2015 13:58
Get a custom list of all Drupal terms on an entity. Useful when you have several term reference fields on a single entity, and want to display them all together. _get_entity_terms() written by Drupal user HydroZ.
<?php
/**
* Example: get all terms attached to a given taxonomy term.
*
* Can be done with any entity.
*/
function custom_example($term) {
$related_term_links = array();
$related_tids = _get_entity_terms('taxonomy_term', $term->vocabulary_machine_name, $term);
@kvnm
kvnm / custom.module
Created April 3, 2014 20:14
Find the last time drupal content of a particular content type or list of content types was updated (without views). Sometimes people like this kind of thing, apparently.
<?php
/**
* Returns a single "Last Updated" date.
*
* $types -- an array of content type machine names. Defaults to 'page'.
*/
function custom_last_updated($types = array('page')) {
$query = db_select('node', 'n');
@kvnm
kvnm / advanced_settings_per_profile
Created February 21, 2014 15:48
Drupal CKEditor module 'Advanced Settings' to get Media Embed plugin to play nice with CKEditor 4
config.allowedContent = true;
config.extraAllowedContent = 'iframe[*]';
@kvnm
kvnm / template.php
Created February 14, 2014 20:35
Return an array of Drupal page alias arguments.
<?php
$alias = explode('/', drupal_get_path_alias(current_path()));
@kvnm
kvnm / custom.module
Last active August 29, 2015 13:56
Change Drupal 7 Taxonomy term links to a custom path (leading to a custom view, page, whatever).
<?php
/**
* Implements hook_entity_info_alter().
*/
function custom_entity_info_alter(&$entity_info) {
$entity_info['taxonomy_term']['uri callback'] = 'custom_taxonomy_term_uri';
}
/**