You are here

function oa_related_field_attach_presave in Open Atrium Related Content 7.2

Implements hook_field_attach_presave().

File

./oa_related.module, line 514

Code

function oa_related_field_attach_presave($entity_type, $entity) {

  // check paragraph entities that use entityreferences with revisions
  if ($entity_type == 'paragraphs_item') {
    if (isset($entity->field_paragraph_lock_revision)) {
      $lock = field_get_items('paragraphs_item', $entity, 'field_paragraph_lock_revision');

      // only process entities that have the Lock Revision option field
      if (isset($lock)) {
        foreach ($entity as $field_name => $field) {

          // loop through entity looking for fields that have revision info
          if (is_array($field) && isset($field[LANGUAGE_NONE][0]['revision_id'])) {
            if (!empty($lock[0]['value'])) {

              // to lock the revision, get the previously saved revision value
              $original_items = isset($entity->original) ? (array) field_get_items($entity_type, $entity->original, $field_name) : array();
              $revision_id = !empty($field[LANGUAGE_NONE][0]['revision_id']) ? $field[LANGUAGE_NONE][0]['revision_id'] : NULL;
              $revision_id = !empty($original_items[0]['revision_id']) ? $original_items[0]['revision_id'] : $revision_id;
              if ($revision_id) {
                $entity->{$field_name}[LANGUAGE_NONE][0]['revision_id'] = $revision_id;
              }
            }
            else {

              // to unlock the revision, just remove it
              unset($entity->{$field_name}[LANGUAGE_NONE][0]['revision_id']);
            }
          }
        }
      }
    }
  }
}