You are here

function flexiform_update_7014 in Flexiform 7

Add display table for flexiforms.

File

./flexiform.install, line 612
Sets up the base table for our entity and a table to store information about the entity types.

Code

function flexiform_update_7014(&$sandbox) {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'flexiform');
  if (!isset($sandbox['progress'])) {
    if (!db_table_exists('flexiform_display')) {
      $schema = array(
        'description' => 'Stores information about flexiform displays.',
        'fields' => array(
          'id' => array(
            'type' => 'int',
            'unsigned' => TRUE,
            'not null' => TRUE,
            'description' => 'The primary identifier of the flexiform.',
          ),
          'form' => array(
            'description' => 'The machine-readable name of this flexiform.',
            'type' => 'varchar',
            'length' => 255,
            'not null' => TRUE,
          ),
          'display' => array(
            'type' => 'varchar',
            'length' => '255',
            'not null' => TRUE,
            'description' => 'The tag string associated with this flexiform',
          ),
        ),
        'unique keys' => array(
          'form_display' => array(
            'form',
            'display',
          ),
        ),
        'indexes' => array(
          'id' => array(
            'id',
          ),
          'form' => array(
            'form',
          ),
          'display' => array(
            'display',
          ),
        ),
      );
      db_create_table('flexiform_display', $schema);
    }
    $sandbox['progress'] = 0;
    $sandbox['last_id'] = 0;
    $count = clone $query;
    $count
      ->count();
    $sandbox['max'] = $count
      ->execute();
  }
  $query
    ->entityCondition('entity_id', $sandbox['last_id'], '>');
  $query
    ->range(0, 50);
  $results = $query
    ->execute();
  if (!empty($results['flexiform'])) {
    $query = db_insert('flexiform_display');
    $query
      ->fields(array(
      'id',
      'form',
      'display',
    ));
    foreach (entity_load('flexiform', array_keys($results['flexiform'])) as $flexiform) {
      foreach ($flexiform->displays as $key => $settings) {
        if (($display = $flexiform
          ->getDisplay($key)) && $display
          ->isEnabled()) {
          $query
            ->values(array(
            'id' => $flexiform->id,
            'form' => $flexiform->form,
            'display' => $key,
          ));
        }
      }
      $sandbox['progress']++;
      $sandbox['last_id'] = $flexiform->id;
    }
    $query
      ->execute();
  }
  $sandbox['#finished'] = !empty($sandbox['max']) ? min($sandbox['progress'] / $sandbox['max'], 1) : 1;
  $t = get_t();
  $sandbox['message'] = $t("Processed @percent% (@progress/@max).\n", array(
    '@percent' => round($sandbox['#finished'] * 100, 2),
    '@progress' => $sandbox['progress'],
    '@max' => $sandbox['max'],
  ));

  // Work out how to output our message.
  if (drupal_is_cli()) {
    if (function_exists('drush_log')) {
      drush_log($sandbox['message'], 'status');
    }
    else {
      print $sandbox['message'] . "\n";
    }
  }
  else {
    return $sandbox['message'];
  }
}