You are here

function socialmedia_tokens in Social media 7

File

./socialmedia.tokens.inc, line 99
Token processing for social media

Code

function socialmedia_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ($type == 'current-page') {
    if (isset($tokens['title-plain'])) {
      $replacements[$tokens['title-plain']] = strip_tags(drupal_get_title());
    }
  }
  if ($type == 'site' || $type == 'user' || $type == 'socialmedia') {

    //$uid = (($type == 'user') && isset($data['user']->uid)) ? $data['user']->uid : 0;
    foreach ($tokens as $name => $original) {
      if (strpos($name, 'sm-') === 0) {
        list($platform_name, $hash) = explode('_', substr($name, 3), 2);

        // process icon paths
        if (strpos($platform_name, 'default') === 0) {
          $replacements[$original] = socialmedia_default_tokens($hash);
          continue;
        }
        if (strpos($hash, 'icon-path') === 0 || strpos($hash, 'icon-markup') === 0) {
          $mode = strpos($hash, 'icon-path') === 0 ? 'path' : 'markup';
          $replacements[$original] = socialmedia_icon_tokens($platform_name, $hash, $mode);
          continue;
        }
        $profile = NULL;

        // if token has user context, load the profiles for the user
        $profile_context = isset($data['set']['data']['socialmedia']['profile_context']) ? $data['set']['data']['socialmedia']['profile_context'] : 'usersite';
        if (strpos($profile_context, 'user') !== FALSE && ($type == 'user' || $type == 'socialmedia')) {
          $uid = socialmedia_get_context_author_uid($data);
          if (isset($uid) && $uid) {
            $profile = socialmedia_profile_load($platform_name, $uid);
          }
        }

        // if site context or user profile not found, load site profile
        $load_site_profile = 0;
        if (strpos($profile_context, 'site') !== FALSE && ($type == 'site' || $type == 'socialmedia')) {
          if ($profile_context == 'site') {
            $load_site_profile = 1;
          }
          else {
            if ($profile_context != 'user' && (!isset($profile['result']) || !$profile['result'])) {
              $load_site_profile = 1;
            }
          }
        }
        if ($load_site_profile) {
          $profile = socialmedia_profile_load($platform_name, 0);
        }
        $platform = socialmedia_platform_definition_load($platform_name);
        $replacement = call_user_func($platform['tokens callback'], $hash, $profile);
        if (!isset($replacement) || $replacement === FALSE) {
          $msg = t('You are trying to use a social media %platform profile token but the profile has not been set. !link', array(
            '%platform' => $platform['title'],
            '!link' => l(t('Set @platform profile.', array(
              '@platform' => $platform['title'],
            )), 'admin/config/media/socialmedia'),
          ));

          //_socialmedia_widgets_set_error($data['widgets']['element'], $msg);
          _socialmedia_widgets_set_error($data['set']['name'] . ':' . $data['widgets']['element']['name'], '');
        }
        else {
          $replacements[$original] = $replacement;
        }
      }
    }
  }
  return $replacements;
}