CRUD functions

Last updated on
30 April 2025

The Subs CRUD API includes the following functions:


subs_load

/**
 * Fetch a subscription object.
 *
 * @param $sid
 *   Integer specifying the subscription id.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   A fully-loaded $subscription object or FALSE if it cannot be loaded.
 *
 * @see subs_load_multiple()
 */
function subs_load($sid, $reset = FALSE) {
  $subscriptions = subs_load_multiple(array($sid), array(), $reset);
  return reset($subscriptions);
}

Back to top


subs_load_multiple

/**
 * Load multiple subscriptions based on certain conditions.
 *
 * @param $sids
 *   An array of subscription IDs.
 * @param $conditions
 *   An array of conditions to match against the {subs} table.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   An array of subscription objects, indexed by ID.
 *
 * @see entity_load()
 * @see subs_load()
 * @see subs_load_by_user()
 */
function subs_load_multiple($sids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('subs', $sids, $conditions, $reset);
}

Back to top


subs_load_by_user

/**
 * Fetch subscriptions by account.
 *
 * @param $account
 *   The user account to load subscriptions for, or its uid.
 * @param $type_name
 *   To load subscriptions of a single type, specify it.
 * @param $reset
 *   To bypass the static cache set this to true.
 * @return
 *   Either a single subscriptionor an array of subscriptions keyed by subscription type.
 *
 * @see subs_load_multiple()
 */
function subs_load_by_user($account, $type_name = NULL, $reset = FALSE) {
  // Use a separate query to determine all subscription ids per user and cache them.
  // That way we can look up subscriptions by id and benefit from the static cache
  // of the entity loader.
  $cache = &drupal_static(__FUNCTION__, array());
  $uid = is_object($account) ? $account->uid : $account;

  if (!isset($cache[$uid]) || $reset) {
    if (empty($type_name)) {
      $subscriptions = subs_load_multiple(FALSE, array('uid' => $uid));
      // Cache ids for further lookups.
      $cache[$uid] = array();
      foreach ($subscriptions as $sid => $subscription) {
        $cache[$uid][] = $sid;
      }
      return $subscriptions;
    }
    $cache[$uid] = db_select('subs', 's')
      ->fields('s', array('sid'))
      ->condition('uid', $uid)
      ->execute()
      ->fetchAllKeyed();
  }

  if (isset($type_name)) {
    // Manually filter, as array_filter() can't take additional parameters.
    $subscriptions_filtered = array();

    foreach ($cache[$uid] as $sid) {
      if ($subscription = subs_load($sid)) {
        if ($subscription->type === $type_name) {
          $subscriptions_filtered[$sid] = $subscription;
        }
      }
    }

    return $subscriptions_filtered;
  }

  // Return an array containing subscriptions keyed by subscription id.
  return subs_load_multiple($cache[$uid]);
}

Back to top


subs_delete

/**
 * Deletes a subscription.
 * Set it to cancelled before to trigger any relevant events.
 *
 * @param Subs
 *   The subscription object.
 */
function subs_delete(Subs $subscription) {
  subs_set_cancelled($subscription);
  $subscription->delete();
}

Back to top


subs_delete_multiple

/**
 * Delete multiple subscriptions.
 *
 * @param array
 *   An array of subscription IDs.
 */
function subs_delete_multiple(array $sids) {
  // Bypass the traditional entity_get_controller() approach because we
  // need to cancel each subscription before its deleted.
  foreach ($sids as $sid) {
    if ($subscription = subs_load($sid)) {
      subs_delete($subscription);
    }
  }
}

Back to top


subs_create

/**
 * Create a new subscription object.
 *
 * @param $values
 *   An array of values to initialize the subscription with.
 * @return Subs
 *   The newly-created subscription object.
 */
function subs_create(array $values) {
  return new Subs($values);
}

Back to top


subs_save

/**
 * Saves a subscription.
 *
 * @param $subscription
 *   The subscription object.
 * @return boolean
 *   Whether the subscription was saved successfully.
 */
function subs_save(Subs $subscription) {
  return $subscription->save();
}

Back to top


subs_type_save

/**
 * Saves a subscription type.
 *
 * @param $type
 *   The subscription type object
 * @return boolean
 *   Whether the subscription type was saved successfully.
 */
function subs_type_save(SubsType $type) {
  $type->save();
}

Back to top


subs_type_delete

/**
 * Deletes a subscription type.
 *
 * @param $type
 *   The subscription type object
 */
function subs_type_delete(SubsType $type) {
  $type->delete();
}

Back to top


subs_get_types

/**
 * Returns an array of all subscription types, keyed by the type name.
 *
 * @param $type_name
 *   If set, the type with the given name is returned.
 * @return SubsType[]
 *   Depending whether $type isset, an array of subscription types or a
 *   single one.
 */
function subs_get_types($type_name = NULL) {
  $subs_types = &drupal_static(__FUNCTION__);

  if (empty($subs_types)) {
    $subs_types = entity_load_multiple_by_name('subs_type', isset($type_name) ? array($type_name)
          : FALSE);
  }

  return isset($type_name) ? reset($subs_types) : $subs_types;
}

Back to top

Help improve this page

Page status: Not set

You can: