Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gdevdeiv opened this issue Feb 1, 2024 · 0 comments
Open

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

gdevdeiv opened this issue Feb 1, 2024 · 0 comments

Comments

@gdevdeiv
Copy link

gdevdeiv commented Feb 1, 2024

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(
       ...
    );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant