Skip to content

Instantly share code, notes, and snippets.

View aschanMarcus's full-sized avatar

Marcus Aschan aschanMarcus

  • Helsinki Finland
View GitHub Profile
<?php
$query = db_select('node', 'n');
$nids = $query
->fields('n')
->condition('type', 'fitspirator')
->execute()
->fetchCol();
$nodes = node_load_multiple($nids);
@aschanMarcus
aschanMarcus / delete_taxonomy_term
Last active August 29, 2015 14:03
Delete unused taxonomy terms
<?php
$field = 'VOCABULARY_MACHINE_NAME';
// Search for unused taxonomy terms.
$query = db_query('SELECT DISTINCT field_' . $field . '_tid FROM field_data_field_' . $field . ' WHERE field_' . $field . '_tid NOT IN (SELECT tid FROM taxonomy_term_data)');
$results = $query->fetchCol();
// Output results with devel.
dpm($results);
@aschanMarcus
aschanMarcus / create_users_for_nodes
Created July 2, 2014 15:01
Create users for every 'fitspirator' node without an entity reference.
/**
* Create user for every fitspirator node without any entity reference.
*/
$query = db_select('node', 'n');
$nids = $query
->fields('n', array('nid'))
->condition('type', 'fitspirator')
->execute()
->fetchCol();
@aschanMarcus
aschanMarcus / enable_comments
Created July 2, 2014 13:42
Enable comments for all nodes of a certain type.
$args = array(':type' => 'fitspirator');
$node = "UPDATE node AS n SET n.comment = 2 WHERE n.type = :type";
db_query($node, $args);
$node_revision = "UPDATE node_revision AS nr JOIN node AS n ON nr.nid = n.nid SET nr.comment = 2 WHERE n.type = :type";
db_query($node_revision, $args);
@aschanMarcus
aschanMarcus / delete_node_type
Created March 28, 2014 09:02
Delete all nodes of certain type
$results = db_select('node', 'n')
->fields('n', array('nid'))
->condition('type', TYPE)
->execute();
foreach ($results as $result) {
$nids[] = $result->nid;
}
if (!empty($nids)) {
node_delete_multiple($nids);
@aschanMarcus
aschanMarcus / remove_css
Created March 19, 2014 11:07
Remove inline CSS from body fields in Drupal.
/**
* For removing inline CSS from body fields in Drupal.
*
* -Install devel module.
* -Navigate to devel/php.
* -Paste the following code and execute.
*/
$results = db_select('field_data_body', 'f')
->fields('f', array('entity_id', 'body_value'))