taxonomy_term_depth.batch.inc in Taxonomy Term Depth 8
File
taxonomy_term_depth.batch.inc
View source
<?php
require_once __DIR__ . '/constants.inc';
function taxonomy_term_depth_batch_callbacks_update_term_depth($options, &$context) {
$dbh = \Drupal::database();
$sandbox =& $context['sandbox'];
$query = $dbh
->select('taxonomy_term_field_data', 'ttd');
$query
->fields('ttd', array(
'tid',
));
if (!empty($options['vids'])) {
$query
->condition('ttd.vid', (array) $options['vids'], 'IN');
}
$query
->groupBy('ttd.tid');
if (!isset($sandbox['count'])) {
$updateQuery = $dbh
->update('taxonomy_term_field_data')
->fields([
'depth_level' => NULL,
'depth' => NULL,
]);
if (!empty($options['vids'])) {
$updateQuery
->condition('vid', (array) $options['vids'], 'IN');
}
$updateQuery
->execute();
$sandbox['count'] = $query
->countQuery()
->execute()
->fetchField();
}
$sandbox += array(
'position' => 0,
'bunch' => 20,
);
$query
->range($sandbox['position'], $sandbox['bunch']);
foreach ($query
->execute() as $row) {
taxonomy_term_depth_get_by_tid($row->tid, TRUE);
$sandbox['position']++;
}
$context['finished'] = $sandbox['position'] / $sandbox['count'];
$context['finished'] = $context['finished'] > 1 ? 1 : $context['finished'];
}