Skip to content

Instantly share code, notes, and snippets.

@ahebrank
Last active June 28, 2018 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahebrank/be91b2bba61496ea5b31d4d38e6687a3 to your computer and use it in GitHub Desktop.
Save ahebrank/be91b2bba61496ea5b31d4d38e6687a3 to your computer and use it in GitHub Desktop.
Set pathauto state for all entities in a bundle
<?php
// PARAMETER EXAMPLES
// ------------------
// desired status for entities in bundle
// // must be 1 or 0
// $pathauto_status = 1;
// // entity and type
// $entity = 'taxonomy_term';
// $bundle = 'offices_and_subunits';
// ------------------
// use the last three parameters
list($pathauto_status, $entity, $bundle) = array_slice($_SERVER['argv'], -3);
$pathauto_status = (int)$pathauto_status;
if (!($pathauto_status === 1 || $pathauto_status === 0)) {
echo "Need parameters [STATUS (1 or 0)] [ENTITY] [BUNDLE]\n";
echo "$pathauto_status must be 1 or 0\n";
exit(1);
}
$base_table = $entity;
$bundle_key = 'type';
$id_key = 'id';
// terms are abnormal entities
if ($entity == 'taxonomy_term') {
$base_table = 'taxonomy_term_data';
$bundle_key = 'vid';
$id_key = 'tid';
}
$db = \Drupal::database();
$result = $db->select($base_table)
->fields($base_table, [$id_key])
->condition($bundle_key, $bundle)
->execute();
$ids = $result->fetchCol();
echo "Found " . count($ids) . " " . $entity . "(s) of type " . $bundle . "\n";
$updated = $db->update('key_value')
->fields([
'value' => 'i:' . $pathauto_status . ';',
])
->condition('collection', 'pathauto_state.' . $entity)
->condition('name', $ids, 'IN')
->execute();
echo "Updated pathauto state for " . $updated . " " . $entity . "(s).\n";
echo "You may want to regenerate aliases for this entity.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment