Dropdown Options in a Repeater set by field setting

Mark asked me to document my problem and my solution for others that may have the same problem:

I have a repeater in ContentBlocks that shows a list of items that can be filtered. Users can set the Filters as a field-settings for each repeater individually.
So Filters are customizable for every repeater-list.

My problem was to get the value (filters) of the repeater field-settting into the dropdown for the items. My solution was to bind a snippet to the dropdown with the following code:

// get CB field
$cbField = $modx->runSnippet('cbGetFieldContent', array(
   'field' => 12,
   'returnAsJSON' => 1,
   'limit' => 1,
));

$cb_values = json_decode($cbField, true);
$filters = explode(',', $cb_values[0]['settings']['filter']);

$options = array();

foreach ($filters as $filter) {
    $value = $modx->runSnippet('translit', array(
       'input' => $filter,
    ));
    
    $options[] = array(
        'value' => $value,
        'display' => $filter,
    );
}

return json_encode($options);

(The Translit-Snippet just removes all special characters and trims the whitespaces)

So if may someone has the same problem. Here is an idea how to. :slight_smile:

2 Likes

Thanks for sharing!

I had a filtered image gallery, for which I’ve used moreGallery and its tag-feature. But nice to see a repeater-solution to filtered items! :+1: