Drupal 8 custom module adds php classes

I created my own Drupal 8 module, which works just like with a custom block and block form to collect some information.

This is all good.

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

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

It contains two files:

ultimate.twitter.feed.php

and

tmhOAuth.php

Currently require_once 'tmhOAuth.php'; located at the beginning of ultimate.twitter.feed.php

According to the instructions, I have to create a php file that has the following:

 $options = array( 'screen_name' => 'FeedTestUser', 'consumer_key' => '...', 'consumer_secret' => '...', 'user_token' => '...', 'user_secret' => '...', ); $twitter = new Twitter($options); $twitter->PrintFeed(); 

I assume this is also an obstacle since twig files are not php

Any help with this is much appreciated.

WITH

+5
source share
3 answers

I would set the class as a Service in your module. Then your block implements this service and performs the processing. You really don't want to use require_once() if you can avoid this instead of using Drupal constructs (in part, so that if you reorganize things later, Drupal will help find the files in a new place).

Put the class in the src directory of your module and add the namespace at the beginning of the file (if it is not already there). Then, in the block class file, you can add a usage statement that references this namespace (it would be even better to use dependency injection, but more on that in your way).

In the build() block class method, you instantiate the class as described in your question, but instead of just letting the module print HTML, you might want to capture this HTML code and put it in your block as markup. If the class allows you to do this without using a buffer, you should (but I have not seen anything in the documents to support it), and then try to separate the structured data. If not, you can use PHP output buffering to record its attempt to print:

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

Then put the generated markup in the rendering array:

 return [ 'my_twitter_block' => [ '#markup' => $content, ], ]; 
+2
source

Create a custom block and add the result of printFeed () to the render array. Like any regular user block. In the rendering array, you can specify the template that should be used (if necessary). If you want to output pure html without any template, you can use the key "#markup".

A small example:

Block Rendering Array:

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

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

 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 template / templates):

 {{ some_twig_variable }} 

Regarding the use of the class: I don't see a problem using require_once (in your php block file). Of course, it is always better / better if you can require a library / package through a make file or composer and then use an autoloader, but if that is not possible, just enter it, for example. in your root drupal directory under / libraries / twitter or so and then require it. If you do this, you need to check this library in your git repository.

+1
source

You use final.twitter.feed.php in your TwitterBlock.php file

If not, try adding this line before the start of the block:

 require_once 'path/to/twitter_class/ultimate.twitter.feed.php'; 
0
source

All Articles