You are here

tablefield.install in TableField 8.2

Installation options for TableField.

File

tablefield.install
View source
<?php

/**
 * @file
 * Installation options for TableField.
 */

/**
 * Add columns for caption field to the database.
 */
function tablefield_update_8001() {
  tablefield_add_new_column('caption', [
    'type' => 'varchar',
    'length' => 255,
    'default' => '',
    'not null' => TRUE,
  ]);
}

/**
 * Helper function to add new columns to the field schema.
 *
 * @param string $column_name
 *   The name of the column that will be added.
 * @param array $spec
 *   The options of the new column.
 */
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);
        }
      }
    }
  }
}

Functions

Namesort descending Description
tablefield_add_new_column Helper function to add new columns to the field schema.
tablefield_update_8001 Add columns for caption field to the database.