diff --git a/src/fields/ColoritField.php b/src/fields/ColoritField.php index e22dc1b..6f4ea5e 100644 --- a/src/fields/ColoritField.php +++ b/src/fields/ColoritField.php @@ -37,6 +37,8 @@ class ColoritField extends Field public $allowCustomColor = false; public $allowOpacity = false; public $colorFormat = 'auto'; + public $multiSiteSupport = false; + public $siteSettings = []; // Static Methods // ========================================================================= @@ -193,6 +195,48 @@ public function serializeValue($value, ElementInterface $element = null) return parent::serializeValue($serialized, $element); } + public static function supportedTranslationMethods(): array + { + return [self::TRANSLATION_METHOD_NONE]; + } + + /** + * Creates a copy of the field settings and stores them in a site specific array + * @author Josh Smith + * @return array + */ + public function getSettings(): array + { + $data = parent::getSettings(); + if( empty($this->multiSiteSupport) ) return $data; + + $currentSite = Craft::$app->sites->getCurrentSite(); + unset($data['siteSettings']); + + $newSettings = []; + $newSettings[$currentSite->id] = $data; + + // Get the existing field settings if this isn't a new field + $fieldSettings = []; + if( !$this->getIsNew() ){ + + // Fetch the current field settings + $field = Craft::$app->getFields()->getFieldById($this->id); + $fieldSettings = $field->siteSettings ?? []; + + // Restore the current field settings at the top level, ignoring any changes. + // This smooths the UX when toggling from multi vs. standard sites. + $data = $field->getAttributes($field->settingsAttributes()); + } + + // We need to explicity set this flag in case it was overriden above. + // Then, merge together the site settings. + $data['multiSiteSupport'] = true; + $data['siteSettings'] = ($newSettings + $fieldSettings); + + return $data; + } + public function getSettingsHtml() { $field = $this; @@ -202,6 +246,9 @@ public function getSettingsHtml() 'label' => 'No Preset' ]; + // Apply multi-site settinsg + $this->_populateWithSiteSettings(); + if(!$this->presetMode) { $presets = Colorit::$plugin->getPresets()->getAllPresetsByType(self::class); @@ -226,8 +273,8 @@ public function getSettingsHtml() public function getInputHtml($value, ElementInterface $element = null): string { + $this->_populateWithSiteSettings(); $this->_populateWithPreset(); - $view = Craft::$app->getView(); $id = $view->formatInputId($this->handle); $namespacedId = Craft::$app->view->namespaceInputId($id); @@ -281,6 +328,25 @@ public function getPalette() // Static Methods // ========================================================================= + /** + * Populates this field object with site specific settings + * @author Josh Smith + * @return void + */ + private function _populateWithSiteSettings() + { + if( empty($this->siteSettings) || empty($this->multiSiteSupport) ) return; + + $currentSite = Craft::$app->sites->getCurrentSite(); + $settings = $this->siteSettings[$currentSite->id] ?? []; + + foreach ($settings as $key => $value) { + if( property_exists($this, $key) ){ + $this->$key = $value; + } + } + } + private function _populateWithPreset() { if($this->presetId) diff --git a/src/templates/_fields/colorit/settings.twig b/src/templates/_fields/colorit/settings.twig index 1eeef17..381b1d8 100644 --- a/src/templates/_fields/colorit/settings.twig +++ b/src/templates/_fields/colorit/settings.twig @@ -123,3 +123,14 @@ }) }} + +{% if not field.presetMode and craft.app.sites.getTotalSites() > 1 %} + {{ forms.lightswitchField({ + label: "Multi-Site Support"|t('colorit'), + instructions: "Keep this field's settings specific to each site."|t('colorit'), + id: 'multiSiteSupport', + name: 'multiSiteSupport', + on: field.multiSiteSupport, + errors: field.getErrors('multiSiteSupport') + }) }} +{% endif %} \ No newline at end of file