Accessing ContentBlocks settings in a snippet

Hello,

I’m trying to create a snippet that highlights specific words for a heading field in ContentBlocks.

Here is what the field looks like in the front-end:

In this example “About” should get a yellow highlight which works.
However, I want the user to be able to select the color of the highlight, hence the setting “Highlight color”.

The ContentBlocks field looks like this:

<[[+level]]>[[+value:addHeadingHighlight]]</[[+level]]>

And the snippet looks like this:

<?php
$input = $modx->getOption('input',$scriptProperties,'');
$highlight = $modx->getOption('highlight',$scriptProperties,'');

// Search for matches within the input
preg_match_all('/##[^##]+##/', $input, $result);

// Loop trough the results and place the underline.
foreach($result[0] as $res) {
    $input = str_replace($res, '<span class="'.$highlight.'">'.str_replace('#', '', $res).'</span>', $input);
}

// Return the string
return $input;

Is it possible to retrieve the “Highlight color” setting in this snippet?

I did find the cbGetFieldContent snippet, however I don’t think it’s possible to use this within the setting to apply the correct class to the heading.

I’ve also tried searching the internet and reading through the ContentBlocks docs, but unfortunately I couldn’t find anyone who had the same issue (perhaps I’m searching for something obvious :wink: ).

Any help would be appreciated!

Thomas

Maybe instead of using this [[+value:addHeadingHighlight]], pass the value of the color setting as a property to the snippet. Something like:

[[addHeadingHighlight? &input=`[[+value]]` &highlight=`[[+highlight_color]]`]]
1 Like

Oh man, totally forgot that that was a possibility.

Thanks for the help, it worked!

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.