fromholdio / silverstripe-dbhtmlanchors
快速简便地识别 DBHTMLText 和 DBHTMLVarchar 字段中的锚点
2.0.0
2024-07-22 08:01 UTC
Requires
- silverstripe/framework: ^5.0
This package is auto-updated.
Last update: 2024-09-22 08:22:28 UTC
README
快速简便地识别 DBHTMLText 和 DBHTMLVarchar 字段中的锚点。
要求
SilverStripe 4
安装
composer require fromholdio/silverstripe-dbhtmlanchors
详细说明
安装后,自动将 DBHTMLAnchorsExtension
应用到 DBHTMLText
和 DBHTMLVarchar
。
这为每个 DBFields 添加了 getAnchors()
访问器。
当在字段对象上调用时,它会处理任何简码,然后搜索 HTML 元素中可用的 name
和 id
属性,这些属性可以用作锚点链接的目标。
锚点值的列表作为简单的数组返回。
使用示例
将 DBHTMLText
或 DBHTMLVarchar
添加到您的数据对象中。常见的例子是 SiteTree
的 Content
字段。
private static $db = [ 'Content' => 'HTMLText' ];
在您的代码中获取字段对象,并调用 getAnchors()
。确保您获取的是字段对象,而不是字段值。
// CORRECT $contentField = $this->dbObject('Content'); $anchors = $contentField->getAnchors(); // WRONG $contentField = $this->Content; $anchors = $contentField->getAnchors();
返回的值将是没有结果时的 null
,或者是一个包含键和值都包含锚点值的关联数组。
// return value from ->getAnchors() [ 'sectionone' => 'sectionone', 'sectiontwo' => 'sectiontwo' ]