定制 toastnz/blocks-layouts 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

toastnz/blocks-layouts

Composer 安装命令:

composer require toastnz/blocks-layouts

包简介

Silverstripe content blocks module

README 文档

README

Simple content blocks system. Nothing fancy, easy to implement.

Requirements

See composer.json

Installation

Add the following to your config.yml (optional):

PageController:
  extensions:
    - Toast\Blocks\Extensions\PageControllerExtension

Use Page or other class that extends SiteTree.

In your Layout/Page.ss template, add the following:

<% loop $ContentBlocks %>
    $ForTemplate
<% end_loop %>

Configuration

Add / remove available block classes

Toast\Blocks\Extensions\PageExtension:
  available_blocks:
    - Toast\Blocks\TextBlock

Position page template content between blocks

Your\Page\Class:
  extensions:
    - Toast\Blocks\Extensions\PageContentBlockPageExtension
<% loop $ContentBlocks.Sort('SortOrder') %>
    <% if $IsPageContentBlock %>
        <% with $Top %>
            <%-- Your page content code here --%>
        <% end_with %>
    <% else %>
        {$ForTemplate}
    <% end_if %>
<% end_loop %>

Add / remove available alternate block layouts

"layout_src": directory that holds folders of different layouts with .ss templates "layout_icon_src": directory that holds all the layout icons "layout_dist_dir": specificed the css for block layouts

CSS file will only be included with the syntax of 'theme/themename/dist/styles/$LayoutName-$BlockType.css"

Toast\Blocks\Extensions\PageExtension:
  layout_src: 'app/templates/Toast/Blocks'
  layout_icon_src: 'app/client/images/layout-icons'
  layout_dist_dir: 'theme/themename/dist/styles'

Ensure there are at least one CustomBlock.ss and 'customblock.svg' icon in each of the specified directory. Layout will be available for all subsites.

.ss template naming

You may have multiple layouts, please ensure you have the block.ss created under a new layout folder in the src directory. e.g. 'app/templates/Toast/Blocks/CustomLayoutNameOne/ImageBlock.ss' or 'app/templates/Toast/Blocks/CustomLayoutNameTwo/ImageBlock.ss'

Layout icon naming:

Please ensure the layout icon are named after the block name are all in lowercase, e.g. customblock.svg. e.g. 'app/client/images/layout-icons/customlayoutone/customblock.svg' or 'app/templates/Toast/Blocks/customlayouttwo/customblock.svg'

Icon extensions

Allowed extension: 'jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg'

Per-layout configuration (labels, icons, disabling)

Use layout_config on any block class to customise the label or icon shown in the CMS layout picker, or to disable a layout entirely. Keys are short layout folder names (e.g. Default, Split).

View the available icon classes here: client/fonts/layout-icons/font/preview.html

Toast\Blocks\TextBlock:
  layout_config:
    Default:
      label: 'Default'
      layout_icon_path: '[resources]/app/client/images/layout-icons/default/textblock.svg'
    Split:
      label: 'Split'
      layout_icon_class: 'layout-icons-text-columns'
      disabled: true

Supported keys per layout entry:

Key Type Description
label string Display label shown beneath the icon in the CMS picker
layout_icon_path string Path to an SVG file. Supports the [resources] token. Highest priority.
layout_icon_class string CSS class for an icon font or sprite. Overrides directory discovery.
disabled bool Set true to hide this layout from the CMS picker

Icon resolution priority: layout_icon_pathlayout_icon_class → auto-discovered SVG from layout_icon_src directory.

Full class-name keys (e.g. Toast\Blocks\Default\TextBlock) are still supported for backward compatibility.

Excluding layouts

Use exclude_layouts alongside layout_config to hide one or more layouts from the picker without specifying full config entries. This is the quickest way to remove a layout.

Toast\Blocks\MediaTextBlock:
  exclude_layouts:
    - ShortRounded

Precedence rules:

  • layout_config.<Layout>.disabled: true takes precedence over exclude_layouts.
  • exclude_layouts only hides layouts from the CMS picker — the .ss template file must still exist for the block to render correctly if a record already holds that layout value.
  • YAML cannot create layouts. A .ss template file must exist for a layout to appear.

Fallback when a saved layout is removed

If a block record has a previously saved layout that is later excluded or disabled, the block will automatically fall back to the Default layout at render time and on next save. If Default is also unavailable, the first available layout is used instead.

When a content editor opens such a block in the CMS, a warning notice will appear above the layout picker advising them that the previous layout is no longer available and prompting them to select a replacement and save.

Create a custom block

Extend Block to create a new block type.

<?php


class MyBlock extends Toast\Blocks\Block
{
    private static $singular_name = 'My Block';
    private static $plural_name = 'My Blocks';
    private static $icon = 'mysite/images/blocks/custom.png';

