Overslaan en naar de inhoud gaan

Twitterblok zonder gebruikers

Als je Twitter berichten wilt laten inlopen op je Drupal website gebruik je daarvoor de Twitter module. Deze heeft alleen het nadeel dat hij gekoppeld (moet) worden aan de gebruikers. Stel je wilt Twitter berichten binnenhalen op een node waarbij het Twitter account gekoppeld is via een CCK-veld bijvoorbeeld.

Dat kan je op de volgende manier doen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
 * Implements hook_theme().
 */
function mymodule_theme() {
  return array(
    'mymodule_twitter_block' => array(
      'arguments' => NULL,
    ),
  );
}
 
/**
 * Implements hook_block().
 */
function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks['twitter'] = array(
        'info' => t('Twitter block activiteit')
      );
      return $blocks;
    case 'configure':
      if ($delta == 'twitter') {
        return array(
          'items' => array(
            '#type' => 'textfield', 
            '#title' => t('Number of items'), 
            '#default_value' => variable_get('twitter_block_items', 5),
          )
        );
      }
      break;
    case 'save':
      if ($delta == 'twitter') {
        variable_set('twitter_block_items', $edit['items']);
      }
      break;
    case 'view':
      if ($delta == 'twitter') {
        return array(
          'subject' => t('Twitter'),
          'content' => theme('mymodule_twitter_block'),
        );
      }
  }
}
 
/**
 * Get the Twitter data based on the CCK field on a node.
 * @param int/string $nid
 */
function mymodule_twitter_get_data($nid) {
  $node = node_load(array('nid' => $nid));
  if (module_exists('twitter')) {
    if ($twitter_username = $node->field_twitter[0]['value']) {
      if (($cache = cache_get('twitter_timeline_'  . $twitter_username)) && !empty($cache->data)) {
        $timeline_data = $cache->data;
      }
      else {
        module_load_include('lib.php', 'twitter');
        $twitter_account = new Twitter($twitter_username);
        $raw_timeline_data = $twitter_account->user_timeline($twitter_username, array('count' => 6));
        foreach ($raw_timeline_data as $tweet) {
          $timeline_data[] = array(
            'text' => $tweet->text,
            'image' => $tweet->user->profile_image_url,
            'source' => $tweet->source,
            'created' => $tweet->created_at,
          );
        }
        cache_set('twitter_timeline_'  . $twitter_username, $timeline_data, 'cache', CACHE_TEMPORARY);
      }
      return $timeline_data;
    }
  }
  return array();
}
 
/**
 * The Twitter block
 */
function theme_mymodule_twitter_block() {
  $output = '';
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    if ($timeline = mymodule_twitter_get_data(arg(1))) {
      foreach ($timeline as $tweet) {
        $time_diff = time() - strtotime($tweet['created']);
        $time_ago = t('%time ago', array('%time' => format_interval($time_diff, is_numeric(strtotime($tweet['created'])) ? strtotime($tweet['created']) : 2)));
 
        $output .= '<div class="tweet">';
        $output .= '<img src="' . $tweet['image'] . '" />';
        $output .= '<p class="text">' . filter_xss(twitter_link_filter($tweet['text'])) . '</p>';
        $output .= '<p class="created">' . $time_ago . '</p>';
        $output .= '</div>';
      }
    }
  }
  return $output;
}