fromholdio/silverstripe-dbhtmlanchors

快速简便地识别 DBHTMLText 和 DBHTMLVarchar 字段中的锚点

安装次数: 1,077

依赖关系: 0

建议者: 1

安全性: 0

星标: 2

关注者: 2

分支: 0

类型:silverstripe-vendormodule

2.0.0 2024-07-22 08:01 UTC

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 应用到 DBHTMLTextDBHTMLVarchar

这为每个 DBFields 添加了 getAnchors() 访问器。

当在字段对象上调用时,它会处理任何简码,然后搜索 HTML 元素中可用的 nameid 属性,这些属性可以用作锚点链接的目标。

锚点值的列表作为简单的数组返回。

使用示例

DBHTMLTextDBHTMLVarchar 添加到您的数据对象中。常见的例子是 SiteTreeContent 字段。

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'
]