Default implementation of the field storage API.

File

modules/field/modules/field_sql_storage/field_sql_storage.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Default implementation of the field storage API.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function field_sql_storage_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#field_sql_storage':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Field SQL storage module stores field data in the database. It is the default field storage module; other field storage mechanisms may be available as contributed modules. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
  15. return $output;
  16. }
  17. }
  18. /**
  19. * Implements hook_field_storage_info().
  20. */
  21. function field_sql_storage_field_storage_info() {
  22. return array(
  23. 'field_sql_storage' => array(
  24. 'label' => t('Default SQL storage'),
  25. 'description' => t('Stores fields in the local SQL database, using per-field tables.'),
  26. ),
  27. );
  28. }
  29. /**
  30. * Generate a table name for a field data table.
  31. *
  32. * @param $field
  33. * The field structure.
  34. * @return
  35. * A string containing the generated name for the database table
  36. */
  37. function _field_sql_storage_tablename($field) {
  38. if ($field['deleted']) {
  39. return "field_deleted_data_{$field['id']}";
  40. }
  41. else {
  42. return "field_data_{$field['field_name']}";
  43. }
  44. }
  45. /**
  46. * Generate a table name for a field revision archive table.
  47. *
  48. * @param $name
  49. * The field structure.
  50. * @return
  51. * A string containing the generated name for the database table
  52. */
  53. function _field_sql_storage_revision_tablename($field) {
  54. if ($field['deleted']) {
  55. return "field_deleted_revision_{$field['id']}";
  56. }
  57. else {
  58. return "field_revision_{$field['field_name']}";
  59. }
  60. }
  61. /**
  62. * Generates a table alias for a field data table.
  63. *
  64. * The table alias is unique for each unique combination of field name
  65. * (represented by $tablename), delta_group and language_group.
  66. *
  67. * @param $tablename
  68. * The name of the data table for this field.
  69. * @param $field_key
  70. * The numeric key of this field in this query.
  71. * @param $query
  72. * The EntityFieldQuery that is executed.
  73. *
  74. * @return
  75. * A string containing the generated table alias.
  76. */
  77. function _field_sql_storage_tablealias($tablename, $field_key, EntityFieldQuery $query) {
  78. // No conditions present: use a unique alias.
  79. if (empty($query->fieldConditions[$field_key])) {
  80. return $tablename . $field_key;
  81. }
  82. // Find the delta and language condition values and append them to the alias.
  83. $condition = $query->fieldConditions[$field_key];
  84. $alias = $tablename;
  85. $has_group_conditions = FALSE;
  86. foreach (array('delta', 'language') as $column) {
  87. if (isset($condition[$column . '_group'])) {
  88. $alias .= '_' . $column . '_' . $condition[$column . '_group'];
  89. $has_group_conditions = TRUE;
  90. }
  91. }
  92. // Return the alias when it has delta/language group conditions.
  93. if ($has_group_conditions) {
  94. return $alias;
  95. }
  96. // Return a unique alias in other cases.
  97. return $tablename . $field_key;
  98. }
  99. /**
  100. * Generate a column name for a field data table.
  101. *
  102. * @param $name
  103. * The name of the field
  104. * @param $column
  105. * The name of the column
  106. * @return
  107. * A string containing a generated column name for a field data
  108. * table that is unique among all other fields.
  109. */
  110. function _field_sql_storage_columnname($name, $column) {
  111. return $name . '_' . $column;
  112. }
  113. /**
  114. * Generate an index name for a field data table.
  115. *
  116. * @param $name
  117. * The name of the field
  118. * @param $column
  119. * The name of the index
  120. * @return
  121. * A string containing a generated index name for a field data
  122. * table that is unique among all other fields.
  123. */
  124. function _field_sql_storage_indexname($name, $index) {
  125. return $name . '_' . $index;
  126. }
  127. /**
  128. * Return the database schema for a field. This may contain one or
  129. * more tables. Each table will contain the columns relevant for the
  130. * specified field. Leave the $field's 'columns' and 'indexes' keys
  131. * empty to get only the base schema.
  132. *
  133. * @param $field
  134. * The field structure for which to generate a database schema.
  135. * @return
  136. * One or more tables representing the schema for the field.
  137. */
  138. function _field_sql_storage_schema($field) {
  139. $deleted = $field['deleted'] ? 'deleted ' : '';
  140. $current = array(
  141. 'description' => "Data storage for {$deleted}field {$field['id']} ({$field['field_name']})",
  142. 'fields' => array(
  143. 'entity_type' => array(
  144. 'type' => 'varchar',
  145. 'length' => 128,
  146. 'not null' => TRUE,
  147. 'default' => '',
  148. 'description' => 'The entity type this data is attached to',
  149. ),
  150. 'bundle' => array(
  151. 'type' => 'varchar',
  152. 'length' => 128,
  153. 'not null' => TRUE,
  154. 'default' => '',
  155. 'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
  156. ),
  157. 'deleted' => array(
  158. 'type' => 'int',
  159. 'size' => 'tiny',
  160. 'not null' => TRUE,
  161. 'default' => 0,
  162. 'description' => 'A boolean indicating whether this data item has been deleted'
  163. ),
  164. 'entity_id' => array(
  165. 'type' => 'int',
  166. 'unsigned' => TRUE,
  167. 'not null' => TRUE,
  168. 'description' => 'The entity id this data is attached to',
  169. ),
  170. 'revision_id' => array(
  171. 'type' => 'int',
  172. 'unsigned' => TRUE,
  173. 'not null' => FALSE,
  174. 'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
  175. ),
  176. // @todo Consider storing language as integer.
  177. 'language' => array(
  178. 'type' => 'varchar',
  179. 'length' => 32,
  180. 'not null' => TRUE,
  181. 'default' => '',
  182. 'description' => 'The language for this data item.',
  183. ),
  184. 'delta' => array(
  185. 'type' => 'int',
  186. 'unsigned' => TRUE,
  187. 'not null' => TRUE,
  188. 'description' => 'The sequence number for this data item, used for multi-value fields',
  189. ),
  190. ),
  191. 'primary key' => array('entity_type', 'entity_id', 'deleted', 'delta', 'language'),
  192. 'indexes' => array(
  193. 'entity_type' => array('entity_type'),
  194. 'bundle' => array('bundle'),
  195. 'deleted' => array('deleted'),
  196. 'entity_id' => array('entity_id'),
  197. 'revision_id' => array('revision_id'),
  198. 'language' => array('language'),
  199. ),
  200. );
  201. $field += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array());
  202. // Add field columns.
  203. foreach ($field['columns'] as $column_name => $attributes) {
  204. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  205. $current['fields'][$real_name] = $attributes;
  206. }
  207. // Add indexes.
  208. foreach ($field['indexes'] as $index_name => $columns) {
  209. $real_name = _field_sql_storage_indexname($field['field_name'], $index_name);
  210. foreach ($columns as $column_name) {
  211. $current['indexes'][$real_name][] = _field_sql_storage_columnname($field['field_name'], $column_name);
  212. }
  213. }
  214. // Add foreign keys.
  215. foreach ($field['foreign keys'] as $specifier => $specification) {
  216. $real_name = _field_sql_storage_indexname($field['field_name'], $specifier);
  217. $current['foreign keys'][$real_name]['table'] = $specification['table'];
  218. foreach ($specification['columns'] as $column_name => $referenced) {
  219. $sql_storage_column = _field_sql_storage_columnname($field['field_name'], $column_name);
  220. $current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced;
  221. }
  222. }
  223. // Construct the revision table.
  224. $revision = $current;
  225. $revision['description'] = "Revision archive storage for {$deleted}field {$field['id']} ({$field['field_name']})";
  226. $revision['primary key'] = array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'language');
  227. $revision['fields']['revision_id']['not null'] = TRUE;
  228. $revision['fields']['revision_id']['description'] = 'The entity revision id this data is attached to';
  229. return array(
  230. _field_sql_storage_tablename($field) => $current,
  231. _field_sql_storage_revision_tablename($field) => $revision,
  232. );
  233. }
  234. /**
  235. * Implements hook_field_storage_create_field().
  236. */
  237. function field_sql_storage_field_storage_create_field($field) {
  238. $schema = _field_sql_storage_schema($field);
  239. foreach ($schema as $name => $table) {
  240. db_create_table($name, $table);
  241. }
  242. drupal_get_schema(NULL, TRUE);
  243. }
  244. /**
  245. * Implements hook_field_update_forbid().
  246. *
  247. * Forbid any field update that changes column definitions if there is
  248. * any data.
  249. */
  250. function field_sql_storage_field_update_forbid($field, $prior_field, $has_data) {
  251. if ($has_data && $field['columns'] != $prior_field['columns']) {
  252. throw new FieldUpdateForbiddenException("field_sql_storage cannot change the schema for an existing field with data.");
  253. }
  254. }
  255. /**
  256. * Implements hook_field_storage_update_field().
  257. */
  258. function field_sql_storage_field_storage_update_field($field, $prior_field, $has_data) {
  259. if (! $has_data) {
  260. // There is no data. Re-create the tables completely.
  261. if (Database::getConnection()->supportsTransactionalDDL()) {
  262. // If the database supports transactional DDL, we can go ahead and rely
  263. // on it. If not, we will have to rollback manually if something fails.
  264. $transaction = db_transaction();
  265. }
  266. try {
  267. $prior_schema = _field_sql_storage_schema($prior_field);
  268. foreach ($prior_schema as $name => $table) {
  269. db_drop_table($name, $table);
  270. }
  271. $schema = _field_sql_storage_schema($field);
  272. foreach ($schema as $name => $table) {
  273. db_create_table($name, $table);
  274. }
  275. }
  276. catch (Exception $e) {
  277. if (Database::getConnection()->supportsTransactionalDDL()) {
  278. $transaction->rollback();
  279. }
  280. else {
  281. // Recreate tables.
  282. $prior_schema = _field_sql_storage_schema($prior_field);
  283. foreach ($prior_schema as $name => $table) {
  284. if (!db_table_exists($name)) {
  285. db_create_table($name, $table);
  286. }
  287. }
  288. }
  289. throw $e;
  290. }
  291. }
  292. else {
  293. // There is data, so there are no column changes. Drop all the
  294. // prior indexes and create all the new ones, except for all the
  295. // priors that exist unchanged.
  296. $table = _field_sql_storage_tablename($prior_field);
  297. $revision_table = _field_sql_storage_revision_tablename($prior_field);
  298. foreach ($prior_field['indexes'] as $name => $columns) {
  299. if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) {
  300. $real_name = _field_sql_storage_indexname($field['field_name'], $name);
  301. db_drop_index($table, $real_name);
  302. db_drop_index($revision_table, $real_name);
  303. }
  304. }
  305. $table = _field_sql_storage_tablename($field);
  306. $revision_table = _field_sql_storage_revision_tablename($field);
  307. foreach ($field['indexes'] as $name => $columns) {
  308. if (!isset($prior_field['indexes'][$name]) || $columns != $prior_field['indexes'][$name]) {
  309. $real_name = _field_sql_storage_indexname($field['field_name'], $name);
  310. $real_columns = array();
  311. foreach ($columns as $column_name) {
  312. $real_columns[] = _field_sql_storage_columnname($field['field_name'], $column_name);
  313. }
  314. db_add_index($table, $real_name, $real_columns);
  315. db_add_index($revision_table, $real_name, $real_columns);
  316. }
  317. }
  318. }
  319. drupal_get_schema(NULL, TRUE);
  320. }
  321. /**
  322. * Implements hook_field_storage_delete_field().
  323. */
  324. function field_sql_storage_field_storage_delete_field($field) {
  325. // Mark all data associated with the field for deletion.
  326. $field['deleted'] = 0;
  327. $table = _field_sql_storage_tablename($field);
  328. $revision_table = _field_sql_storage_revision_tablename($field);
  329. db_update($table)
  330. ->fields(array('deleted' => 1))
  331. ->execute();
  332. // Move the table to a unique name while the table contents are being deleted.
  333. $field['deleted'] = 1;
  334. $new_table = _field_sql_storage_tablename($field);
  335. $revision_new_table = _field_sql_storage_revision_tablename($field);
  336. db_rename_table($table, $new_table);
  337. db_rename_table($revision_table, $revision_new_table);
  338. drupal_get_schema(NULL, TRUE);
  339. }
  340. /**
  341. * Implements hook_field_storage_load().
  342. */
  343. function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  344. $load_current = $age == FIELD_LOAD_CURRENT;
  345. foreach ($fields as $field_id => $ids) {
  346. // By the time this hook runs, the relevant field definitions have been
  347. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  348. // on each field individually is more efficient than loading all fields in
  349. // memory upfront with field_info_field_by_ids().
  350. $field = field_info_field_by_id($field_id);
  351. $field_name = $field['field_name'];
  352. $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
  353. $query = db_select($table, 't')
  354. ->fields('t')
  355. ->condition('entity_type', $entity_type)
  356. ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
  357. ->condition('language', field_available_languages($entity_type, $field), 'IN')
  358. ->orderBy('delta');
  359. if (empty($options['deleted'])) {
  360. $query->condition('deleted', 0);
  361. }
  362. $results = $query->execute();
  363. $delta_count = array();
  364. foreach ($results as $row) {
  365. if (!isset($delta_count[$row->entity_id][$row->language])) {
  366. $delta_count[$row->entity_id][$row->language] = 0;
  367. }
  368. if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
  369. $item = array();
  370. // For each column declared by the field, populate the item
  371. // from the prefixed database column.
  372. foreach ($field['columns'] as $column => $attributes) {
  373. $column_name = _field_sql_storage_columnname($field_name, $column);
  374. $item[$column] = $row->$column_name;
  375. }
  376. // Add the item to the field values for the entity.
  377. $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
  378. $delta_count[$row->entity_id][$row->language]++;
  379. }
  380. }
  381. }
  382. }
  383. /**
  384. * Implements hook_field_storage_write().
  385. */
  386. function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fields) {
  387. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  388. if (!isset($vid)) {
  389. $vid = $id;
  390. }
  391. foreach ($fields as $field_id) {
  392. $field = field_info_field_by_id($field_id);
  393. $field_name = $field['field_name'];
  394. $table_name = _field_sql_storage_tablename($field);
  395. $revision_name = _field_sql_storage_revision_tablename($field);
  396. $all_languages = field_available_languages($entity_type, $field);
  397. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  398. // Delete and insert, rather than update, in case a value was added.
  399. if ($op == FIELD_STORAGE_UPDATE) {
  400. // Delete languages present in the incoming $entity->$field_name.
  401. // Delete all languages if $entity->$field_name is empty.
  402. $languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
  403. if ($languages) {
  404. db_delete($table_name)
  405. ->condition('entity_type', $entity_type)
  406. ->condition('entity_id', $id)
  407. ->condition('language', $languages, 'IN')
  408. ->execute();
  409. db_delete($revision_name)
  410. ->condition('entity_type', $entity_type)
  411. ->condition('entity_id', $id)
  412. ->condition('revision_id', $vid)
  413. ->condition('language', $languages, 'IN')
  414. ->execute();
  415. }
  416. }
  417. // Prepare the multi-insert query.
  418. $do_insert = FALSE;
  419. $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
  420. foreach ($field['columns'] as $column => $attributes) {
  421. $columns[] = _field_sql_storage_columnname($field_name, $column);
  422. }
  423. $query = db_insert($table_name)->fields($columns);
  424. $revision_query = db_insert($revision_name)->fields($columns);
  425. foreach ($field_languages as $langcode) {
  426. $items = (array) $entity->{$field_name}[$langcode];
  427. $delta_count = 0;
  428. foreach ($items as $delta => $item) {
  429. // We now know we have someting to insert.
  430. $do_insert = TRUE;
  431. $record = array(
  432. 'entity_type' => $entity_type,
  433. 'entity_id' => $id,
  434. 'revision_id' => $vid,
  435. 'bundle' => $bundle,
  436. 'delta' => $delta,
  437. 'language' => $langcode,
  438. );
  439. foreach ($field['columns'] as $column => $attributes) {
  440. $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
  441. }
  442. $query->values($record);
  443. if (isset($vid)) {
  444. $revision_query->values($record);
  445. }
  446. if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
  447. break;
  448. }
  449. }
  450. }
  451. // Execute the query if we have values to insert.
  452. if ($do_insert) {
  453. $query->execute();
  454. $revision_query->execute();
  455. }
  456. }
  457. }
  458. /**
  459. * Implements hook_field_storage_delete().
  460. *
  461. * This function deletes data for all fields for an entity from the database.
  462. */
  463. function field_sql_storage_field_storage_delete($entity_type, $entity, $fields) {
  464. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  465. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  466. if (isset($fields[$instance['field_id']])) {
  467. $field = field_info_field_by_id($instance['field_id']);
  468. field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
  469. }
  470. }
  471. }
  472. /**
  473. * Implements hook_field_storage_purge().
  474. *
  475. * This function deletes data from the database for a single field on
  476. * an entity.
  477. */
  478. function field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance) {
  479. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  480. $table_name = _field_sql_storage_tablename($field);
  481. $revision_name = _field_sql_storage_revision_tablename($field);
  482. db_delete($table_name)
  483. ->condition('entity_type', $entity_type)
  484. ->condition('entity_id', $id)
  485. ->execute();
  486. db_delete($revision_name)
  487. ->condition('entity_type', $entity_type)
  488. ->condition('entity_id', $id)
  489. ->execute();
  490. }
  491. /**
  492. * Implements hook_field_storage_query().
  493. */
  494. function field_sql_storage_field_storage_query(EntityFieldQuery $query) {
  495. if ($query->age == FIELD_LOAD_CURRENT) {
  496. $tablename_function = '_field_sql_storage_tablename';
  497. $id_key = 'entity_id';
  498. }
  499. else {
  500. $tablename_function = '_field_sql_storage_revision_tablename';
  501. $id_key = 'revision_id';
  502. }
  503. $table_aliases = array();
  504. $query_tables = NULL;
  505. // Add tables for the fields used.
  506. foreach ($query->fields as $key => $field) {
  507. $tablename = $tablename_function($field);
  508. $table_alias = _field_sql_storage_tablealias($tablename, $key, $query);
  509. $table_aliases[$key] = $table_alias;
  510. if ($key) {
  511. if (!isset($query_tables[$table_alias])) {
  512. $select_query->join($tablename, $table_alias, "$table_alias.entity_type = $field_base_table.entity_type AND $table_alias.$id_key = $field_base_table.$id_key");
  513. }
  514. }
  515. else {
  516. $select_query = db_select($tablename, $table_alias);
  517. // Store a reference to the list of joined tables.
  518. $query_tables =& $select_query->getTables();
  519. // Allow queries internal to the Field API to opt out of the access
  520. // check, for situations where the query's results should not depend on
  521. // the access grants for the current user.
  522. if (!isset($query->tags['DANGEROUS_ACCESS_CHECK_OPT_OUT'])) {
  523. $select_query->addTag('entity_field_access');
  524. }
  525. $select_query->addMetaData('base_table', $tablename);
  526. $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
  527. $field_base_table = $table_alias;
  528. }
  529. if ($field['cardinality'] != 1 || $field['translatable']) {
  530. $select_query->distinct();
  531. }
  532. }
  533. // Add field conditions. We need a fresh grouping cache.
  534. drupal_static_reset('_field_sql_storage_query_field_conditions');
  535. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldConditions, $table_aliases, '_field_sql_storage_columnname');
  536. // Add field meta conditions.
  537. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldMetaConditions, $table_aliases, '_field_sql_storage_query_columnname');
  538. if (isset($query->deleted)) {
  539. $select_query->condition("$field_base_table.deleted", (int) $query->deleted);
  540. }
  541. // Is there a need to sort the query by property?
  542. $has_property_order = FALSE;
  543. foreach ($query->order as $order) {
  544. if ($order['type'] == 'property') {
  545. $has_property_order = TRUE;
  546. }
  547. }
  548. if ($query->propertyConditions || $has_property_order) {
  549. if (empty($query->entityConditions['entity_type']['value'])) {
  550. throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
  551. }
  552. $entity_type = $query->entityConditions['entity_type']['value'];
  553. $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
  554. $query->entityConditions['entity_type']['operator'] = '=';
  555. foreach ($query->propertyConditions as $property_condition) {
  556. $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
  557. }
  558. }
  559. foreach ($query->entityConditions as $key => $condition) {
  560. $query->addCondition($select_query, "$field_base_table.$key", $condition);
  561. }
  562. // Order the query.
  563. foreach ($query->order as $order) {
  564. if ($order['type'] == 'entity') {
  565. $key = $order['specifier'];
  566. $select_query->orderBy("$field_base_table.$key", $order['direction']);
  567. }
  568. elseif ($order['type'] == 'field') {
  569. $specifier = $order['specifier'];
  570. $field = $specifier['field'];
  571. $table_alias = $table_aliases[$specifier['index']];
  572. $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
  573. $select_query->orderBy($sql_field, $order['direction']);
  574. }
  575. elseif ($order['type'] == 'property') {
  576. $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
  577. }
  578. }
  579. return $query->finishQuery($select_query, $id_key);
  580. }
  581. /**
  582. * Adds the base entity table to a field query object.
  583. *
  584. * @param SelectQuery $select_query
  585. * A SelectQuery containing at least one table as specified by
  586. * _field_sql_storage_tablename().
  587. * @param $entity_type
  588. * The entity type for which the base table should be joined.
  589. * @param $field_base_table
  590. * Name of a table in $select_query. As only INNER JOINs are used, it does
  591. * not matter which.
  592. *
  593. * @return
  594. * The name of the entity base table joined in.
  595. */
  596. function _field_sql_storage_query_join_entity(SelectQuery $select_query, $entity_type, $field_base_table) {
  597. $entity_info = entity_get_info($entity_type);
  598. $entity_base_table = $entity_info['base table'];
  599. $entity_field = $entity_info['entity keys']['id'];
  600. $select_query->join($entity_base_table, $entity_base_table, "$entity_base_table.$entity_field = $field_base_table.entity_id");
  601. return $entity_base_table;
  602. }
  603. /**
  604. * Adds field (meta) conditions to the given query objects respecting groupings.
  605. *
  606. * @param EntityFieldQuery $query
  607. * The field query object to be processed.
  608. * @param SelectQuery $select_query
  609. * The SelectQuery that should get grouping conditions.
  610. * @param condtions
  611. * The conditions to be added.
  612. * @param $table_aliases
  613. * An associative array of table aliases keyed by field index.
  614. * @param $column_callback
  615. * A callback that should return the column name to be used for the field
  616. * conditions. Accepts a field name and a field column name as parameters.
  617. */
  618. function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, SelectQuery $select_query, $conditions, $table_aliases, $column_callback) {
  619. $groups = &drupal_static(__FUNCTION__, array());
  620. foreach ($conditions as $key => $condition) {
  621. $table_alias = $table_aliases[$key];
  622. $field = $condition['field'];
  623. // Add the specified condition.
  624. $sql_field = "$table_alias." . $column_callback($field['field_name'], $condition['column']);
  625. $query->addCondition($select_query, $sql_field, $condition);
  626. // Add delta / language group conditions.
  627. foreach (array('delta', 'language') as $column) {
  628. if (isset($condition[$column . '_group'])) {
  629. $group_name = $condition[$column . '_group'];
  630. if (!isset($groups[$column][$group_name])) {
  631. $groups[$column][$group_name] = $table_alias;
  632. }
  633. else {
  634. $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
  635. }
  636. }
  637. }
  638. }
  639. }
  640. /**
  641. * Field meta condition column callback.
  642. */
  643. function _field_sql_storage_query_columnname($field_name, $column) {
  644. return $column;
  645. }
  646. /**
  647. * Implements hook_field_storage_delete_revision().
  648. *
  649. * This function actually deletes the data from the database.
  650. */
  651. function field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields) {
  652. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  653. if (isset($vid)) {
  654. foreach ($fields as $field_id) {
  655. $field = field_info_field_by_id($field_id);
  656. $revision_name = _field_sql_storage_revision_tablename($field);
  657. db_delete($revision_name)
  658. ->condition('entity_type', $entity_type)
  659. ->condition('entity_id', $id)
  660. ->condition('revision_id', $vid)
  661. ->execute();
  662. }
  663. }
  664. }
  665. /**
  666. * Implements hook_field_storage_delete_instance().
  667. *
  668. * This function simply marks for deletion all data associated with the field.
  669. */
  670. function field_sql_storage_field_storage_delete_instance($instance) {
  671. $field = field_info_field($instance['field_name']);
  672. $table_name = _field_sql_storage_tablename($field);
  673. $revision_name = _field_sql_storage_revision_tablename($field);
  674. db_update($table_name)
  675. ->fields(array('deleted' => 1))
  676. ->condition('entity_type', $instance['entity_type'])
  677. ->condition('bundle', $instance['bundle'])
  678. ->execute();
  679. db_update($revision_name)
  680. ->fields(array('deleted' => 1))
  681. ->condition('entity_type', $instance['entity_type'])
  682. ->condition('bundle', $instance['bundle'])
  683. ->execute();
  684. }
  685. /**
  686. * Implements hook_field_attach_rename_bundle().
  687. */
  688. function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  689. // We need to account for deleted or inactive fields and instances.
  690. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
  691. foreach ($instances as $instance) {
  692. $field = field_info_field_by_id($instance['field_id']);
  693. if ($field['storage']['type'] == 'field_sql_storage') {
  694. $table_name = _field_sql_storage_tablename($field);
  695. $revision_name = _field_sql_storage_revision_tablename($field);
  696. db_update($table_name)
  697. ->fields(array('bundle' => $bundle_new))
  698. ->condition('entity_type', $entity_type)
  699. ->condition('bundle', $bundle_old)
  700. ->execute();
  701. db_update($revision_name)
  702. ->fields(array('bundle' => $bundle_new))
  703. ->condition('entity_type', $entity_type)
  704. ->condition('bundle', $bundle_old)
  705. ->execute();
  706. }
  707. }
  708. }
  709. /**
  710. * Implements hook_field_storage_purge_field().
  711. *
  712. * All field data items and instances have already been purged, so all
  713. * that is left is to delete the table.
  714. */
  715. function field_sql_storage_field_storage_purge_field($field) {
  716. $table_name = _field_sql_storage_tablename($field);
  717. $revision_name = _field_sql_storage_revision_tablename($field);
  718. db_drop_table($table_name);
  719. db_drop_table($revision_name);
  720. }
  721. /**
  722. * Implements hook_field_storage_details().
  723. */
  724. function field_sql_storage_field_storage_details($field) {
  725. $details = array();
  726. if (!empty($field['columns'])) {
  727. // Add field columns.
  728. foreach ($field['columns'] as $column_name => $attributes) {
  729. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  730. $columns[$column_name] = $real_name;
  731. }
  732. return array(
  733. 'sql' => array(
  734. FIELD_LOAD_CURRENT => array(
  735. _field_sql_storage_tablename($field) => $columns,
  736. ),
  737. FIELD_LOAD_REVISION => array(
  738. _field_sql_storage_revision_tablename($field) => $columns,
  739. ),
  740. ),
  741. );
  742. }
  743. }

