function properties_field_presave in Dynamic properties 7
Implements hook_field_presave().
Converts single item array in key 0 into multiple items, which are stored separately.
File
- ./
properties.module, line 254 - This module provides a dynamic property field that can contain an unlimited number of attribute value pairs.
Code
function properties_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
if (!empty($items[0]) && !isset($items[0]['attribute'])) {
$categories = $items[0];
$items = array();
// First step, move items into correct category. Necessary to be able to sort
// them withing their category.
foreach ($categories as $cname => $category) {
if (!is_array($category) || !is_array($category['properties'])) {
continue;
}
foreach ($category['properties'] as $pname => $property) {
if ($property['category'] != $cname) {
// Add to new category.
$categories[$property['category']]['properties'][$property['attribute']] = $property;
// Remove from current category.
unset($categories[$cname]['properties'][$property['attribute']]);
}
}
}
// Next step, order categories and attributes.
uasort($categories, '_field_sort_items_helper');
foreach ($categories as $category) {
if (empty($category['properties']) || !is_array($category['properties'])) {
continue;
}
$properties = $category['properties'];
uasort($properties, '_field_sort_items_helper');
foreach ($properties as $property) {
// If the attribute name was deleted, attribute is empty. Ignore these.
if (empty($property['attribute'])) {
continue;
}
// Add them in the sorted order to the items array.
$items[] = array(
'attribute' => $property['attribute'],
'value' => $property['value'],
'category' => $property['category'],
);
}
}
}
}