You are here

function tablefield_add_new_column in TableField 8.2

Helper function to add new columns to the field schema.

Parameters

string $column_name: The name of the column that will be added.

array $spec: The options of the new column.

1 call to tablefield_add_new_column()
tablefield_update_8001 in ./tablefield.install
Add columns for caption field to the database.

File

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

Code

function tablefield_add_new_column($column_name, array $spec) {
  $field_map = \Drupal::service('entity_field.manager')
    ->getFieldMapByFieldType('tablefield');
  $schema = \Drupal::database()
    ->schema();
  foreach ($field_map as $entity_type_id => $fields) {
    foreach (array_keys($fields) as $field_name) {
      $tables = [
        "{$entity_type_id}__{$field_name}",
        "{$entity_type_id}_revision__{$field_name}",
      ];
      $new_column_name = $field_name . '_' . $column_name;
      foreach ($tables as $table) {
        $field_exists = $schema
          ->fieldExists($table, $new_column_name);
        $table_exists = $schema
          ->tableExists($table);
        if (!$field_exists && $table_exists) {
          $schema
            ->addField($table, $new_column_name, $spec);
        }
      }
    }
  }
}