Created
August 16, 2019 03:20
-
-
Save chriscalip/b11f155611c296a9ddf0292144dbe4ee to your computer and use it in GitHub Desktop.
bulk data retrieval for drupal 8.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Utility get all field values | |
* @param string $entity_type Entity Type eg. (node,taxonomy) | |
* @param string $bundle Bundle (asset) | |
* @param string $field_name Field Name machine_name (field_asset_address) | |
* @param string $column_key $records keyed by column eg. (entity_id) | |
* @param array $columns Field columns eg. (value,uri,address_line1,locality) | |
* @return array $records index by entity_id AND delta | |
* | |
*/ | |
function cduxx_get_bulk_field_values(string $entity_type, string $bundle, string $field_name, string $column_key = 'entity_id', array $columns) { | |
$records = []; | |
// required db_columns | |
$db_columns = ['entity_id', 'delta']; | |
// make sure db columns ARE NOT inside columns. | |
$columns = array_values(array_diff($columns, $db_columns)); | |
$columns = array_unique($columns); | |
$table = $entity_type . '__' . $field_name; | |
$table_alias = substr($entity_type, 0, 1) . substr($field_name, 0, 1); | |
$query = db_select($table, $table_alias)->fields($table_alias, $db_columns); | |
foreach ($columns as $column) { | |
$field_alias = $column; | |
$table_column = $field_name . '_' . $column; | |
$query->addField($table_alias, $table_column, $field_alias); | |
} | |
$query->condition($table_alias . '.bundle', $bundle); | |
$query->orderBy($column_key, 'delta'); | |
$temps = $query->execute()->fetchAll(); | |
foreach ($temps as $temp) { | |
$records[$temp->entity_id][$temp->delta] = (array) $temp; | |
} | |
return $records; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment