From ece6fb0ec3f5852fa45cf5d6b4abf5ef4e08d697 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Wed, 22 Nov 2017 13:01:06 -0500 Subject: [PATCH] Implement hook_farm_constraint() on behalf of all entity and term reference fields. --- .../farm_constraint.farm_constraint.inc | 87 +++++++++++++++++++ .../farm/farm_constraint/farm_constraint.info | 1 + 2 files changed, 88 insertions(+) create mode 100644 modules/farm/farm_constraint/farm_constraint.farm_constraint.inc diff --git a/modules/farm/farm_constraint/farm_constraint.farm_constraint.inc b/modules/farm/farm_constraint/farm_constraint.farm_constraint.inc new file mode 100644 index 00000000..41d37868 --- /dev/null +++ b/modules/farm/farm_constraint/farm_constraint.farm_constraint.inc @@ -0,0 +1,87 @@ + array( + 'farm_asset', + 'log', + 'user', + ), + 'taxonomy_term_reference' => array( + 'taxonomy_term', + ), + ); + + // Get information about all field instances. + $instances = field_info_field_map(); + + // Iterate through the instances. + foreach ($instances as $field_name => $instance) { + + // If the field type is not one of the ones we care about, skip it. + if (!array_key_exists($instance['type'], $field_types)) { + continue; + } + + // If the entity type does not match the field type, skip it. + if (!in_array($type, $field_types[$instance['type']])) { + continue; + } + + // Load the field info. + $field_info = field_info_field($field_name); + + // If this is an entityreference field, and the entity type does not match + // the field target type, skip it. + if (($instance['type'] == 'entityreference') && ($type != $field_info['settings']['target_type'])) { + continue; + } + + // Get the database storage details. + $storage_details = $field_info['storage']['details']; + + // This only works with SQL, so skip if that information isn't available. + if (empty($storage_details['sql']['FIELD_LOAD_CURRENT'])) { + continue; + } + + // Iterate through the database tables and column information. There should + // only be one of each, so collect information about it. + $table = ''; + $column = ''; + foreach ($storage_details['sql']['FIELD_LOAD_CURRENT'] as $table_name => $data) { + $table = $table_name; + foreach ($data as $key => $column_name) { + $column = $column_name; + } + } + + // If a table and column are not found, skip. + if (empty($table) || empty($column)) { + continue; + } + + // Finally, query the table to see if there are any references to this + // entity. + $references = db_query('SELECT COUNT(entity_id) FROM {' . $table . '} WHERE ' . $column . ' = :id', array(':id' => $id))->fetchField(); + + // If references were found, a constraint was detected! Return TRUE. + if (!empty($references)) { + return TRUE; + } + } +} + diff --git a/modules/farm/farm_constraint/farm_constraint.info b/modules/farm/farm_constraint/farm_constraint.info index cfa7852d..45642e87 100644 --- a/modules/farm/farm_constraint/farm_constraint.info +++ b/modules/farm/farm_constraint/farm_constraint.info @@ -2,3 +2,4 @@ name = Farm Constraint description = Provides a framework for managing foreign key constraints between farmOS entities. core = 7.x package = farmOS +dependencies[] = field