Skip to content

Toc modifier not working with Bard field (statamic 4) #21

@gdevdeiv

Description

@gdevdeiv

Hi there,

I have installed the addon in a new Statamic 4 project.
There seems to be a problem injecting the IDs in the content headers.

Parser's injectIds function expects its first param $value to be a string or an array of strings (see preg_replace_callback).
When using Bard, $value is an array of \Statamic\Fields\Values objects.

I am not sure about the bast way to solve this, but I've tried this locally. I appreciate feedback.

/**
 * Injects id directives in header HTML elements.
 * @return string|array
 */
public function injectIds($value, $params = null)
{
    // Handles an array (we could also check the type of the items in the array)
    if (is_array($value)) {
        return array_map(function (Values $valueItem) use ($params) {
            if ($valueItem['type'] === 'text') {
                return new Values(
                    array_merge(
                        $valueItem->all(),
                        ['text' => $this->getInjectedText($valueItem['text'], $params)]
                    )
                );
            }
            return $valueItem;
        }, $value);
    }

    return $this->getInjectedText($value, $params);
}

private function getInjectedText(string $text, ?array $params): string
{
    return preg_replace_callback(
       ...
    );
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions