From 8a22e5f095d2c3fb8f644267e2aa42d4e61ed78b Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 19 Nov 2021 10:13:45 -0500 Subject: [PATCH] Add support for specifying a background color for repeater items by type. This is likely only useful in RepeaterMatrix but had to be added to the core repeater to support. The way you define it is by specifying a color hex code like #999999 or #999 somewhere in the repeater item type label (I suggest at the end). The color hex code will be removed from the label automatically and used to change its background color. Note that the text color is white, so use only background colors that would work with white text. --- .../InputfieldRepeater.module | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module b/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module index b328b124..c33e1a41 100644 --- a/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module +++ b/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module @@ -327,6 +327,7 @@ class InputfieldRepeater extends Inputfield implements InputfieldItemList { $input = $this->wire()->input; $session = $this->wire()->session; $modules = $this->wire()->modules; + $typeStyles = array(); // if it's already been built, then return the cached version if(!is_null($this->form)) return $this->form; @@ -460,6 +461,10 @@ class InputfieldRepeater extends Inputfield implements InputfieldItemList { $wrapIcon = ''; $wrapClasses = array_unique(array('InputfieldRepeaterItem', $this->className() . 'Item', 'InputfieldNoFocus')); $wrap->addClass(implode(' ', $wrapClasses)); + + $itemType = $this->getRepeaterItemType($page); + $itemTypeName = $this->getRepeaterItemTypeName($itemType); + if(!$isPost) { $wrap->entityEncodeLabel = false; $wrapLabel = @@ -470,12 +475,21 @@ class InputfieldRepeater extends Inputfield implements InputfieldItemList { $wrapLabel = str_replace($matches[0], '', $wrapLabel); $wrapIcon = $matches[1]; } + if(strpos($wrapLabel, '#') !== false && preg_match('/#([0-9a-fA-F]{3,})/', $wrapLabel, $matches)) { + // background-color definition + if(strlen($matches[1]) === 3 || strlen($matches[1]) === 6) { + $wrapLabel = str_replace($matches[0], '', $wrapLabel); + $typeStyles[] = + ".Inputfield_$this->name .InputfieldContent " . + ".InputfieldRepeaterItem[data-typeName=$itemTypeName] > .InputfieldHeader " . + "{ background-color: #$matches[1] }"; + } + } + $wrap->label = $wrapLabel; } else { $wrap->label = "$label " . (++$cnt); } - $itemType = $this->getRepeaterItemType($page); - $itemTypeName = $this->getRepeaterItemTypeName($itemType); $wrap->name = "repeater_item_{$page->id}"; $wrap->wrapAttr('data-page', $page->id); $wrap->wrapAttr('data-type', $itemType); @@ -596,6 +610,11 @@ class InputfieldRepeater extends Inputfield implements InputfieldItemList { // cache $this->form = $form; + + if(!$isPost && count($typeStyles)) { + $styles = ""; + $this->wire()->adminTheme->addExtraMarkup('head', $styles); + } return $form; }