0

I've created a new custom module and I used Facebook SDK to retrieve the latest post from a page. Here is my .module file:

function social_theme($existing, $type, $theme, $path){
  return array(
    'block__feed_feed' => array(
    'render element' => 'elements',
    'template' => 'block--instagram-feed',
    'base hook' => 'block'
  )
}
function social_preprocess_block(&$variables){
 if ($variables['elements']['#configuration']['id'] === 'facebook-feed'){
   $fb = new \Facebook\Facebook([
     'app_id' => {$app_id},
     'app_secret' => {$app_secret},
     'default_graph_version' => 'v2.10',
     'default_access_token' => {$app_id}.'|'.{$app_secret},
   ]);

   $request = $fb->request('GET', '/'.{$page_id}.'/posts?fields=created_time,reactions,message,comments{comment_count,like_count},likes&limit=4');
   try {
     $response = $fb->getClient()->sendRequest($request);
   } catch(Facebook\Exceptions\FacebookResponseException $e) {
     echo 'Graph returned an error: ' . $e->getMessage();
     exit;
   } catch(Facebook\Exceptions\FacebookSDKException $e) {
     echo 'Facebook SDK returned an error: ' . $e->getMessage();
     exit;
   }
   $posts = $response->getDecodedBody();
   $facebookposts = [];
   for($c=0 ;$c<$posts_no ;$c++){
     if(isset($posts['data'][$c]['reactions'])){
       $likes_count = count($posts['data'][$c]['reactions']['data']);
     }
     else{
       $likes_count = 0;
     }
     if(isset($posts['data'][$c]['comments'])){
       $comments_count = count($posts['data'][$c]['comments']['data']);
     }
     else{
       $comments_count = 0;
     }
     array_push($facebookposts,['post_text'=>$posts['data'][$c]['message'],'likes_count'=>$likes_count,'comments_count'=>$comments_count]);
   }
   $variables['elements']['#cache']['max-age'] = 0;
   $variables['fbposts'] = $facebookposts;
   $variables['likes_comments'] = $likes_comments;
 }
}

and my block plugin code:

public function build() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return array(
    '#markup' => ' ',
    '#cache' => ['max-age' => 0,],
  );
}

and this is my block template file:

<ul>
{% for post in fbposts %}
 <li>
  <div class="facebook">{{post.post_text}}</div>
    <div class="likes-comments">
     <span id="comments">{{post.comments_count}}</span>
     <span id="likes">{{post.likes_count}}</span>
    </div>
  </li>
 {% endfor %}
</ul>

Now my problem is that the code is working perfectly, but when a new post is added it will not change on the website unless I clear the cache!!! I tried to give the block '#cache' => ['max-age' => 0,] but it didn't work, I tried \Drupal::service('page_cache_kill_switch')->trigger(); on the hook_preprocess_block but id didn't work too.

What shall I do?

  • Some additional recommendations: Not caching that at all seems quite problematic actually, it means every single request to your site has to wait on the external API. Why not cache it for 5 or 10 minutes, I assume your facebook page doesn't change that fast? You can also use getCacheMaxAge() and return 0 there, then you don't have to print content. I'd recommend putting the logic in the block and sub array keys, then you can access it as e.g. fbposts. You could also either make a template for them all as suggested or make a template for one fbpost and then show them as a #theme item_list. – Berdir Sep 18 '17 at 20:03
2

You have to render the content build() returns:

block.html.twig:

...

{{ content }}

Even if it's only an empty markup string. Otherwise the cache max-age can't bubble up.

A better approach would be to return from build() a custom template, which you use for the html markup instead of the block template. See https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module

Then you don't need a preprocess hook and caching works with the default block template.

| improve this answer | |
  • thanks!!! it worked, maybe i spend 2 days for this xD and i think i'll use your approach. thanks again ;) – Ahmad Abed Sep 18 '17 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.