You are here

public function PanelsPaneController::save in Fieldable Panels Panes (FPP) 7

Save the given FPP object.

Parameters

object $entity: An FPP object to save.

Return value

object|bool If the save operation completed correctly, this will be the updated FPP object, otherwise it will return FALSE and an exception will be logged in watchdog.

File

includes/PanelsPaneController.class.php, line 135
Contains the controller class for the Fieldable Panel Pane entity.

Class

PanelsPaneController
Entity controller class.

Code

public function save($entity) {
  $entity = (object) $entity;

  // Determine if we will be inserting a new entity.
  $entity->is_new = !(isset($entity->fpid) && is_numeric($entity->fpid));
  $transaction = db_transaction();

  // Load the stored entity, if any.
  if (!empty($entity->fpid) && !isset($entity->original)) {
    $entity->original = entity_load_unchanged('fieldable_panels_pane', $entity->fpid);
  }

  // Set the timestamp fields.
  if (empty($entity->created)) {
    $entity->created = REQUEST_TIME;
  }

  // Only change revision timestamp if it doesn't exist.
  if (empty($entity->timestamp)) {
    $entity->timestamp = REQUEST_TIME;
  }
  $entity->changed = REQUEST_TIME;
  field_attach_presave('fieldable_panels_pane', $entity);

  // Trigger hook_fieldable_panels_pane_presave().
  module_invoke_all('fieldable_panels_pane_presave', $entity);

  // Trigger hook_entity_presave_update().
  module_invoke_all('entity_presave', $entity, 'fieldable_panels_pane');

  // When saving a new entity revision, unset any existing $entity->vid
  // to ensure a new revision will actually be created and store the old
  // revision ID in a separate property for entity hook implementations.
  if (!$entity->is_new && !empty($entity->revision) && $entity->vid) {
    $entity->old_vid = $entity->vid;
    unset($entity->vid);
    $entity->timestamp = REQUEST_TIME;
  }
  try {

    // Since we already have an fpid, write the revision to ensure the vid is
    // the most up to date, then write the record.
    if (!$entity->is_new) {
      $this
        ->saveRevision($entity);
      drupal_write_record('fieldable_panels_panes', $entity, 'fpid');
      field_attach_update('fieldable_panels_pane', $entity);

      // Trigger hook_fieldable_panels_pane_update().
      module_invoke_all('fieldable_panels_pane_update', $entity);

      // Trigger hook_entity_update().
      module_invoke_all('entity_update', $entity, 'fieldable_panels_pane');
    }
    else {
      drupal_write_record('fieldable_panels_panes', $entity);
      $this
        ->saveRevision($entity);
      db_update('fieldable_panels_panes')
        ->fields(array(
        'vid' => $entity->vid,
      ))
        ->condition('fpid', $entity->fpid)
        ->execute();
      field_attach_insert('fieldable_panels_pane', $entity);

      // Trigger hook_fieldable_panels_pane_insert().
      module_invoke_all('fieldable_panels_pane_insert', $entity);

      // Trigger hook_entity_insert().
      module_invoke_all('entity_insert', $entity, 'fieldable_panels_pane');
    }

    // Clear the appropriate caches for this object.
    entity_get_controller('fieldable_panels_pane')
      ->resetCache(array(
      $entity->fpid,
    ));
    return $entity;
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('fieldable_panels_pane', $e);
  }
  return FALSE;
}