5

I have created a custom Drupal 8 module that works as is with a custom block and block form to collect some info.

This is all good.

I also have a twig template that I want to render a twitter feed using a php feed class I bought. I just don't know how it integrate this into the module.

This is the setup for the class: http://austinbrunkhorst.com/demos/twitter-class/#setup

It contains two files:

ultimate.twitter.feed.php

and

tmhOAuth.php

Which is currently a require_once 'tmhOAuth.php'; in the head of ultimate.twitter.feed.php

According to the instruction I should be creating a php file that has this:

$options = array(
    'screen_name' => 'FeedTestUser',
    'consumer_key'  => '...',
    'consumer_secret' => '...',
    'user_token' => '...',
    'user_secret' => '...',
);

$twitter = new Twitter($options);

$twitter->PrintFeed();

Which I'm guessing is also a hurdle as twig files are not php

Any help with this is very much appreciated.

C

Cybercampbell
  • 2,486
  • 11
  • 48
  • 75

3 Answers3

2

I would setup the class as a Service in your module. Your block will then implement that service and do the handling. You don't really want to use require_once() if you can avoid it, rather use Drupal constructs (in part so that if you reorganize things later Drupal will help find the files in their new location).

Place the class in your module's src directory, and add a namespace to the start of the file (assuming there isn't one there already). Then in your block's class file you should be able to add a use statement that references that name space (even better would be to use a dependency injection, but the details on that would get in your way here).

In your block class's build() method you then instantiate the class as described in your question, but instead of just letting the module print HTML, you can want to capture that HTML and place it into your block as markup. If the class allows you to do that without using a buffer, you should (but I didn't see anything in the docs to support that), and then attempt to theme the structured data. If not, you can use PHP's output buffering to capture its attempt to print:

 ob_start();
 $twitter->PrintFeed();
 $content= ob_get_contents();
 ob_end_clean();

Then place the generated markup into a render array:

return [ 
  'my_twitter_block' => [
    '#markup' => $content,
  ],
];
acrosman
  • 12,814
  • 10
  • 39
  • 55
1

Create a custom block and add the result of PrintFeed() to the render array. Just as with any usual custom block. In the render array you can specify a template which should be used (if needed). If you wanna output pure html without any template you could use the '#markup' key.

Small example:

Your block render array:

return array(
    '#theme' => 'name_of_your_theme',
    '#some_twig_variable' => $twitter->PrintFeed();
);

your your_module.module file (in the root of your module folder):

function your_module_theme() {
  return array(
    'name_of_your_theme' => array(
      'variables' => array(
        'some_twig_variable' => some-default-value,
      ),
    ),
  );
}

your name-of-your-theme.html.twig template (should be under your_module/templates):

{{ some_twig_variable }}

As far as using the class: I see no problem using a require_once for that matter (in your Block php file). Of course it's always better/nicer if you can require the library/package via the make file or composer and then use the autoloader, but if that's not possible just put it e.g. in your drupal root under /libraries/twitter or so and then require it. If you do it like that you have to check that library into your git repository obviously.

Frank Drebin
  • 1,063
  • 6
  • 11
  • Thanks @Frank I did everything as you described but I get this error: `Error: Class 'Drupal\my_module\Plugin\Block\Twitter' not found in Drupal\my_module\Plugin\Block\TwitterBlock->build() (line 64 of /Users/username/Sites/devdesktop/devsite/modules/custom/my_module/src/Plugin/Block/TwitterBlock.php).` The error gets triggered by this line `$twitter = new Twitter($options);` Any thoughs? – Cybercampbell Jun 07 '16 at 15:40
0

have you use ultimate.twitter.feed.php in your TwitterBlock.php file

If not then try adding this line before class block beginns:

require_once 'path/to/twitter_class/ultimate.twitter.feed.php';
Monal Soft
  • 320
  • 1
  • 2
  • 5
  • Thanks @Monal - I tried both those things but the error is the same. It seems the require is working fine but the site goes offline and the error in the comment above shows in the logs. Any ideas what i'm doing wrong? – Cybercampbell Jun 09 '16 at 09:38