|
@@ -464,6 +464,27 @@ function i18n_field_field_info_alter(&$field_info) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Prime the cache to avoid single db queries for entity fields / properties.
|
|
|
+ *
|
|
|
+ * This is mainly uses when large operations are occuring like a flush of the
|
|
|
+ * entity_property_infos().
|
|
|
+ */
|
|
|
+function i18n_field_prime_caches() {
|
|
|
+ global $language;
|
|
|
+ static $cache_primed;
|
|
|
+
|
|
|
+ // Fill the cache. This should avoid single db queries when filling the
|
|
|
+ // properties.
|
|
|
+ if (empty($cache_primed)) {
|
|
|
+ $cache_primed = TRUE;
|
|
|
+ $text_group = i18n_string_textgroup('field');
|
|
|
+ // Load all strings at once to avoid callbacks for each individual string.
|
|
|
+ $text_group->load_strings();
|
|
|
+ $text_group->multiple_translation_search(array('type' => '*', 'objectid' => '*', 'property' => '*'), $language->language);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
* Callback to translate entity property info for a fields.
|
|
|
*
|
|
|
* @see entity_metadata_field_entity_property_info()
|
|
@@ -485,6 +506,7 @@ function i18n_field_entity_property_callback(&$info, $entity_type, $field, $inst
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ i18n_field_prime_caches();
|
|
|
$name = $field['field_name'];
|
|
|
$property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
|
|
|
$property['label'] = i18n_field_translate_property($instance, 'label', $language->language);
|
|
@@ -498,9 +520,38 @@ function i18n_field_i18n_object_info_alter(&$info) {
|
|
|
if ($info = drupal_static('i18n_object_info')) {
|
|
|
// Clean static and permanent cache of the data and then re-run the property
|
|
|
// building.
|
|
|
- drupal_static_reset('entity_get_property_info');
|
|
|
- cache_clear_all('entity_property_info:' . $GLOBALS['language']->language, 'cache');
|
|
|
- entity_get_property_info();
|
|
|
+ // Use a lock to avoid stampeding.
|
|
|
+ $lock_name = 'i18n_field_entity_property_callback_fallback:' . $GLOBALS['language']->language;
|
|
|
+ // See if another request is already doing this. If so we bail out here as
|
|
|
+ // we won't help with anything at the moment.
|
|
|
+ if (!lock_may_be_available($lock_name)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (lock_acquire($lock_name)) {
|
|
|
+ i18n_field_prime_caches();
|
|
|
+ // Inject translated properties.
|
|
|
+ $entity_property_info = entity_get_property_info();
|
|
|
+ foreach ($entity_property_info as $entity_type => $properties) {
|
|
|
+ if (isset($properties['bundles'])) {
|
|
|
+ foreach ($properties['bundles'] as $bundle => $bundle_properties) {
|
|
|
+ if ($bundle_properties['properties']) {
|
|
|
+ foreach ($bundle_properties['properties'] as $bundle_property => $bundle_property_info) {
|
|
|
+ if ($instance = field_info_instance($entity_type, $bundle_property, $bundle)) {
|
|
|
+ $property = &$entity_property_info[$entity_type]['bundles'][$instance['bundle']]['properties'][$bundle_property];
|
|
|
+ $property['label'] = i18n_field_translate_property($instance, 'label', $GLOBALS['language']->language);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Inject into static cache.
|
|
|
+ $entity_get_property_info = &drupal_static('entity_get_property_info', array());
|
|
|
+ $entity_get_property_info = $entity_property_info;
|
|
|
+ // Write permanent cache.
|
|
|
+ cache_set('entity_property_info:' . $GLOBALS['language']->language, $entity_property_info);
|
|
|
+ lock_release($lock_name);
|
|
|
+ }
|
|
|
}
|
|
|
else {
|
|
|
watchdog('i18n_field', 'Unable to run fall-back handling for entity property translation due missing "i18n_object_info" cache', array(), WATCHDOG_WARNING);
|