Bart's Snippets

Search API Views Query Alter Example

/** 
 * Implements hook_search_api_views_query_alter().
 */
function cga_publications_search_api_views_query_alter(view &$view, SearchApiViewsQuery &$query) {
  if (!empty($view->exposed_raw_input['field_date'])) {
    $or = $query->createFilter('AND');                           
    $start = new DateTime($view->exposed_raw_input['field_date'] . "-01-01");                                                                     
    $start_date = $start->format('Y-m-d H:i:s');                                                                                                  
    $end = new DateTime($view->exposed_raw_input['field_date'] . '-12-31');                                                                       
    $end_date = $end->format('Y-m-d H:i:s');                                                                                                      
    $or->condition('field_date', $start_date, '>=');                                                                                              
    $or->condition('field_date', $end_date, '<=');                                                                                                
    $query->filter($or);                                                                                                                          
  }                                                                                                                                               
} 

Entity Field Query example

$b = new EntityFieldQuery();
$b->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'bill')
  ->propertyCondition('status', NODE_PUBLISHED)
  ->fieldCondition('field_sessions', 'target_id', 10171);
$result = $b->execute();

dpm($result);

Drush command to run queues

# time limit is in seconds
drush @cga.prod queue-run --time-limit=30 node_bills_queue

check if field_date exists in node object before accessing it in entity metadata wrapper

fd.php
// check if field_date exists in node object before accessing it in entity metadata wrapper
if (!empty($vars['content']['field_date'])) {
  $wrapper = entity_metadata_wrapper('node', $vars['node']);
  $timestamp = $wrapper->field_date->value();
  $vars['last_action'][] = date('m/j/Y', $timestamp);
}

// Check if a field_date isset in an object
if(isset($meeting->field_date)) {
  $meeting_date = $meeting_wrapper->field_date->value();
}
else {
  $meeting_date = "";
}

git branch in bash prompt

bashrc.bash
# Add git branch if its present to PS1
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

if [ "$color_prompt" = yes ]; then
  PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\] \$ '
else
  PS1='\u@\h:\w$(parse_git_branch) \$ '
fi

Entity Field Query

efq.php
$query = new EntityFieldQuery;
$results = $query
     ->entityCondition('entity_type', 'node')
     ->propertyCondition('type', 'bill')
     ->fieldCondition("field_field_bill_budget_rel", 1)
     ->addMetaData('account', user_load(1))->execute();

node_load(key($results['node']), NULL, TRUE);