function _office_hours_update_7100_daynum in Office Hours 7
Helper function for hook_update_N.
Change value of 'day' column from 0-13 range to normal 0-6 day range. This allows for more then 2 hours blocks per day.
1 call to _office_hours_update_7100_daynum()
- office_hours_update_7100 in ./
office_hours.install - Change value of 'day' column from 0-13 range to normal 0-6 day range.
File
- ./
office_hours.install, line 87 - Install, update and uninstall functions for the Office hours module.
Code
function _office_hours_update_7100_daynum() {
// Step 1: Collect field_ids for office hours fields.
$ids = array();
$fields = field_read_fields();
foreach ($fields as $field) {
if ($field['type'] == 'office_hours') {
$ids[] = $field['id'];
}
}
if (empty($ids)) {
return;
}
// Step 2: Collect fields of type 'office_hours' via id.
$oh_fields = array();
$instances = field_info_instances();
foreach ($instances as $entity => $bundles) {
foreach ($bundles as $bundle => $fields) {
foreach ($fields as $field_name => $field) {
// Check each field for the correct $field_id
foreach ($ids as $key => $id) {
if ($field['field_id'] == $id) {
$oh_fields[$field['id']] = $field;
}
}
}
}
}
// Step 3: Update entities.
foreach ($oh_fields as $id => $field) {
$entity_type = $field['entity_type'];
$bundle = $field['bundle'];
$field_id = $field['field_id'];
$field_name = $field['field_name'];
unset($entities);
$query = new EntityFieldQuery();
$entities = $query
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', $bundle)
->execute();
// Remove extra layer from data structure.
$entities = $entities[$entity_type];
ksort($entities);
// We may run in time problems. Allow restart using a variable.
$id = -1;
$latest_id = variable_get('office_hours-' . $entity_type . '-' . $bundle . '-' . $field_id, $id);
// Convert the 'day' column to from 0-13 to 0-6 day range.
foreach ($entities as $id => $entity) {
if ($id > $latest_id) {
// Non-node Entity types (like 'taxonomy_term') need enhancing.
// Copied from entity_create_stub_entity() (removed in D8).
$info = entity_get_info($entity_type);
// $entity->{$info['entity keys']['id']} = $entity->id;
if (!empty($info['entity keys']['revision']) && isset($entity->vid)) {
$entity->{$info['entity keys']['revision']} = $entity->vid;
}
if (!empty($info['entity keys']['bundle']) && isset($bundle)) {
$entity->{$info['entity keys']['bundle']} = $bundle;
}
$full_entities = array(
$id => $entity,
);
field_attach_load($entity_type, $full_entities, FIELD_LOAD_CURRENT, array(
'field_id' => $field_id,
));
$empty = TRUE;
foreach ($full_entities as $id => $full_entity) {
$entity_field =& $full_entity->{$field_name};
foreach ($entity_field as $langcode => &$items) {
foreach ($items as $index => &$item) {
$empty = FALSE;
$item['day'] = (int) ($item['day'] / 2);
}
}
}
// In order to avoid time-outs, only update objects with changed data.
if (!$empty) {
// Prevent core-error #985642, which should be fixed per D7.22
$full_entity->original = $full_entity;
// field_attach_presave($entity_type, $full_entity); // This unnecessarily shifts the days.
field_attach_update($entity_type, $full_entity);
variable_set('office_hours-' . $entity_type . '-' . $bundle . '-' . $field_id, $id);
}
}
}
}
// If we did not break, remove variables.
foreach ($oh_fields as $id => $field) {
$entity_type = $field['entity_type'];
$bundle = $field['bundle'];
$field_id = $field['field_id'];
$field_name = $field['field_name'];
variable_del('office_hours-' . $entity_type . '-' . $bundle . '-' . $field_id);
}
}