Skip to content

Instantly share code, notes, and snippets.

@creynders
Last active October 13, 2015 16:28
Show Gist options
  • Save creynders/4223909 to your computer and use it in GitHub Desktop.
Save creynders/4223909 to your computer and use it in GitHub Desktop.
Drupal Snippets
/*
searches all nodes (of all nodetypes) that reference <code>target_nid</code> and deletes them
dependencies: Entity, EntityReference
*/
$conditions = array( 'type' => 'entityreference' );
$include_additional = array( 'include_inactive' => TRUE );
$fields = field_read_fields( $conditions, $include_additional );
foreach( $fields as $field ){
$query = new EntityFieldQuery();
$results = $query
->entityCondition( 'entity_type', 'node' )
->fieldCondition( $field, 'target_id', $target_nid )
->execute();
if( array_key_exists( 'node', $results ) ){
$nids = array_keys( $results[ 'node' ] );
node_delete_multiple( $nids );
}
}
//on source drupal installation
drush pm-list --no-core --pipe --status=enabled --type=module > dependencies.txt
//move dependencies.txt to target installation
drush en `cat dependencies.txt` --yes
/*
return all nodes for a nodetype 'qux_type' that have a value of 'Bar' for a field 'field_foo' OR no value at all.
*/
$query = db_select( 'node', 'n' );
$query->leftJoin( 'field_data_field_foo', 'foo', 'n.nid=foo.entity_id' );
$result = $query->fields( 'foo', array( 'field_foo_value' ) )
->fields( 'n', array( 'nid' ) )
->condition( 'n.type', 'qux_type' )
->where( "foo.field_foo_value='Bar' OR (foo.field_foo_value IS NULL)" )
->execute()
->fetchAll();
/*
N.B.: the expression on the right of the OR should use the IS comparison operator, _not_ "=",
otherwise it will only return the nodes that have a "NULL" value for that field.
*/
function <themename?_preprocess_page(&$variables, $hook) {
/*
* allows for defining templates by alias
* see http://cheekymonkeymedia.ca/blog/gene-bernier/how-create-page-tpl-suggestions-based-path-alias-drupal-7
*/
// Get the alias for the page being viewed
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$template_filename = 'page';
//Break it down for each piece of the alias path
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '__' . $path_part;
$variables['theme_hook_suggestions'][] = $template_filename;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment