You are here

function faq_update_7002 in Frequently Asked Questions 7

Convert old-style detailed questions to new fields.

File

./faq.install, line 315
FAQ module install file.

Code

function faq_update_7002(&$sandbox) {

  // Number of nodes to update each pass.
  define('BATCH_SIZE_7002', '5');

  // Do this the first time.
  if (!isset($sandbox['progress'])) {

    // Initialize sandbox structure for multi-pass update.
    $sandbox['progress'] = 0;
    $sandbox['current_idx'] = 0;

    // Get faq nodes and run the query as user 1.
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'faq')
      ->addMetaData('account', user_load(1));
    $result = $query
      ->execute();
    if (isset($result['node'])) {
      $sandbox['faq_items_nids'] = array_keys($result['node']);
      $sandbox['max'] = count($sandbox['faq_items_nids']);
    }
    else {
      $sandbox['faq_items_nids'] = array();
      $sandbox['max'] = 0;
    }

    // Add the detailed question field.
    _faq_add_custom_fields();

    // Adjust the weight of the field so that it is above the answer (body).
    _faq_shift_fields_down();
  }
  $count = 0;

  // Convert old-style detailed questions to new full field.
  while (($nid = $sandbox['faq_items_nids'][$sandbox['current_idx']]) && $count < BATCH_SIZE_7002) {

    // Load the full node to be updated.
    $node = node_load($nid);

    // Load the detailed question.
    $dq = isset($node->detailed_question) ? $node->detailed_question : '';
    if ($dq == '') {
      $select = db_select('faq_questions', 'f');
      $dq = $select
        ->condition('f.nid', $node->nid)
        ->fields('f', array(
        'detailed_question',
      ))
        ->execute()
        ->fetchField();
    }

    // Get the default text filter format from DB as this might be integer if upgraded site or tekststring if new D7 site.
    // Default filter format: Filtered HTML.
    $filter_formats = filter_formats();
    $filter_formats_keys = array_keys($filter_formats);
    $filter_format = reset($filter_formats_keys);

    // Get the language(s) from the body, making sure we have the same set for detailed question too.
    $langs = array_keys($node->body);

    // Add proper taxonomy fields.
    $txonselect = db_select('taxonomy_index', 't');
    $taxres = $txonselect
      ->fields('t', array(
      'tid',
    ))
      ->condition('t.nid', $node->nid)
      ->execute();
    foreach ($taxres as $taxon) {
      $term = taxonomy_term_load($taxon->tid);
      $vocab = taxonomy_vocabulary_load($term->vid);
      foreach ($langs as $language) {

        // Find out if there is a field added with the vocabulary of this term.
        if (isset($node->{$vocab->module . "_" . $vocab->machine_name})) {
          $node->{$vocab->module . "_" . $vocab->machine_name}[$language][$term->tid] = (array) $term;
        }
      }
    }

    // Add detailed question field for all languages.
    foreach ($langs as $language) {
      $node->field_detailed_question[$language][0]['value'] = $dq;
      $node->field_detailed_question[$language][0]['format'] = $filter_format;
      $node->field_detailed_question[$language][0]['safe_value'] = check_markup($dq, $filter_format, $language);
    }

    // Save resulting node.
    node_save($node);

    // Should not be more than BATCH_SIZE_7002.
    $count++;

    // Progress counter.
    $sandbox['progress']++;

    // Node array index pointer.
    $sandbox['current_idx']++;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  return t('Custom field added, @count questions converted into fields.', array(
    '@count' => $sandbox['max'] + 1,
  ));
}