Skip to content

Instantly share code, notes, and snippets.

View jonathanfranks's full-sized avatar

J F jonathanfranks

  • Breakthrough
  • Evanston, IL
View GitHub Profile
@jonathanfranks
jonathanfranks / gist:467d054fea3242085522
Created December 1, 2014 18:17
Adding a node as a step to a Course object
/**
* Adds a new step to a course
* @param $course_id NID of the Course Node to add the step to
* @param $step_node_id NID of the node to add as a step
*/
function my_module_add_existing_step_to_course($course_id, $step_node_id) {
$node = node_load($course_id);
$course = course_get_course($node);
$new_weight = count($course->getObjects());
@jonathanfranks
jonathanfranks / gist:552b6f650e15f6dfe113
Last active August 29, 2015 14:13
Behat html dump on failure
public function afterScenario($event) {
if ($event->getResult()) {
$this->recordFailedEvent($event);
}
parent::afterScenario($event);
}
public function recordFailedEvent($event) {
$fileName = $this->timestamp;
@jonathanfranks
jonathanfranks / drupal_vocab_delete_terms
Last active August 29, 2015 14:14
Delete all terms from a vocabulary
$vocab_name = '';
$vocab_id = taxonomy_vocabulary_machine_name_load($vocab_name)->vid;
dpm($vocab_id);
$tids = array();
foreach (taxonomy_get_tree($vocab_id) as $term) {
taxonomy_term_delete($term->tid);
}
@jonathanfranks
jonathanfranks / ignore_ds_store.sh
Created February 10, 2015 21:39
Globally ignore all .ds_store files (OSX)
echo .DS_Store > ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
@jonathanfranks
jonathanfranks / Form alter
Created March 5, 2015 18:35
Drupal #states or conditions
$form['my_field']['#states'] = array(
'visible' => array(
array(
array(':input[name="field_other[und]"]' => array('value' => 'first')),
'or',
array(':input[name="field_other[und]"]' => array('value' => 'second')),
),
),
);
@jonathanfranks
jonathanfranks / FeatureContext.php
Created March 21, 2015 21:34
Writing an html dump on a Behat failure
public function recordFailedEvent($event) {
$fileName = $this->timestamp;
// TODO: Make this a setting in behat.yml?
$html_dump_path = 'failures';
$message = '';
$session = $this->getSession();
$page = $session->getPage();
$driver = $session->getDriver();
$date = date('Y-m-d H:i:s');
@jonathanfranks
jonathanfranks / EFQ
Last active August 29, 2015 14:17
Delete all entities of a type
$entity_type = 'node';
$bundle = 'article';
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', $bundle);
$result = $query->execute();
if (isset($result[$entity_type])) {
$ids = array_keys($result[$entity_type]);
@jonathanfranks
jonathanfranks / FailureHtmlDumpContext.php
Created April 9, 2015 00:51
Behat 3 HTML dump on failure
<?php
use Behat\MinkExtension\Context\RawMinkContext,
Behat\Behat\Hook\Scope\AfterScenarioScope;
/**
* Defines application features from the specific context.
*/
class FailureHtmlDumpContext extends RawMinkContext {
/**
@jonathanfranks
jonathanfranks / publish_nodes.php
Created April 20, 2015 23:45
Publish all nodes of a type
public function iPublishAllNodes($content_type) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', $content_type);
$result = $query->execute();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
$node->status = 1;
@jonathanfranks
jonathanfranks / gist:a38829b184eb54ebf83a
Created June 5, 2015 21:07
Drupal Facet API Custom Hierarchy Callback for Entity Reference Fields
/**
* Implements hook_facetapi_facet_info_alter().
*/
function hook_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {
foreach ($facet_info as $name => $facet) {
$name = 'field_parent';
if ($name == $name) {
$facet_info[$name]['hierarchy callback'] = 'hook_facets_entity_reference_hierarchy';
}
}