You are here

function linked_field_update_7003 in Linked Field 7

Update views which contain Linked Field settings.

File

./linked_field.install, line 186
Contains install and update functions for Linked Field.

Code

function linked_field_update_7003(&$sandbox) {
  if (db_table_exists('views_display')) {

    // Getting all view displays from database.
    $view_displays = db_select('views_display', 'vd')
      ->fields('vd')
      ->execute();
    $view_displays_count = db_select('views_display', 'vd')
      ->countQuery()
      ->execute()
      ->fetchField();

    // We initialize the batch process.
    if (!isset($sandbox['progress'])) {
      $sandbox['progress'] = 0;
      $sandbox['current_view_display'] = 1;
      $sandbox['max'] = $view_displays_count;
    }

    // Iterate all view displays and modify them.
    foreach ($view_displays as $view_display) {
      $vid = $view_display->vid;
      $id = $view_display->id;
      $display_options = unserialize($view_display->display_options);
      if (isset($display_options['fields'])) {
        foreach ($display_options['fields'] as &$field) {
          $settings =& $field['settings'];
          if (isset($settings['linked_field'])) {
            foreach ($settings['linked_field'] as $name => $value) {
              if ($name != 'linked' && $name != 'destination' && $name != 'advanced') {

                // We move the setting into the 'advanced' array.
                $settings['linked_field']['advanced'][$name] = $value;
                unset($settings['linked_field'][$name]);
              }
            }
          }
        }
      }
      $display_options = serialize($display_options);

      // Finally we update the current view display.
      db_update('views_display')
        ->fields(array(
        'display_options' => $display_options,
      ))
        ->condition('vid', $vid)
        ->condition('id', $id)
        ->execute();

      // Prepeare the values for the next step.
      $sandbox['progress']++;
      $sandbox['current_view_display']++;

      // Update process is finished when all settings have been ran.
      $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
    }
  }
}