You are here

function panels_update_7302 in Panels 7.3

Adding universally unique identifiers to panels.

Note: This update hook is not written well. It calls apis which uses the most updated drupal database, causing missing columns or tables errors. To mitigate the issue, we've added updates from 7303 and 7305. Future updates should go below the 7305 update.

See https://www.drupal.org/node/2787123 for more info.

File

./panels.install, line 488

Code

function panels_update_7302() {
  if (!module_load_include('inc', 'ctools', 'includes/uuid')) {
    throw new DrupalUpdateException(t('Ctools UUID support not detected. You must update to a more recent version of the ctools module.'));
  }

  // Run the 7303 update first to avoid caching issues.
  // This *probably* should be placed right above update 7305, however it was
  // tested here and re-testing this update is difficult, so it stays here.
  panels_update_7303();

  // Load the schema.
  $schema = panels_schema_5();
  $msg = array();

  // Add the uuid column to the pane table.
  $table = 'panels_pane';
  $field = 'uuid';

  // Due to a previous failure, the column may already exist:
  if (!db_field_exists($table, $field)) {
    $spec = $schema[$table]['fields'][$field];
    db_add_field($table, $field, $spec);
    $msg[] = t('Added panels_pane.uuid column.');
  }

  // Add the uuid column to the display table.
  $table = 'panels_display';
  $field = 'uuid';

  // Due to a previous failure, the column may already exist:
  if (!db_field_exists($table, $field)) {
    $spec = $schema[$table]['fields'][$field];
    db_add_field($table, $field, $spec);
    $msg[] = t('Added panels_display.uuid column.');
  }
  if (empty($msg)) {
    $msg[] = t('UUID column already present in the panels_display & panels_pane tables.');
  }

  // Update all DB-based panes & displays to ensure that they all contain a UUID.
  $display_dids = db_select('panels_display')
    ->fields('panels_display', array(
    'did',
  ))
    ->condition(db_or()
    ->condition('uuid', '')
    ->isNull('uuid'))
    ->execute()
    ->fetchCol();

  // Check the panes as well, for paranoia.
  $pane_dids = db_select('panels_pane')
    ->distinct()
    ->fields('panels_pane', array(
    'did',
  ))
    ->condition(db_or()
    ->condition('uuid', '')
    ->isNull('uuid'))
    ->execute()
    ->fetchCol();
  $dids = array_unique(array_merge($display_dids, $pane_dids));

  // Before using panels_save_display(), we have to make sure any new fields
  // are added from future updates.
  panels_update_7305();

  // If the Panels module is disabled we don't have access to
  // panels_load_displays().
  if (!function_exists('panels_load_displays')) {
    module_load_include('module', 'panels');
  }
  if ($displays = panels_load_displays($dids)) {
    foreach ($displays as $display) {

      // A display save also triggers pane saves.
      panels_save_display($display);
    }
    $msg[] = t('Generated UUIDs for database-based panel displays and panes.');
  }
  else {
    $msg[] = t('No database-based panel displays or panes for which to generate UUIDs.');
  }
  return implode("\n", $msg);
}