public function MenuLinkStorageController::save

Overrides DatabaseStorageController::save().

Overrides DatabaseStorageController::save

1 call to MenuLinkStorageController::save()
MenuLinkStorageController::preDelete in drupal/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php
Overrides DatabaseStorageController::preDelete().

File

drupal/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php, line 146
Contains \Drupal\menu_link\MenuLinkStorageController.

Class

MenuLinkStorageController
Controller class for menu links.

Namespace

Drupal\menu_link

Code

public function save(EntityInterface $entity) {

  // We return SAVED_UPDATED by default because the logic below might not
  // update the entity if its values haven't changed, so returning FALSE
  // would be confusing in that situation.
  $return = SAVED_UPDATED;
  $transaction = $this->database
    ->startTransaction();
  try {

    // Load the stored entity, if any.
    if (!$entity
      ->isNew() && !isset($entity->original)) {
      $entity->original = entity_load_unchanged($this->entityType, $entity
        ->id());
    }
    if ($entity
      ->isNew()) {
      $entity->mlid = $this->database
        ->insert($this->entityInfo['base_table'])
        ->fields(array(
        'menu_name' => 'tools',
      ))
        ->execute();
      $entity
        ->enforceIsNew();
    }

    // Unlike the save() method from DatabaseStorageController, we invoke the
    // 'presave' hook first because we want to allow modules to alter the
    // entity before all the logic from our preSave() method.
    $this
      ->invokeHook('presave', $entity);
    $this
      ->preSave($entity);

    // If every value in $entity->original is the same in the $entity, there
    // is no reason to run the update queries or clear the caches. We use
    // array_intersect_key() with the $entity as the first parameter because
    // $entity may have additional keys left over from building a router entry.
    // The intersect removes the extra keys, allowing a meaningful comparison.
    if ($entity
      ->isNew() || array_intersect_key(get_object_vars($entity), get_object_vars($entity->original)) != get_object_vars($entity->original)) {
      $return = drupal_write_record($this->entityInfo['base_table'], $entity, $this->idKey);
      if ($return) {
        if (!$entity
          ->isNew()) {
          $this
            ->resetCache(array(
            $entity->{$this->idKey},
          ));
          $this
            ->postSave($entity, TRUE);
          $this
            ->invokeHook('update', $entity);
        }
        else {
          $return = SAVED_NEW;
          $this
            ->resetCache();
          $entity
            ->enforceIsNew(FALSE);
          $this
            ->postSave($entity, FALSE);
          $this
            ->invokeHook('insert', $entity);
        }
      }
    }

    // Ignore slave server temporarily.
    db_ignore_slave();
    unset($entity->original);
    return $return;
  } catch (\Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception($this->entityType, $e);
    throw new EntityStorageException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}