summaryrefslogtreecommitdiff
path: root/modules/field
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2010-09-11 00:03:42 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2010-09-11 00:03:42 +0000
commit7cc3d92d65cccf7e09943312b7fa28642372d4c9 (patch)
tree0bc09ca5d76dc48f67935304bb1a9c24c0ea8b73 /modules/field
parent3b21e82a3770c61ad64062204c33911c42e19d7e (diff)
downloadbrdo-7cc3d92d65cccf7e09943312b7fa28642372d4c9.tar.gz
brdo-7cc3d92d65cccf7e09943312b7fa28642372d4c9.tar.bz2
#616240 follow-up by yched, dereine, marvil07: Make Field UI screens extensible from contrib - part II.
Diffstat (limited to 'modules/field')
-rw-r--r--modules/field/field.api.php27
-rw-r--r--modules/field/field.crud.inc24
-rw-r--r--modules/field/field.info.inc41
3 files changed, 72 insertions, 20 deletions
diff --git a/modules/field/field.api.php b/modules/field/field.api.php
index 9b2d3bb4f..2b99c2309 100644
--- a/modules/field/field.api.php
+++ b/modules/field/field.api.php
@@ -2066,6 +2066,33 @@ function hook_field_storage_pre_update($entity_type, $entity, &$skip_fields) {
}
/**
+ * Returns the maximum weight for the entity components handled by the module.
+ *
+ * Field API takes care of fields and 'extra_fields'. This hook is intended for
+ * third-party modules adding other entity components (e.g. field_group).
+ *
+ * @param $entity_type
+ * The type of entity; e.g. 'node' or 'user'.
+ * @param $bundle
+ * The bundle name.
+ * @param $context
+ * The context for which the maximum weight is requested. Either 'form', or
+ * the name of a view mode.
+ * @return
+ * The maximum weight of the entity's components, or NULL if no components
+ * were found.
+ */
+function hook_field_info_max_weight($entity_type, $bundle, $context) {
+ $weights = array();
+
+ foreach (my_module_entity_additions($entity_type, $bundle, $context) as $addition) {
+ $weights[] = $addition['weight'];
+ }
+
+ return $weights ? max($weights) : NULL;
+}
+
+/**
* Alters the display settings of a field before it gets displayed.
*
* Note that instead of hook_field_display_alter(), which is called for all
diff --git a/modules/field/field.crud.inc b/modules/field/field.crud.inc
index 96e44bf0e..b9f38814d 100644
--- a/modules/field/field.crud.inc
+++ b/modules/field/field.crud.inc
@@ -761,16 +761,8 @@ function _field_write_instance($instance, $update = FALSE) {
);
// If no weight specified, make sure the field sinks at the bottom.
if (!isset($instance['widget']['weight'])) {
- $weights = array();
- foreach (field_info_instances($instance['entity_type'], $instance['bundle']) as $existing_instance) {
- if ($instance['field_name'] != $existing_instance['field_name']) {
- $weights[] = $existing_instance['widget']['weight'];
- }
- }
- foreach (field_info_extra_fields($instance['entity_type'], $instance['bundle'], 'form') as $extra) {
- $weights[] = $extra['weight'];
- }
- $instance['widget']['weight'] = $weights ? max($weights) + 1 : 0;
+ $max_weight = field_info_max_weight($instance['entity_type'], $instance['bundle'], 'form');
+ $instance['widget']['weight'] = !is_null($max_weight) ? $max_weight + 1 : 0;
}
// Check widget module.
$widget_type = field_info_widget_types($instance['widget']['type']);
@@ -795,16 +787,8 @@ function _field_write_instance($instance, $update = FALSE) {
}
// If no weight specified, make sure the field sinks at the bottom.
if (!isset($display['weight'])) {
- $weights = array();
- foreach (field_info_instances($instance['entity_type'], $instance['bundle']) as $existing_instance) {
- if ($instance['field_name'] != $existing_instance['field_name']) {
- $weights[] = $existing_instance['display'][$view_mode]['weight'];
- }
- }
- foreach (field_info_extra_fields($instance['entity_type'], $instance['bundle'], 'display') as $extra) {
- $weights[] = $extra['display'][$view_mode]['weight'];
- }
- $display['weight'] = $weights ? max($weights) + 1 : 0;
+ $max_weight = field_info_max_weight($instance['entity_type'], $instance['bundle'], $view_mode);
+ $display['weight'] = !is_null($max_weight) ? $max_weight + 1 : 0;
}
$instance['display'][$view_mode] = $display;
}
diff --git a/modules/field/field.info.inc b/modules/field/field.info.inc
index fbc52528e..f435d99a6 100644
--- a/modules/field/field.info.inc
+++ b/modules/field/field.info.inc
@@ -761,6 +761,47 @@ function field_info_extra_fields($entity_type, $bundle, $context) {
}
/**
+ * Returns the maximum weight of all the components in an entity.
+ *
+ * This includes fields, 'extra_fields', and other components added by
+ * third-party modules (e.g. field_group).
+ *
+ * @param $entity_type
+ * The type of entity; e.g. 'node' or 'user'.
+ * @param $bundle
+ * The bundle name.
+ * @param $context
+ * The context for which the maximum weight is requested. Either 'form', or
+ * the name of a view mode.
+ * @return
+ * The maximum weight of the entity's components, or NULL if no components
+ * were found.
+ */
+function field_info_max_weight($entity_type, $bundle, $context) {
+ $weights = array();
+
+ // Collect weights for fields.
+ foreach (field_info_instances($entity_type, $bundle) as $instance) {
+ if ($context == 'form') {
+ $weights[] = $instance['widget']['weight'];
+ }
+ else {
+ $weights[] = $instance['display'][$context]['weight'];
+ }
+ }
+ // Collect weights for extra fields.
+ foreach (field_info_extra_fields($entity_type, $bundle, $context) as $extra) {
+ $weights[] = $extra['weight'];
+ }
+
+ // Let other modules feedback about their own additions.
+ $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $entity_type, $bundle, $context));
+ $max_weight = $weights ? max($weights) : NULL;
+
+ return $max_weight;
+}
+
+/**
* Returns a field type's default settings.
*
* @param $type