自定义一个layout布局的东西需要的东西
布局定义:注解->regions
html模板:注解->template
js/css库:注解->library->.libraries.yml
注解说明
<?php
namespace Drupal\my_module\Plugin\Layout;
use Drupal\layout_builder\Plugin\Layout\LayoutBase;
/**
* Provides a custom layout.
*
* @Layout(
* id = "my_custom_layout",
* label = @Translation("My Custom Layout"),
* category = @Translation("My Custom Category"),
* path = "layouts/custom",
* template = "my-custom-layout",
* regions = {
* "header" = {
* "label" = @Translation("Header"),
* },
* "content" = {
* "label" = @Translation("Content"),
* },
* "footer" = {
* "label" = @Translation("Footer"),
* },
* }
* )
*/
class MyCustomLayout extends LayoutBase {
// Your class logic here.
}
注解字段说明
id
唯一标识此布局的ID,用于在代码中引用。 示例:”my_custom_layout” label
布局的可读名称,用于UI显示。 示例:@Translation(“My Custom Layout”) category
布局分类,用于在布局选择界面中分组。 示例:@Translation(“My Custom Category”) path
模板文件的路径,基于模块的根目录。 示例:”layouts/custom” template
模板文件的名称,不带扩展名(默认扩展为 .html.twig)。 示例:”my-custom-layout” regions
定义布局的可用区域,每个区域都有一个 label。 示例:
"header" = {
"label" = @Translation("Header"),
}
配置模板文件
在 layouts/custom 路径下创建 my-custom-layout.html.twig,定义布局的结构,例如:
<div class="layout">
<div class="layout-header">
{{ content.header }}
</div>
<div class="layout-content">
{{ content.content }}
</div>
<div class="layout-footer">
{{ content.footer }}
</div>
</div>
安装与使用
首先,清除缓存。
然后,导航到/admin/structure/types/manage/cklb_landing_page/display/admin/structure/types/manage/cklb_landing_page/display
展开Layouts available for sections
,勾选刚刚添加的Layout.