You are here

function data_create_table in Data 8

Same name and namespace in other branches
  1. 6 data.module \data_create_table()
  2. 7 data.module \data_create_table()

Create a table.

Parameters

$name: String that identifies the data table. It is recommended to use data_name() to generate a table name in the data namespace. For example: $table = data_get_tabe(data_name('my_table'));

$schema: Schema for the table.

$title: A natural title for the table.

Return value

\Drupal\data\Entity\TableConfigInterface A DataTable object if one could be created, FALSE otherwise.

See also

DataTable class.

2 calls to data_create_table()
DataTestCaseAPI::testAPIFunctions in tests/data.test
Test API functions of DataTable and TableFactory.
DataTestCaseAPI::testCRUD in tests/data.test
Run CRUD tests.

File

./data.module, line 40
Hooks and API functions for data module.

Code

function data_create_table($name, $schema, $title = NULL) {
  $storage = \Drupal::entityTypeManager()
    ->getStorage('data_table_config');

  /** @var \Drupal\data\Entity\TableConfigInterface $table */
  $table = $storage
    ->load($name);
  if (!$table || !$table
    ->exists()) {
    $table = $storage
      ->create(array(
      'id' => $name,
      'table_schema' => $schema,
    ));
    $table
      ->save();
  }
  if ($title) {
    $table->title = $title;
    $table
      ->save();
  }
  return $table;
}