You are here

function tablefield_update_7005 in TableField 7.3

Remove all empty multi-value TableField fields with locked values.

File

./tablefield.install, line 266
Installation options for TableField.

Code

function tablefield_update_7005() {

  // Get all the field names for all multi-value tablefields with locked values.
  $instances = field_info_instances();
  $field_names = array();
  foreach ($instances as $entity_type => $entities) {
    foreach ($entities as $bundle => $fields) {
      foreach ($fields as $field_name => $instance) {
        $field_info = field_info_field($field_name);
        if (in_array($instance['widget']['type'], array(
          'tablefield',
        )) && $field_info['cardinality'] != 1 && $field_info['settings']['lock_values'] == 1) {

          // Uniquely store the field names in an array for later use.
          if (!in_array($instance['field_name'], $field_names)) {
            array_push($field_names, $instance['field_name']);
          }
        }
      }
    }
  }

  // Go through all empty multi-value TableField fields with locked values.
  foreach ($field_names as $field_name) {
    $tables = array(
      'field_data_' . $field_name,
      'field_revision_' . $field_name,
    );
    foreach ($tables as $table) {
      $field = $field_name . '_value';
      $query = db_select($table, 'n')
        ->fields('n')
        ->execute()
        ->fetchAll();
      foreach ($query as $record) {
        $instance = unserialize($record->{$field});

        // Rationalize the table data.
        if (!empty($instance)) {

          // Remove extraneous data.
          $count_cols = $instance['rebuild']['count_cols'];
          $count_rows = $instance['rebuild']['count_rows'];
          $caption = $instance['caption'];
          $rebuild = $instance['rebuild'];
          $import = $instance['import'];
          $paste = $instance['paste'];
          unset($instance['caption']);
          unset($instance['rebuild']);
          unset($instance['import']);
          unset($instance['paste']);
          foreach ($instance as $key => $value) {
            array_shift($value);
            $empty = TRUE;
            foreach ($value as $row) {
              array_pop($row);
              if (array_filter($row)) {
                $empty = FALSE;
              }
            }
          }
        }
        if ($empty) {
          unset($instance['tabledata']);
        }
        else {

          // Recreate previous removed data.
          $instance['caption'] = $caption;
          $instance['rebuild'] = $rebuild;
          $instance['import'] = $import;
          $instance['paste'] = $paste;
        }

        // Change the stored data by a per record unique column key combination.
        db_update($table)
          ->fields(array(
          $field => serialize($instance),
        ))
          ->condition('entity_id', $record->entity_id)
          ->condition('revision_id', $record->revision_id)
          ->condition('delta', $record->delta)
          ->condition('entity_type', $record->entity_type)
          ->condition('bundle', $record->bundle)
          ->execute();

        // Delete the tables that are now empty.
        db_delete($table)
          ->condition($field_name . '_value', 'a:0:{}')
          ->execute();
      }
    }
  }
  field_cache_clear();
  drupal_set_message(t('All empty multi-value TableField fields with locked values are now removed.'), 'warning');
}