【发布时间】:2016-02-11 13:48:14
【问题描述】:
我正在 Drupal 8 中开发一个自定义模块。它显示有关使用我们服务的一些组织的数据。为此,我创建了一个控制器来显示来自数据库的数据,该控制器由另一个模块放在那里。从 Drupal 8 开发中可用的稀缺信息和教程中,我已经能够创建以下内容。在 .routing.yml 文件中,我创建了此概览表的路径,如下所示(此处未正确复制,但缩进没问题):
OrganizationOverview.world:
path: '/world'
defaults:
_controller: 'Drupal\OrganizationOverview\Controller\OrganizationOverviewController::overview'
_title: 'World'
requirements:
_role: 'administrator'
_permission: 'access content'
现在可以通过 URL site.com/world 访问概览。但我们想要的是在首页上显示它或在网站的其他任何地方显示它。为此,它需要是一个块。为此,我在OrganizationOverview/src/Plugin/Block/OrganizationOverviewBlock.php 中创建了一个OrganizationOverviewBlock 类,这是符合PSR-4 标准的正确方法。该类如下所示:
<?php
namespace Drupal\OrganizationOverview\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a 'OrganizationOverviewBlock' block.
*
* @Block(
* id = "organization_overview_block",
* admin_label = @Translation("OrganizationOverviewBlock"),
* category = @Translation("Custom")
* )
*/
class OrganizationOverviewBlock extends BlockBase
{
public function build()
{
return array(
'#markup' => 'Hello World',
);
}
public function blockAccess(AccountInterface $account)
{
return $account->hasPermission('access content');
}
}
所以现在它应该显示在块布局页面中(在刷新缓存之后,我一直这样做)site.com/admin/structure/block/ 作为“组织概览块”,我应该在其中启用它,根据大量来源(Create custom Block,@ 987654322@)。但它并没有出现在那里。我已经尝试使用其中一些方法实现 ContainerFactoryPluginInterface ,但这没有任何改变。它没有出现。我尝试使用具有相同代码但名称更简单的块制作一个新的测试模块,但它没有显示出来。我已将代码复制到另一个平台(生产站点),但它也没有出现在那里。我究竟做错了什么?有人能帮我吗?我知道 Drupal 8 是新的,但这个模块确实需要尽快发布。
【问题讨论】:
-
我投票支持在 StackOverflow 上关闭这个问题,并移至 Drupal Answers。我认为您将有更好的机会获得有用的答案。
标签: module block drupal-8 drupal-blocks