You are here

function notifications_build_link in Notifications 6.4

Return link array for notifications objects

Parameters

$op: Link operation: 'subscribe', 'unsubscribe', 'edit', 'manage'

$params: Aditional parameters for the subscription, may be

  • uid, the user for which the link is generated
  • confirm, whether to show confirmation page or not
  • destination, form destination or TRUE to use current path
  • signed, to produce a signed link that can be used by anonymous users (Example: unsubscribe link in emails)
  • language, language object to user for generating the url
  • Other subscription parameters: type, fields...

$type: Object type: 'subscription', 'destination'

$format: Optional format string to return a formatted link/url instead or a raw one (array)

  • 'link' => Return a link
  • 'url' => Only the URL (no link)
7 calls to notifications_build_link()
NotificationsContentTests::testNotificationsContent in tests/notifications_content.test
Play with creating, retrieving, deleting a pair subscriptions
notifications_anonymous_manage_links in notifications_anonymous/notifications_anonymous.module
Get raw /formatted links to manage subscription / destination
notifications_anonymous_subscription_get_link in notifications_anonymous/notifications_anonymous.module
Get subscribe / unsubscribe link for subscription
notifications_build_url in ./notifications.module
Shorthand for building absolute signed URLs for messages
notifications_get_link in ./notifications.module
Return link array for subscriptions. OBSOLETED.

... See full list

File

./notifications.module, line 1473
Notifications module

Code

function notifications_build_link($op, $params, $type = 'subscription', $object = NULL, $format = 'array') {

  // Find the key fields and adjust parameters for each object type
  $key = array_search($type, array(
    'sid' => 'subscription',
    'mdid' => 'destination',
    'uid' => 'user',
  ));
  if ($object) {
    $params += array(
      'oid' => $object->{$key},
      'uid' => $object->uid,
    );
  }
  else {
    $params += array(
      'uid' => 0,
    );
  }

  // Build path elements that won't include the fixed prefix 'notifications'
  $elements = array();

  // Anonymous operations will be handled differently and the path will be notifications/anonymous
  if (!$params['uid']) {
    $params += array(
      'signed' => TRUE,
    );
  }
  $title = isset($params['title']) ? $params['title'] : NULL;

  // Build path elements depending on object type and operation
  switch ($op) {
    case 'subscribe':
      if ($object && !isset($params['type']) && !isset($params['fields'])) {
        $params += array(
          'type' => $object->type,
          'fields' => $object
            ->get_conditions(),
        );
      }
      if ($params['uid']) {
        $elements = array(
          'subscribe',
          $params['uid'],
        );
      }
      else {
        $elements = array(
          'anonymous',
          'subscribe',
        );
      }
      $elements[] = $params['type'];
      $elements[] = implode(',', array_keys($params['fields']));
      $elements[] = implode(',', $params['fields']);
      $title = $title ? $title : t('subscribe');
      break;
    case 'unsubscribe':

      // The unsubscribe link can be for a single subscription or all subscriptions for a user or a destination
      if (!$params['uid'] && $type == 'subscription') {

        // For anonymous: notifications/subscription/sid/unsubscribe
        $elements[] = 'anonymous';
        $elements[] = 'subscription';
        $elements[] = $params['oid'];
        $elements[] = 'unsubscribe';
      }
      else {
        $elements[] = 'unsubscribe';
        $elements[] = $type;
        $elements[] = $params['oid'];
      }
      $title = $title ? $title : t('unsubscribe');
      break;
    case 'confirm':

      // For this and next cases we just set title if not set and fallback to default
      $title = $title ? $title : t('confirm');
    case 'edit':
      $title = $title ? $title : t('edit');
    case 'manage':
      $title = $title ? $title : t('manage');
    case 'delete':
      $title = $title ? $title : t('delete');
    default:
      if (!$params['uid']) {
        $elements[] = 'anonymous';
      }

      // This is a generic operation like 'subscription/sid/edit' or 'destination/mdid/manage'
      $elements[] = $type;
      $elements[] = $params['oid'];
      $elements[] = $op;
      break;
  }
  $params += array(
    'title' => $title,
  );
  $link = _notifications_get_link($elements, $params);

  // Finally, format as requested or return full array
  switch ($format) {
    case 'link':
      return l($params['title'], $link['href'], $link['options']);
    case 'url':
      return url($link['href'], $link['options']);
    default:
      return $link;
  }
}