| 7 field.crud.inc | field_read_instances($params = array(), $include_additional = array()) |
| 8 field.crud.inc | field_read_instances($params = array(), $include_additional = array()) |
Reads in field instances that match an array of conditions.
Parameters
$param: An array of properties to use in selecting a field instance. Valid keys include any column of the field_config_instance table. If NULL, all instances will be returned.
$include_additional: The default behavior of this function is to not return field instances that have been marked deleted, or whose field is inactive. Setting $include_additional['include_inactive'] or $include_additional['include_deleted'] to TRUE will override this behavior.
Return value
An array of instances matching the arguments.
Related topics
13 calls to field_read_instances()
File
- modules/
field/ field.crud.inc, line 681 - Field CRUD API, handling field and field instance creation and deletion.
Code
function field_read_instances($params = array(), $include_additional = array()) {
$include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive'];
$include_deleted = isset($include_additional['include_deleted']) && $include_additional['include_deleted'];
$query = db_select('field_config_instance', 'fci', array('fetch' => PDO::FETCH_ASSOC));
$query->join('field_config', 'fc', 'fc.id = fci.field_id');
$query->fields('fci');
// Turn the conditions into a query.
foreach ($params as $key => $value) {
$query->condition('fci.' . $key, $value);
}
if (!$include_inactive) {
$query
->condition('fc.active', 1)
->condition('fc.storage_active', 1);
}
if (!$include_deleted) {
$query->condition('fc.deleted', 0);
$query->condition('fci.deleted', 0);
}
$instances = array();
$results = $query->execute();
foreach ($results as $record) {
// Filter out instances on unknown entity types (for instance because the
// module exposing them was disabled).
$entity_info = entity_get_info($record['entity_type']);
if ($include_inactive || $entity_info) {
$instance = unserialize($record['data']);
$instance['id'] = $record['id'];
$instance['field_id'] = $record['field_id'];
$instance['field_name'] = $record['field_name'];
$instance['entity_type'] = $record['entity_type'];
$instance['bundle'] = $record['bundle'];
$instance['deleted'] = $record['deleted'];
module_invoke_all('field_read_instance', $instance);
$instances[] = $instance;
}
}
return $instances;
}
Login or register to post comments
Comments
Brief example
I was looking to retrieve all the fields of a content type but could not find an API for that, eventually I found this one. The value of bundle in the params array is the machine-readable name of the content type which in my case is 'product'
$params = array('entity_type' => 'node', 'bundle' => 'product');$fields = field_read_instances($params);
If someone finds a better way to retrieve the fields of a content type, please let us know.
Try this
field_info_instances($entity_type, $bundle)
There's no hook to alter a field instance =(
What's the point of this being a hook when you can't alter any pass through? The same goes for
field_read_instance. I was looking for a way to add a few properties to a field instance, but there's no hook that can alter a field after it's fetched and before it's cached.@ rudiedirkx - You can use
@ rudiedirkx - You can use
hook_field_storage_details_alterto alter some properties of a field./*** Implements hook_field_storage_details_alter().
*/
function mymodule_field_storage_details_alter(&$details, &$field) {
if ($field['field_name'] == 'media_gallery_columns') {
$field['locked'] = 0;
}
}