From 80e3140149f1ce02ae9ff84ed9b0e62ec53da44d Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Wed, 19 Jun 2019 10:56:07 +1200 Subject: [PATCH 1/2] Implemented multi-site field settings. --- src/fields/ColoritField.php | 52 ++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/fields/ColoritField.php b/src/fields/ColoritField.php index e22dc1b..f97e8e6 100644 --- a/src/fields/ColoritField.php +++ b/src/fields/ColoritField.php @@ -37,6 +37,7 @@ class ColoritField extends Field public $allowCustomColor = false; public $allowOpacity = false; public $colorFormat = 'auto'; + public $siteSettings = []; // Static Methods // ========================================================================= @@ -193,6 +194,33 @@ public function serializeValue($value, ElementInterface $element = null) return parent::serializeValue($serialized, $element); } + /** + * 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(); + $currentSite = Craft::$app->sites->getCurrentSite(); + + // Create a site specific copy of the settings + $settings = $data; unset($settings['siteSettings']); + $data['siteSettings'][$currentSite->id] = $settings; + + // Get the existing field settings if this isn't a new field + $fieldSettings = []; + if( !$this->getIsNew() ){ + $field = Craft::$app->getFields()->getFieldById($this->id); + $fieldSettings = $field->siteSettings ?? []; + } + + // Merge the site settings + $data['siteSettings'] = ($fieldSettings + $data['siteSettings']); + + return $data; + } + public function getSettingsHtml() { $field = $this; @@ -202,6 +230,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 +257,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 +312,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) ) 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) From c9c723f8f0ac913c1ed962f438d46aa69330a729 Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Wed, 19 Jun 2019 17:28:09 +1200 Subject: [PATCH 2/2] Few tweaks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + Added an option to choose whether to apply colors across all sites, or keep setting specific to each site. + Removed the translation select box. + Ensured the “site wide” changes don’t get modified when site specific changes are made. --- src/fields/ColoritField.php | 28 ++++++++++++++++----- src/templates/_fields/colorit/settings.twig | 11 ++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/fields/ColoritField.php b/src/fields/ColoritField.php index f97e8e6..6f4ea5e 100644 --- a/src/fields/ColoritField.php +++ b/src/fields/ColoritField.php @@ -37,6 +37,7 @@ class ColoritField extends Field public $allowCustomColor = false; public $allowOpacity = false; public $colorFormat = 'auto'; + public $multiSiteSupport = false; public $siteSettings = []; // Static Methods @@ -194,6 +195,11 @@ 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 @@ -202,21 +208,31 @@ public function serializeValue($value, ElementInterface $element = null) public function getSettings(): array { $data = parent::getSettings(); + if( empty($this->multiSiteSupport) ) return $data; + $currentSite = Craft::$app->sites->getCurrentSite(); + unset($data['siteSettings']); - // Create a site specific copy of the settings - $settings = $data; unset($settings['siteSettings']); - $data['siteSettings'][$currentSite->id] = $settings; + $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()); } - // Merge the site settings - $data['siteSettings'] = ($fieldSettings + $data['siteSettings']); + // 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; } @@ -319,7 +335,7 @@ public function getPalette() */ private function _populateWithSiteSettings() { - if( empty($this->siteSettings) ) return; + if( empty($this->siteSettings) || empty($this->multiSiteSupport) ) return; $currentSite = Craft::$app->sites->getCurrentSite(); $settings = $this->siteSettings[$currentSite->id] ?? []; 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