Functions

Namesort descending Description
field_sql_storage_field_attach_rename_bundle Implements hook_field_attach_rename_bundle().
field_sql_storage_field_storage_create_field Implements hook_field_storage_create_field().
field_sql_storage_field_storage_delete Implements hook_field_storage_delete().
field_sql_storage_field_storage_delete_field Implements hook_field_storage_delete_field().
field_sql_storage_field_storage_delete_instance Implements hook_field_storage_delete_instance().
field_sql_storage_field_storage_delete_revision Implements hook_field_storage_delete_revision().
field_sql_storage_field_storage_details Implements hook_field_storage_details().
field_sql_storage_field_storage_info Implements hook_field_storage_info().
field_sql_storage_field_storage_load Implements hook_field_storage_load().
field_sql_storage_field_storage_purge Implements hook_field_storage_purge().
field_sql_storage_field_storage_purge_field Implements hook_field_storage_purge_field().
field_sql_storage_field_storage_query Implements hook_field_storage_query().
field_sql_storage_field_storage_update_field Implements hook_field_storage_update_field().
field_sql_storage_field_storage_write Implements hook_field_storage_write().
field_sql_storage_field_update_forbid Implements hook_field_update_forbid().
field_sql_storage_help Implements hook_help().
_field_sql_storage_columnname Generate a column name for a field data table.
_field_sql_storage_indexname Generate an index name for a field data table.
_field_sql_storage_query_columnname Field meta condition column callback.
_field_sql_storage_query_field_conditions Adds field (meta) conditions to the given query objects respecting groupings.
_field_sql_storage_query_join_entity Adds the base entity table to a field query object.
_field_sql_storage_revision_tablename Generate a table name for a field revision archive table.
_field_sql_storage_schema Return the database schema for a field. This may contain one or more tables. Each table will contain the columns relevant for the specified field. Leave the $field's 'columns' and 'indexes' keys empty to get only the base schema.
_field_sql_storage_tablealias Generates a table alias for a field data table.
_field_sql_storage_tablename Generate a table name for a field data table.