    private static $db = [
        'Content' => 'HTMLText'
    ];

}

/themes/default/templates/Toast/Blocks/MyBlock.ss:

<%-- Your block template here --%>

<h2>$Title</h2>
$Content

Block Method Reference

The Toast\Blocks\Block class exposes the following methods.

Public methods

Rendering and CMS

  • forTemplate(): string - Resolve and render the active template with fallback.
  • getCMSFields() - Build CMS fields for block editing.
  • getAvailableLayouts($className = null) - Return discovered/allowed layouts.
  • getOptionsForLayouts($layouts = []) - Build icon/label options for layout selectors.
  • getTemplateOptionsField() - Return selector field (OptionsetField, DropdownField, or hidden fallback).
  • getCSSFile() - Resolve the CSS file for the current layout/template.
  • onBeforeWrite() - Enforce template fallback and persist CSS file path.
  • getTemplateClass() - Return default template class name.
  • populateDefaults() - Set default/fallback template on new records.

Display and metadata

  • getContentSummary() - CMS summary for content/heading.
  • getIconForCMS() - Block icon HTML shown in CMS.
  • IconForCMS() - Alias for getIconForCMS().
  • getTitle() - Display title fallback to block singular name.
  • getApiURL() - API endpoint URL for this block.
  • getBlockTemplateName() - Short class name used as template base.
  • getHtmlID() - Deterministic HTML id value for this block.
  • getDisplayTitle() - Title with parent page context.
  • getImageFocusPosition($imageid = null) - Return focus point CSS position for an image.
  • getBlockID() - Anchor-friendly block id (from AnchorName or fallback).
  • getExtraRequirements() - Extra frontend requirements injected by extensions.
  • getBlockSpecificExtraClasses() - Extra CSS classes injected by extensions.

Linking and preview

  • getLink($action = null) - Relative URL to this block anchor.
  • Link($action = null) - Alias for getLink().
  • getBlockLink($parent) - Block link for a specific parent page.
  • getBlockPreviewURL($anchor = null) - CMS preview URL including stage/subsite params.
  • getAbsoluteLink($action = null) - Absolute stage URL to this block anchor.
  • AbsoluteLink($action = null) - Alias for getAbsoluteLink().

Page relationships

  • getLinkedPagesList() - Comma-separated linked page titles.
  • getAllPages() - Unique pages (main + subsites) using this block.
  • getPagesFromSiteTree() - Pages using this block in current context.
  • getPagesFromMainSite() - Pages using this block in main site context.
  • getPagesFromSubsites() - Pages using this block across all subsites.
  • getParentPage() - Attempt to resolve current parent page.
  • getPage() - Resolve page owning this block in current controller context.
  • getCMSParentPage() - Current CMS parent page.
  • getCMSEditLink(): ?string - Direct CMS edit URL for this block.
  • getCMSSiblingBlocks() - Sibling blocks on the same page.
  • getCMSSiblingBlocksLinks() - HTML list of sibling block links.

Permissions and lifecycle

  • canView($member = null)
  • canEdit($member = null)
  • canDelete($member = null)
  • canCreate($member = null, $context = [])
  • canDeleteFromLive($member = null)
  • canPublish($member = null)
  • canArchive($member = null)
  • isPublished()
  • isNew()
  • doArchive()

Key protected methods for subclasses

These are not part of the public API, but are useful if you extend Block and override behavior.

  • Layout config and filtering:
    • getLayoutConfig(), getExcludedLayouts(), getLayoutConfigEntry(...), isLayoutDisabled(...)
    • filterLayoutsByClassName(...), filterToConfiguredLayouts(...), filterDisabledLayouts(...)
  • Layout discovery:
    • discoverLayouts(), discoverModuleLayouts(), discoverAdditionalLayouts(), scanTemplateDirectory(...)
  • Icon resolution:
    • getConfiguredLayoutIcon(...), getLayoutIconClass(...), getDiscoveredLayoutIcon(...), getLayoutIconSourcePath()
    • buildLayoutOption(...), getLayoutOptionLabel(...)
  • Template fallback and warnings:
    • resolveTemplateWithFallback(...), getAvailableTemplateClasses(...), isTemplateMissing(), getTemplateMissingNoticeField()
  • URL helpers:
    • getCurrentCMSRecord(), getLinkParentPage(), buildBlockLinkFromParent(...), appendQueryParams(...), appendHash(...)

Todo:

toastnz/blocks-layouts 适用场景与选型建议

toastnz/blocks-layouts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.56k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「layouts」 「modular」 「silverstripe」 「Content Blocks」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 toastnz/blocks-layouts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 toastnz/blocks-layouts 我们能提供哪些服务?
定制开发 / 二次开发

基于 toastnz/blocks-layouts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2022-08-25