You are here

function taxonomy_term_depth_batch_callbacks_update_term_depth in Taxonomy Term Depth 8

Same name and namespace in other branches
  1. 8.2 taxonomy_term_depth.batch.inc \taxonomy_term_depth_batch_callbacks_update_term_depth()
  2. 7 taxonomy_term_depth.batch.inc \taxonomy_term_depth_batch_callbacks_update_term_depth()

Parameters

$options:

$context:

1 string reference to 'taxonomy_term_depth_batch_callbacks_update_term_depth'
DepthUpdateForm::submitForm in src/Form/DepthUpdateForm.php
Form submission handler.

File

./taxonomy_term_depth.batch.inc, line 15

Code

function taxonomy_term_depth_batch_callbacks_update_term_depth($options, &$context) {

  /**
   * @var \Drupal\Core\Database\Connection.
   */
  $dbh = \Drupal::database();
  $sandbox =& $context['sandbox'];

  // Build query
  $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');

  // Count ALL elements and save the value for further usage
  if (!isset($sandbox['count'])) {

    // Clear all depths first
    $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,
  );

  //$sandbox['position'] += $sandbox['bunch'];
  $query
    ->range($sandbox['position'], $sandbox['bunch']);
  foreach ($query
    ->execute() as $row) {

    // Forcely rebuild data in database
    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'];
}