Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion src/fields/ColoritField.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ColoritField extends Field
public $allowCustomColor = false;
public $allowOpacity = false;
public $colorFormat = 'auto';
public $multiSiteSupport = false;
public $siteSettings = [];

// Static Methods
// =========================================================================
Expand Down Expand Up @@ -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 <me@joshsmith.dev>
* @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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -281,6 +328,25 @@ public function getPalette()
// Static Methods
// =========================================================================

/**
* Populates this field object with site specific settings
* @author Josh Smith <me@joshsmith.dev>
* @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)
Expand Down
11 changes: 11 additions & 0 deletions src/templates/_fields/colorit/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,14 @@
}) }}

</div>

{% 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 %}