-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfield_split.module
More file actions
115 lines (98 loc) · 3.7 KB
/
field_split.module
File metadata and controls
115 lines (98 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* @file
* Main file of Field Split module.
*/
/**
* Implements hook_help().
*/
function field_split_help($path, $arg) {
switch ($path) {
case 'admin/structure/field-split':
return '<p>' . t('This is the configuration form for Field Split module. Just open the specific fieldsets, enable the split functionality and select the amount of total fields.') . '</p>';
}
}
/**
* Implements hook_menu().
*/
function field_split_menu() {
$items = array();
$items['admin/structure/field-split'] = array(
'title' => 'Field Split',
'page callback' => 'drupal_get_form',
'page arguments' => array('field_split_config_form'),
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('administer site configuration'),
'file' => 'field_split.admin.inc',
);
return $items;
}
/**
* Implements hook_field_extra_fields().
*/
function field_split_field_extra_fields() {
$extra = array();
$entity_info = entity_get_info();
// Iterate all settings and create extra fields.
foreach ($entity_info as $entity_type => $info) {
foreach ($info['bundles'] as $bundle => $fields) {
$settings = variable_get('field_split_config_' . $entity_type . '__' . $bundle, array());
foreach ($settings as $field_name => $field_settings) {
if ($field_settings['enable']) {
$field_info = field_info_instance($entity_type, $field_name, $bundle);
$field_label = $field_info['label'];
for ($i = 1; $i < $field_settings['values']; $i++) {
$extra[$entity_type][$bundle]['display'][$field_name . '_' . $i] = array(
'label' => $field_label . ' #' . ($i + 1),
'weight' => $i + 20,
'type' => 'hidden',
);
}
}
}
}
}
return $extra;
}
/**
* Implements hook_entity_view().
*/
function field_split_entity_view($entity, $type, $view_mode, $langcode) {
list(, , $bundle) = entity_extract_ids($type, $entity);
$field_split_settings = variable_get('field_split_config_' . $type . '__' . $bundle, array());
$extra_fields = field_extra_fields_get_display($type, $bundle, $view_mode);
// Iterate all fields inside the bundle.
foreach ($field_split_settings as $field_name => $split_settings) {
// If the main field is not available in this entity, continue,
if (!isset($entity->{$field_name}) || (isset($entity->{$field_name}) && !count($entity->{$field_name})) || !isset($entity->content[$field_name])) {
continue;
}
$enabled = $split_settings['enable'];
$values = $split_settings['values'];
if ($enabled) {
for ($i = 1; $i < $values; $i++) {
$extra_field_name = $field_name . '_' . $i;
if (isset($extra_fields[$extra_field_name]) && isset($extra_fields[$extra_field_name]['visible']) && $extra_fields[$extra_field_name]['visible']) {
$entity->content[$extra_field_name] = $entity->content[$field_name];
// Remove unnecessary values from render array.
foreach (element_children($entity->content[$extra_field_name]) as $key) {
if ($key != $i) {
unset($entity->content[$extra_field_name][$key]);
}
}
// Move field value to '0' key.
if ($i != 0 && isset($entity->content[$field_name][$i])) {
$entity->content[$extra_field_name][0] = $entity->content[$field_name][$i];
unset($entity->content[$field_name][$i]);
}
// Finally remove all unnecessary field values in the main field.
foreach (element_children($entity->content[$extra_field_name]) as $key) {
if ($key != 0) {
unset($entity->content[$extra_field_name][$key]);
}
}
}
}
}
}
}