Commit ce17e244 authored by Dries's avatar Dries

- Patch #870292 by yched, sun: hook_field_extra_fields() results are not cached.

parent b2726296
......@@ -73,14 +73,12 @@ function hook_field_extra_fields() {
* @see hook_field_extra_fields()
*/
function hook_field_extra_fields_alter(&$info) {
// Force node title to always be at the top of the list
// by default.
// Force node title to always be at the top of the list by default.
foreach (node_type_get_types() as $bundle) {
if (isset($info['node'][$bundle]['title'])) {
$info['node'][$bundle]['title']['weight'] = -20;
}
}
}
/**
......
......@@ -225,6 +225,18 @@ function _field_info_collate_fields($reset = FALSE) {
// are thus not in $definitions['instances'].
$info['fields'][$instance['field_id']]['bundles'][$instance['entity_type']][] = $instance['bundle'];
}
// Populate 'extra_fields'.
$extra = module_invoke_all('field_extra_fields');
drupal_alter('field_extra_fields', $extra);
// Merge in saved settings.
foreach ($extra as $entity_type => $bundles) {
foreach ($bundles as $bundle => $extra_fields) {
$extra_fields = _field_info_prepare_extra_fields($extra_fields, $entity_type, $bundle);
$info['extra_fields'][$entity_type][$bundle] = $extra_fields;
}
}
cache_set('field_info_fields', $info, 'cache_field');
}
}
......@@ -370,6 +382,54 @@ function _field_info_prepare_instance_widget($field, $widget) {
return $widget;
}
/**
* Prepares 'extra fields' for the current run-time context.
*
* @param $extra_fields
* The array of extra fields, as collected in hook_field_extra_fields().
* @param $entity_type
* The entity type.
* @param $bundle
* The bundle name.
*/
function _field_info_prepare_extra_fields($extra_fields, $entity_type, $bundle) {
$entity_type_info = entity_get_info($entity_type);
$bundle_settings = field_bundle_settings($entity_type, $bundle);
$extra_fields += array('form' => array(), 'display' => array());
$result = array();
// Extra fields in forms.
foreach ($extra_fields['form'] as $name => $field_data) {
$settings = isset($bundle_settings['extra_fields']['form'][$name]) ? $bundle_settings['extra_fields']['form'][$name] : array();
if (isset($settings['weight'])) {
$field_data['weight'] = $settings['weight'];
}
$result['form'][$name] = $field_data;
}
// Extra fields in displayed entities.
$data = $extra_fields['display'];
foreach ($extra_fields['display'] as $name => $field_data) {
$settings = isset($bundle_settings['extra_fields']['display'][$name]) ? $bundle_settings['extra_fields']['display'][$name] : array();
$view_modes = array_merge(array('default'), array_keys($entity_type_info['view modes']));
foreach ($view_modes as $view_mode) {
if (isset($settings[$view_mode])) {
$field_data['display'][$view_mode] = $settings[$view_mode];
}
else {
$field_data['display'][$view_mode] = array(
'weight' => $field_data['weight'],
'visible' => TRUE,
);
}
}
unset($field_data['weight']);
$result['display'][$name] = $field_data;
}
return $result;
}
/**
* Determines the behavior of a widget with respect to an operation.
*
......
......@@ -399,7 +399,7 @@ function field_bundle_settings($entity_type, $bundle, $settings = NULL) {
$stored_settings[$entity_type][$bundle] = $settings;
variable_set('field_bundle_settings', $stored_settings);
drupal_static_reset('field_view_mode_settings');
drupal_static_reset('field_extra_fields');
field_info_cache_clear();
}
else {
$settings = isset($stored_settings[$entity_type][$bundle]) ? $stored_settings[$entity_type][$bundle] : array();
......@@ -501,54 +501,11 @@ function field_view_mode_settings($entity_type, $bundle) {
* The array of pseudo-field elements in the bundle.
*/
function field_extra_fields($entity_type, $bundle, $context) {
$extra = &drupal_static(__FUNCTION__);
if (!isset($extra)) {
$info = (array) module_invoke_all('field_extra_fields');
drupal_alter('field_extra_fields', $info);
// Merge in saved settings, and make sure we have settings for all view
// modes.
foreach ($info as $entity_type_name => $bundles) {
$entity_type_info = entity_get_info($entity_type_name);
foreach ($bundles as $bundle_name => $extra_fields) {
$bundle_settings = field_bundle_settings($entity_type_name, $bundle_name);
$extra_fields += array('form' => array(), 'display' => array());
// Extra fields in forms.
$data = $extra_fields['form'];
foreach ($data as $name => $field_data) {
$settings = isset($bundle_settings['extra_fields']['form'][$name]) ? $bundle_settings['extra_fields']['form'][$name] : array();
if (isset($settings['weight'])) {
$data[$name]['weight'] = $settings['weight'];
}
}
$extra[$entity_type_name][$bundle_name]['form'] = $data;
// Extra fields in displayed entities.
$data = $extra_fields['display'];
foreach ($data as $name => $field_data) {
$settings = isset($bundle_settings['extra_fields']['display'][$name]) ? $bundle_settings['extra_fields']['display'][$name] : array();
$view_modes = array_merge(array('default'), array_keys($entity_type_info['view modes']));
foreach ($view_modes as $view_mode) {
if (isset($settings[$view_mode])) {
$data[$name]['display'][$view_mode] = $settings[$view_mode];
}
else {
$data[$name]['display'][$view_mode] = array(
'weight' => $field_data['weight'],
'visible' => TRUE,
);
}
unset($data[$name]['weight']);
}
}
$extra[$entity_type_name][$bundle_name]['display'] = $data;
}
}
$info = _field_info_collate_fields();
if (isset($info['extra_fields'][$entity_type][$bundle][$context])) {
return $info['extra_fields'][$entity_type][$bundle][$context];
}
return isset($extra[$entity_type][$bundle][$context]) ? $extra[$entity_type][$bundle][$context] : array();
return array();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment