简体   繁体   English

Drupal 8自定义模块添加php类

[英]Drupal 8 custom module add php classes

I have created a custom Drupal 8 module that works as is with a custom block and block form to collect some info. 我已经创建了一个自定义Drupal 8模块,该模块与自定义块和块表单一起工作以收集一些信息。

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. 我还有一个twig模板,我想用我买的php feed类渲染一个twitter feed。 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 这是该课程的设置: 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'; 这是目前的require_once 'tmhOAuth.php'; in the head of ultimate.twitter.feed.php ultimate.twitter.feed.php的头部

According to the instruction I should be creating a php file that has this: 根据指令,我应该创建一个具有以下内容的php文件:

$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 我猜这也是一个障碍因为twig文件不是php

Any help with this is very much appreciated. 非常感谢任何帮助。

C C

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). 如果你可以避免使用require_once() ,你真的不想使用它,而是使用Drupal构造(部分使得如果你以后重新组织东西,Drupal将帮助在新位置找到文件)。

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). 将类放在模块的src目录中,并将一个命名空间添加到文件的开头(假设已经没有一个)。 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). 然后在你的块的类文件中,你应该能够添加一个引用该名称空间的use语句(更好的是使用依赖注入,但是这里的详细信息会妨碍你)。

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. 在块类的build()方法中,然后按照问题中的描述实例化类,但不是只让模块打印HTML,而是希望捕获该HTML并将其作为标记放入块中。 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: 如果没有,您可以使用PHP的输出缓冲来捕获其打印尝试:

 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,
  ],
];

Create a custom block and add the result of PrintFeed() to the render array. 创建自定义块并将PrintFeed()的结果添加到渲染数组。 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. 如果你想输出没有任何模板的纯HTML,你可以使用'#markup'键。

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): your_module.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_module/templates): 你的name-of- theme.html.twig模板(应该在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). 至于使用类:我发现使用require_once没问题(在你的Block php文件中)。 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 eg in your drupal root under /libraries/twitter or so and then require it. 当然,如果你可以通过make文件或编辑器需要库/包然后使用自动加载器,它总是更好/更好,但如果这不可能只是把它放在你的drupal根目录下/ libraries / twitter左右然后需要它。 If you do it like that you have to check that library into your git repository obviously. 如果您这样做,您必须明确检查该库到您的git存储库。

have you use ultimate.twitter.feed.php in your TwitterBlock.php file 你在TwitterBlock.php文件中使用ultimate.twitter.feed.php吗?

If not then try adding this line before class block beginns: 如果没有,那么尝试在类块开始之前添加此行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM