[英]Drupal 8 custom module not showing up in block layout
我有一個用於 Drupal 8 的自定義塊模塊。它正在我的 localhost 版本的 drupal(版本 8.7.8)上工作。 當我將它上傳到 Web 服務器(版本 8.7.11)時,我可以啟用該模塊,但是當我嘗試將塊放置在塊布局頁面上時它沒有顯示。 我對 Web 服務器沒有太多控制權 - 文件是通過 git 存儲庫上傳的,但我添加的其他模塊沒有問題。
我的模塊只有 2 個文件:
模塊/自定義/ischool_section_title_level_two/ischool_section_title_level_two.info.yml
name: iSchool Section Title Level Two
description: Provides a block that shows the Level Two title, or Level One if there is no Level Two.
core: 8.x
package: Custom
dependencies:
- block
type: module
模塊/自定義/ischool_section_title_level_two/src/plugin/block/iSchoolSectionTitlelevel_two.php
<?php
namespace Drupal\ischool_section_title_level_two\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a block that shows the Level Two section title, or Level One title if there is no level Two
*
* @Block(
* id = "ischool_section_title_level_two",
* admin_label = @Translation("iSchool Section Title Level Two"),
* category = @Translation("Custom"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
* }
* )
*/
//code adapted from http://hussainweb.me/an-easier-way-to-get-the-current-node-in-a-block-plugin-in-drupal-8/
//and https://design.briarmoon.ca/tutorials/drupal-8/getting-the-parent-node-of-a-drupal-8-node
class iSchoolSectionTitlelevel_two extends BlockBase {
public function build() {
$node = $this->getContextValue('node');
if (empty($node)) {
return [
'#markup' => "",
];
}
$L1_Title = $node->getTitle();
$L2_Title = $node->getTitle();
$currentNode = $node;
while (true) {
$parent_node = $this->getParentNode($currentNode);
if (empty($parent_node)){
break;
}
$L2_Title = $L1_Title;
$L1_Title = $parent_node->getTitle();
$currentNode = $parent_node;
}
return [
'#markup' => $L2_Title,
];
}
private function getParentNode($node){
if (empty($node)) return null;
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$links = $menu_link_manager->loadLinksByRoute('entity.node.canonical', ['node' => $node->id()]);
// Because loadLinksByRoute() returns an array keyed by a complex id
// it is simplest to just get the first result by using array_pop().
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$link = array_pop($links);
if (empty($link)) return null;
/** @var \Drupal\Core\Menu\MenuLinkInterface $parent */
if ($link->getParent() && $parent = $menu_link_manager->createInstance($link->getParent())) {
if (!method_exists($parent, "getUrlObject")) return null;
$urlObj = $parent->getUrlObject();
if (is_null($urlObj)) return null;
if (!method_exists($urlObj, "getRouteParameters")) return null;
$route = $urlObj->getRouteParameters();
if (empty($route)) return null;
if (!isset($route['node'])) return null;
$parent_node = \Drupal::entityManager()->getStorage('node')->load($route['node']);
return $parent_node;
}
else return null;
}
// cache this block for a definite time.
public function getCacheMaxAge() {
return 43200;
}
}
這是文件夾大小寫的問題。
第二個文件應該在 /src/Plugin/Block/ 文件夾中,而是在 /src/plugin/block/ 文件夾中(缺少初始大寫字母)。
在本地 Windows 機器上,這沒有任何區別。 在 LAMP 堆棧機器上,它導致塊未顯示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.