You are here

public function Table::adopt in Data 8

Let Data manage a table that already exists in the database.

Uses the $name property of the object to determine which database table to adopt.

Return value

TRUE if the table was successfully adopted; FALSE if the table was already known to Data, if the query failed, or if Schema isn't available.

File

src/Table.php, line 133

Class

Table
Manages data access and manipulation for a single data table. Use data_create_table() or data_get_table() to instantiate an object from this class.

Namespace

Drupal\data

Code

public function adopt() {
  if ($this
    ->defined() || !module_exists('schema')) {
    return FALSE;
  }
  $schema = schema_dbobject()
    ->inspect(variable_get('schema_database_connection', 'default'), $this->name);
  if (isset($schema[$this->name])) {
    $table = array(
      'name' => $this->name,
      'title' => data_natural_name($this->name),
      'table_schema' => $schema[$this->name],
      // Add in an empty meta array with the field names so other modules can rely on it.
      'meta' => array(
        'fields' => array_fill_keys(array_keys($schema[$this->name]['fields']), array()),
      ),
    );
    if (drupal_write_record('data_tables', $table)) {
      return TRUE;
    }
  }

  // Clear caches.
  drupal_get_schema($this->name, TRUE);

  // Have views read new views information about table.
  // @todo: this doesn't seem to quite cut it.
  if (module_exists('views')) {
    views_invalidate_cache();
  }

  // data ui exposes path to a new default view.
  if (module_exists('data_ui')) {
    menu_rebuild();
  }
  return FALSE;
}