function template_preprocess_socialfeed_twitter_post in Social Feed 8
Preprocess socialfeed_twitter_post.
See also
File
- ./
socialfeed.theme.inc, line 57 - Theme preprocessors.
Code
function template_preprocess_socialfeed_twitter_post(&$variables) {
if (empty($variables['post']) || !is_object($variables['post'])) {
unset($variables['post']);
return;
}
$twitter_settings = Drupal::config('socialfeed.twittersettings');
$display_time = $twitter_settings
->get('time_stamp');
$display_date_twitter_style = $twitter_settings
->get('time_ago');
$twitter_hash_tag = $twitter_settings
->get('hashtag');
$time_format = $twitter_settings
->get('time_format');
$trim_length = $twitter_settings
->get('trim_length');
$teaser_text = $twitter_settings
->get('teaser_text');
$post =& $variables['post'];
$post->username = $post->user->screen_name;
$post->full_username = '//twitter.com/' . $post->username;
$post->text = $post->full_text;
preg_match_all('#\\bhttps?://[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/))#', $post->text, $extra_links);
foreach ($extra_links[0] as $extra_link) {
$post->text = str_replace($extra_link, Link::fromTextAndUrl($extra_link, Url::fromUri($extra_link, [
'attributes' => [
'target' => '_blank',
],
]))
->toString(), $post->text);
}
if (isset($post->text)) {
$post->tweet = substr(rtrim($post->text), 0, $trim_length);
}
if (!empty($teaser_text)) {
if (isset($post->entities->media)) {
$post->tweet_url = Link::fromTextAndUrl(t('@teaser_text', [
'@teaser_text' => $teaser_text,
]), Url::fromUri($post->entities->media[0]->url, [
'attributes' => [
'target' => '_blank',
],
]))
->toString();
}
}
if ($display_time) {
$formatted_twitter_date = new DateTime($post->created_at);
if ($display_date_twitter_style) {
$post->twitter_date = socialfeed_time_elapsed_string($formatted_twitter_date);
}
else {
$post->twitter_date = $formatted_twitter_date
->format($time_format);
}
}
if ($twitter_hash_tag) {
$post->tweet = preg_replace_callback('/#(\\w+)|@(\\w+)/', function ($hash) {
if ($hash[0][0] == '#') {
return Link::fromTextAndUrl($hash[0], Url::fromUri('https:twitter.com/hashtag/' . $hash[1], [
'attributes' => [
'target' => '_blank',
],
]))
->toString();
}
if ($hash[0][0] == '@') {
return Link::fromTextAndUrl($hash[0], Url::fromUri('https:twitter.com/' . $hash[2], [
'attributes' => [
'target' => '_blank',
],
]))
->toString();
}
}, $post->tweet);
}
$post->tweet = [
'#markup' => $post->tweet,
];
}