Field Type API

MatrixBlockAnchorField extends craft\base\Field and implements craft\base\PreviewableFieldInterface.

Class: johnhenry\matrixblockanchor\fields\MatrixBlockAnchorField

Methods

normalizeValue(mixed $value, ?ElementInterface $element): string

Returns the resolved anchor ID for a given element. If custom anchors are enabled and the editor has set a value, returns that value (stripped of any # prefix). Otherwise returns {prefix}{separator}{blockId}.

getInputHtml(mixed $value, ?ElementInterface $element): string

Renders the field input. When custom anchors are disabled, renders a read-only display of the auto-generated anchor. When enabled, renders an editable text input alongside the auto-generated anchor as a fallback preview.

getElementValidationRules(): array

Registers the validateAnchorId validator, which enforces:

  • Starts with a letter
  • Contains only [a-zA-Z0-9_-]
  • Does not exceed 100 characters
  • Is unique within the owner entry

getAnchorPrefix(): string

Returns the configured anchor prefix from plugin settings. Defaults to blockIdAnchor.

getAllowCustomAnchors(): bool

Returns whether editors may set a custom anchor on blocks using this field.

getPreviewHtml(mixed $value, ElementInterface $element): string

Returns <code>#anchorId</code> for display in element index preview columns.

Template Output

The field value is the anchor ID without a # prefix. Use it directly as an id attribute:

<section id="{{ block.anchorField }}">

To build a full anchor link:

<a href="#{{ block.anchorField }}">Jump to section</a>