View source
<?php
function field_weight_multiple_form_field_weight_display_overview_form_alter(&$form, &$form_state, $form_id) {
$node = $form_state['#node'];
$weight_instances_original = $weight_instances = $form['field_weight']['instances'] ? $form['field_weight']['instances']['#value'] : array();
$revision_weights = $form['field_weight']['revision_weights'];
$multi_fields = _field_weight_multiple_parse($weight_instances_original);
$form['field_weight']['multi_fields'] = array(
'#type' => 'value',
'#value' => array(),
);
$multi_fields_value =& $form['field_weight']['multi_fields']['#value'];
$field_weights = field_weight_multiple_get_weight($node->vid);
$revision_field_weights = field_weight_multiple_get_revision_weight($node->vid);
list($highest_weight, $weight_instances) = _field_weight_multiple_process_instances($form, $multi_fields_value, $field_weights, $revision_field_weights, $weight_instances_original, $multi_fields, $node, $weight_instances, $revision_weights);
$context = array(
'highest weight' => &$highest_weight,
'original weights' => $field_weights,
'revision weights' => $revision_field_weights,
'form reference' => &$multi_fields_value,
'original instances' => $weight_instances_original,
'field names' => $multi_fields,
'node' => $node,
);
drupal_alter('field_weight_multiple_display_overview_weights', $weight_instances, $form, $context);
$weight_delta = $highest_weight;
$form['field_weight']['instances'] = array(
'#type' => 'value',
'#value' => $weight_instances,
);
$instances = $weight_instances;
$original_instances = field_info_instances('node', $node->type);
foreach ($weight_instances as $field => $values) {
if (isset($instances[$field]) && !isset($instances[$field]['label'])) {
$instances[$field]['label'] = $original_instances[$field]['label'];
}
$form['field_weight'][$field]['field'] = array(
'#markup' => (isset($values['new']) && $values['new'] ? t("<strong>(unsaved)</strong>") . ' ' : '') . check_plain($instances[$field]['label']),
);
$form['field_weight'][$field]['weight'] = array(
'#type' => 'weight',
'#delta' => $weight_delta,
'#default_value' => isset($values['weight']) ? $values['weight'] : 0,
'#attributes' => array(
'class' => array(
'field-weight',
),
),
);
$form['field_weight'][$field]['hidden'] = array(
'#type' => 'checkbox',
'#default_value' => isset($values['hidden']) ? $values['hidden'] : 0,
);
}
array_unshift($form['#submit'], '_field_weight_multiple_before_field_weight_display_overview_form_submit');
array_unshift($form['continue']['#submit'], '_field_weight_multiple_before_field_weight_display_overview_form_submit');
$form['#submit'][] = '_field_weight_multiple_after_field_weight_display_overview_form_submit';
$form['continue']['#submit'][] = '_field_weight_multiple_after_field_weight_display_overview_form_submit';
$form['reset']['#submit'][] = '_field_weight_multiple_remove_weights';
}
function _field_weight_multiple_process_instances(&$form, &$multi_fields_value, $field_weights, $revision_field_weights, $weight_instances_original, $multi_fields, $node, $weight_instances, $revision_weights) {
$highest_weight = count($weight_instances_original);
foreach ($multi_fields as $multi_field) {
$node_multi_field = field_get_items('node', $node, $multi_field);
if ($node_multi_field === FALSE) {
$node_multi_field = array();
}
if (!empty($weight_instances[$multi_field])) {
$multi_fields_value[$multi_field] = $weight_instances[$multi_field];
unset($form['field_weight'][$multi_field]);
unset($weight_instances[$multi_field]);
}
foreach ($node_multi_field as $delta => $node_multi_field_data) {
if (isset($field_weights["{$multi_field}_{$delta}"])) {
$weight_instances["{$multi_field}_{$delta}"] = $field_weights["{$multi_field}_{$delta}"];
if (!isset($revision_field_weights["{$multi_field}_{$delta}"])) {
$weight_instances["{$multi_field}_{$delta}"]['new'] = TRUE;
}
}
else {
if ($delta == "0" && isset($weight_instances[$multi_field])) {
$weight_instances["{$multi_field}_{$delta}"] = array(
'weight' => $weight_instances_original[$multi_field]['weight'],
'hidden' => $weight_instances_original[$multi_field]['hidden'],
);
}
else {
$weight_instances["{$multi_field}_{$delta}"] = array(
'weight' => "0",
'hidden' => 0,
'new' => TRUE,
);
}
}
$highest_weight = max($highest_weight, $weight_instances["{$multi_field}_{$delta}"]['weight']);
$multi_field_info_instance = field_info_instance('node', $multi_field, $node->type);
$human_delta = $delta + 1;
$weight_instances["{$multi_field}_{$delta}"]['label'] = "{$multi_field_info_instance['label']} (#{$human_delta})";
$weight_instances["{$multi_field}_{$delta}"]['field_name'] = $multi_field;
$weight_instances["{$multi_field}_{$delta}"]['module'] = 'field_weight_multiple';
}
}
uasort($weight_instances, 'drupal_sort_weight');
return array(
$highest_weight,
$weight_instances,
);
}
function _field_weight_multiple_parse($weight_instances) {
$multi_fields = array();
foreach ($weight_instances as $field_name => $field) {
$field_info = field_info_field($field_name);
if ($field_info && $field_info['cardinality'] > 1 || $field_info['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
$multi_fields[] = $field_name;
}
}
return $multi_fields;
}
function _field_weight_multiple_before_field_weight_display_overview_form_submit(&$form, &$form_state) {
$node = $form_state['#node'];
$values =& $form_state['values']['field_weight'];
$form_state['values']['field_weight_multiple'] = array();
$our_values =& $form_state['values']['field_weight_multiple'];
$old_multi_fields =& $values['multi_fields'];
foreach ($old_multi_fields as $old_multi_field => $old_multi_field_value) {
$node_old_multi_field_items = field_get_items('node', $node, $old_multi_field);
$values[$old_multi_field] = $old_multi_field_value;
if ($node_old_multi_field_items) {
foreach ($node_old_multi_field_items as $delta => $node_old_multi_field) {
$our_values["{$old_multi_field}_{$delta}"] = $values["{$old_multi_field}_{$delta}"];
unset($values["{$old_multi_field}_{$delta}"]);
}
}
}
}
function _field_weight_multiple_after_field_weight_display_overview_form_submit(&$form, &$form_state) {
$node = $form_state['#node'];
$values = $form_state['values']['field_weight_multiple'];
$instances = array_keys($values);
$weights = array();
foreach ($instances as $field) {
$weights[$field] = array(
'weight' => $values[$field]['weight'],
'hidden' => $values[$field]['hidden'],
);
}
$empty_check = array_filter($weights);
if (!empty($empty_check)) {
db_merge('field_weight_multiple')
->key(array(
'vid' => $node->vid,
))
->fields(array(
'nid' => $node->nid,
'vid' => $node->vid,
'type' => $node->type,
'field_weights' => serialize($weights),
))
->execute();
}
elseif (empty($empty_check)) {
_field_weight_multiple_remove_weights($form, $form_state);
}
}
function field_weight_multiple_get_weight($vid) {
$weights = field_weight_multiple_get_revision_weight($vid);
if (empty($weights)) {
}
return $weights;
}
function field_weight_multiple_get_revision_weight($vid) {
$result = db_select('field_weight_multiple', 'fwm')
->fields('fwm', array(
'field_weights',
))
->condition('vid', $vid)
->execute()
->fetchField();
$weights = unserialize($result);
if (!$weights) {
$node = node_load(NULL, $vid);
$node_result = db_select('field_weight_multiple', 'fwm')
->fields('fwm', array(
'field_weights',
))
->condition('nid', $node->nid)
->execute()
->fetchField();
}
$weights = unserialize($node_result);
return $weights;
}
function _field_weight_multiple_remove_weights(&$form, &$form_state) {
$node = $form_state['#node'];
db_delete('field_weight_multiple')
->condition('vid', $node->vid)
->execute();
}
function field_weight_multiple_node_delete($node) {
db_delete('field_weight_multiple')
->condition('nid', $node->nid)
->execute();
}
function field_weight_multiple_node_revision_delete($node) {
db_delete('field_weight_multiple')
->condition('vid', $node->vid)
->execute();
}
function field_weight_multiple_node_insert($node) {
if (isset($node->clone_from_original_nid)) {
$clone_source = node_load($node->clone_from_original_nid);
if ($clone_source) {
$field_weights = field_weight_multiple_get_weight($clone_source->vid);
}
if ($field_weights) {
db_merge('field_weight_multiple')
->key(array(
'vid' => $node->vid,
))
->fields(array(
'nid' => $node->nid,
'vid' => $node->vid,
'type' => $node->type,
'field_weights' => serialize($field_weights),
))
->execute();
}
}
}
function field_weight_multiple_node_update($node) {
if ($node->original->vid !== $node->vid) {
$field_weights = field_weight_multiple_get_weight($node->original->vid);
if ($field_weights) {
db_merge('field_weight_multiple')
->key(array(
'vid' => $node->vid,
))
->fields(array(
'nid' => $node->nid,
'vid' => $node->vid,
'type' => $node->type,
'field_weights' => serialize($field_weights),
))
->execute();
}
}
}
function field_weight_multiple_entity_view_alter(&$build, $type) {
if ($type == 'node') {
$enabled_node_types = variable_get('field_weight_node_types', array());
if (in_array($build['#bundle'], $enabled_node_types, TRUE)) {
$multi_weights = field_weight_multiple_get_weight($build['#node']->vid);
if ($multi_weights) {
$fields = _field_weight_multiple_parse(field_info_instances('node', $build['#node']->type));
foreach ($fields as $field) {
if (isset($build[$field])) {
$field_structure = $build[$field];
$node_multi_items = $build[$field]['#items'];
foreach ($node_multi_items as $delta => $node_multi) {
$build["{$field}_{$delta}"] = $field_structure;
$new_item =& $build["{$field}_{$delta}"];
foreach (element_children($new_item) as $element_delta) {
if ($element_delta !== $delta) {
unset($new_item[$element_delta]);
unset($new_item['#items'][$element_delta]);
}
}
}
unset($build[$field]);
}
}
foreach ($multi_weights as $key => $values) {
$build[$key]['#weight'] = $values['weight'];
if ($values['hidden'] == TRUE) {
$build[$key]['#access'] = FALSE;
}
}
}
}
}
}