Drupal 8 - 如何在搜索结果中显示自定义模块内容?

时间:2016-07-13 14:07:11

标签: drupal

学习自定义模块 - 我已经创造了一个" hello world"模块和使用单词" hello world" (有或没有引号),它不会显示在搜索结果中。

如何在搜索结果中显示自定义模块内容?

我想要这个的原因是因为我要创建一个导入一堆word文档的模块,这些文档会不断变化(它们通过owncloud上传到服务器),我希望它们可以搜索。我打算制作一个自定义模块来处理这个过程。我提到它的原因是因为我可能没有正确地解决这个问题。我假设它是正确使用模块和实现此需求的最佳方式。

编辑(添加代码)

控制器:

$ cat src/Controller/HelloWorldController.php
<?php

namespace Drupal\hello_world\Controller;

class HelloWorldController {

  public function myCallbackMethod() {
    $element = array(
      '#markup' => '<p>Hello World</p>',
    );
    return $element;
  }
}

info yaml file:

$ cat hello_world.info.yml
name:  Hello World
type:  module
description:  'A basic Drupal 8 Hello World Module.'
package:  Custom Modules
version:  1.0
core:  8.x

模块文件:

$ cat hello_world.module
<?php

use Drupal\Core\Routing\RouteMatchInterface;

function hello_world_permission() {
  $permissions = array(
    'administer hello world' => array(
      'title' => t('Administer Hello World module'),
      'description' => t('Change the settings for Hello World module.'),
    ),
  );
 return $permissions;
}

路由yaml文件:

$ cat hello_world.routing.yml
hello_world.hello_page:
  path:  '/hello/world'
  defaults:
    _controller:  '\Drupal\hello_world\Controller\HelloWorldController::myCallbackMethod'
    _title: 'Hello World'
  requirements:
    _permission: 'access content'
first.form:
  path:  '/first/form'
  defaults:
    _form: '\Drupal\hello_world\Form\FirstForm'
    _title: 'First Form'
  requirements:
    _permission: 'access content'

1 个答案:

答案 0 :(得分:-1)

必须将Drupal中的每个内容编入索引才能在搜索结果页面中显示。索引是Drupal中的cron任务之一,因此您需要在添加新内容后运行cron才能在搜索结果中显示。您可以手动运行cron,也可以设置cron在某个时间间隔内自动运行。

您可以在Drupal cron找到更多信息。

希望这有帮助。

相关问题