XF 2.1 Getting data for color picker

Arty

Well-known member
I'm using XenForo color picker in my custom tag.

Issue is getting palette for color picker. It seems weird.

My current code:
Code:
// Code to get color data
                $colorData = null;
                $style = \XF::em()->find('XF:Style', $this->styleId, null);
                if ($style) {
                    /** @var \XF\Repository\StyleProperty $propertiesRepo */
                    $propertiesRepo = \XF::repository('XF:StyleProperty');
                    $colorData = $propertiesRepo->getStyleColorData($style);
                }

// Code to add icon picker
            $params = [
...
                'allowPalette' => $colorData !== null ? 'true' : '',
                'includeScripts' => true,
                'colorData' => $colorData,
            ];
            $input .= $this->callMacro('public:color_picker_macros', 'color_picker', $params, $this->defaultParams);
That code is inside function in Templater instance.

Here are 2 weird things:

1. When in admin panel, second line that retrieves $style variable returns null. Code works perfectly in public templates, but when styleId is 0, it fails.

2. Whole thing seems too long. Isn't there an easier way to get colorData array? Its inside Templater instance, so its weird that it requires getting style entity and style properties repository.
 
1. When in admin panel, second line that retrieves $style variable returns null. Code works perfectly in public templates, but when styleId is 0, it fails.

There's no entry for the master style in xf_style in the database, so attempting to fetch an entity that doesn't actually exist will give you null.
 
There's no entry for the master style in xf_style in the database, so attempting to fetch an entity that doesn't actually exist will give you null.
Thanks!

After digging a bit further I've changed code to this:
Code:
                /** @var \XF\Entity\Style $style */
                if ($this->styleId) {
                    $style = \XF::em()->find('XF:Style', $this->styleId, null);
                } else {
                    /** @var \XF\Repository\Style $stylesRepo */
                    $stylesRepo = \XF::repository('XF:Style');
                    $style = $stylesRepo->getMasterStyle();
                }
 
Top Bottom