From c5e8c263fff67ee3f13775af19ca7e450c2d9721 Mon Sep 17 00:00:00 2001 From: "alexgilbertdesign@gmail.com" Date: Thu, 29 Aug 2019 10:24:20 -0400 Subject: [PATCH 1/5] Fix support for multi-site --- src/fields/Checkboxes.php | 29 +++++++++++++++++++---------- src/fields/Dropdown.php | 31 ++++++++++++++++++++----------- src/fields/Multiselect.php | 37 +++++++++++++++++++++++-------------- src/fields/Radiobuttons.php | 31 ++++++++++++++++++++----------- 4 files changed, 82 insertions(+), 46 deletions(-) diff --git a/src/fields/Checkboxes.php b/src/fields/Checkboxes.php index bbc355b..214befb 100644 --- a/src/fields/Checkboxes.php +++ b/src/fields/Checkboxes.php @@ -30,7 +30,7 @@ public function getSettingsHtml() ] ); } - + public function getContentColumnType(): string { return $this->columnType; @@ -41,14 +41,23 @@ public function normalizeValue($value, ElementInterface $element = null): string $view = Craft::$app->getView(); $templateMode = $view->getTemplateMode(); $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - + $variables['element'] = $element; $variables['this'] = $this; - + + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle()); + } + $options = json_decode('[' . $view->renderString($this->checkboxOptions, $variables) . ']', true); $view->setTemplateMode($templateMode); - + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } + if ($this->isFresh($element)) : foreach ($options as $key => $option) : if (!empty($option['default'])) : @@ -56,14 +65,14 @@ public function normalizeValue($value, ElementInterface $element = null): string endif; endforeach; endif; - + if (is_array($value)) : $value = json_encode($value); endif; - + return (is_null($value) ? '' : $value); } - + public function getInputHtml($value, ElementInterface $element = null): string { $view = Craft::$app->getView(); @@ -72,11 +81,11 @@ public function getInputHtml($value, ElementInterface $element = null): string $variables['element'] = $element; $variables['this'] = $this; - + $options = json_decode('[' . $view->renderString($this->checkboxOptions, $variables) . ']', true); - + $view->setTemplateMode($templateMode); - + return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/checkboxGroup', [ 'name' => $this->handle, 'values' => $value, diff --git a/src/fields/Dropdown.php b/src/fields/Dropdown.php index 4c69903..2777c23 100644 --- a/src/fields/Dropdown.php +++ b/src/fields/Dropdown.php @@ -13,7 +13,7 @@ class Dropdown extends Field { - + public $dropdownOptions = ''; public $columnType = 'text'; @@ -31,25 +31,34 @@ public function getSettingsHtml() ] ); } - + public function getContentColumnType(): string { return $this->columnType; } public function normalizeValue($value, ElementInterface $element = null): string - { + { $view = Craft::$app->getView(); $templateMode = $view->getTemplateMode(); $view->setTemplateMode($view::TEMPLATE_MODE_SITE); $variables['element'] = $element; $variables['this'] = $this; - + + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle()); + } + $options = json_decode('[' . $view->renderString($this->dropdownOptions, $variables) . ']', true); - + $view->setTemplateMode($templateMode); - + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } + if ($this->isFresh($element) ) : foreach ($options as $key => $option) : if (!empty($option['default'])) : @@ -57,10 +66,10 @@ public function normalizeValue($value, ElementInterface $element = null): string endif; endforeach; endif; - + return (is_null($value) ? '' : $value); } - + public function getInputHtml($value, ElementInterface $element = null): string { $view = Craft::$app->getView(); @@ -69,11 +78,11 @@ public function getInputHtml($value, ElementInterface $element = null): string $variables['element'] = $element; $variables['this'] = $this; - + $options = json_decode('[' . $view->renderString($this->dropdownOptions, $variables) . ']', true); - + $view->setTemplateMode($templateMode); - + return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/select', [ 'name' => $this->handle, 'value' => $value, diff --git a/src/fields/Multiselect.php b/src/fields/Multiselect.php index 80bf037..493b8e6 100644 --- a/src/fields/Multiselect.php +++ b/src/fields/Multiselect.php @@ -32,7 +32,7 @@ public function getSettingsHtml() ] ); } - + public function getContentColumnType(): string { return $this->columnType; @@ -43,14 +43,23 @@ public function normalizeValue($value, ElementInterface $element = null): string $view = Craft::$app->getView(); $templateMode = $view->getTemplateMode(); $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - + $variables['element'] = $element; $variables['this'] = $this; - + + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle()); + } + $options = json_decode('[' . $view->renderString($this->multiselectOptions, $variables) . ']', true); - + $view->setTemplateMode($templateMode); - + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } + if ($this->isFresh($element)) : foreach ($options as $key => $option) : if (!empty($option['default'])) : @@ -58,32 +67,32 @@ public function normalizeValue($value, ElementInterface $element = null): string endif; endforeach; endif; - + if (is_array($value)) : $value = json_encode($value); endif; - + return (is_null($value) ? '' : $value); } - + public function getInputHtml($value, ElementInterface $element = null): string { $view = Craft::$app->getView(); $templateMode = $view->getTemplateMode(); $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - + $variables['element'] = $element; $variables['this'] = $this; - + $options = json_decode('[' . $view->renderString($this->multiselectOptions, $variables) . ']', true); - + $view->setTemplateMode($templateMode); - + return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/multiselect', [ 'name' => $this->handle, 'values' => $value, 'options' => $options, 'size' => ($this->fieldHeight > 0 ? $this->fieldHeight : count($options)) ]); - } -} \ No newline at end of file + } +} diff --git a/src/fields/Radiobuttons.php b/src/fields/Radiobuttons.php index 0571e90..ab90f97 100644 --- a/src/fields/Radiobuttons.php +++ b/src/fields/Radiobuttons.php @@ -13,7 +13,7 @@ class Radiobuttons extends Field { - + public $radioOptions = ''; public $columnType = 'text'; @@ -31,25 +31,34 @@ public function getSettingsHtml() ] ); } - + public function getContentColumnType(): string { return $this->columnType; } public function normalizeValue($value, ElementInterface $element = null): string - { + { $view = Craft::$app->getView(); $templateMode = $view->getTemplateMode(); $view->setTemplateMode($view::TEMPLATE_MODE_SITE); $variables['element'] = $element; $variables['this'] = $this; - + + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle()); + } + $options = json_decode('[' . $view->renderString($this->radioOptions, $variables) . ']', true); - + $view->setTemplateMode($templateMode); - + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } + if ($this->isFresh($element) ) : foreach ($options as $key => $option) : if (!empty($option['default'])) : @@ -57,10 +66,10 @@ public function normalizeValue($value, ElementInterface $element = null): string endif; endforeach; endif; - + return (is_null($value) ? '' : $value); } - + public function getInputHtml($value, ElementInterface $element = null): string { $view = Craft::$app->getView(); @@ -69,11 +78,11 @@ public function getInputHtml($value, ElementInterface $element = null): string $variables['element'] = $element; $variables['this'] = $this; - + $options = json_decode('[' . $view->renderString($this->radioOptions, $variables) . ']', true); - + $view->setTemplateMode($templateMode); - + return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/radioGroup', [ 'name' => $this->handle, 'value' => $value, From 4b9fd0a2ecb66d94fe1c610813d6768ec514b50c Mon Sep 17 00:00:00 2001 From: "alexgilbertdesign@gmail.com" Date: Thu, 29 Aug 2019 10:26:07 -0400 Subject: [PATCH 2/5] Fix indent --- .idea/craft-dynamic-fields.iml | 18 ++ .idea/encodings.xml | 4 + .idea/inspectionProfiles/Project_Default.xml | 27 +++ .idea/misc.xml | 10 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 222 +++++++++++++++++++ src/fields/Checkboxes.php | 82 +++---- src/fields/Dropdown.php | 74 +++---- src/fields/Multiselect.php | 80 +++---- src/fields/Radiobuttons.php | 72 +++--- 11 files changed, 449 insertions(+), 154 deletions(-) create mode 100644 .idea/craft-dynamic-fields.iml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/craft-dynamic-fields.iml b/.idea/craft-dynamic-fields.iml new file mode 100644 index 0000000..0e630a2 --- /dev/null +++ b/.idea/craft-dynamic-fields.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..13e2911 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,27 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f3c4354 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..cf4f05c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..b9856f9 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,222 @@ + + + + + + + + + + + + $PROJECT_DIR$/composer.json + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $USER_HOME$/.nvm/versions/node/v6.6.0/bin/bower + + + + true + + false + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - 1567088704193 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Python 2.7.12 (/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7) - - - - - - - - \ No newline at end of file From 57ce3c280355773e324be953bdfed06d5535e6db Mon Sep 17 00:00:00 2001 From: "alexgilbertdesign@gmail.com" Date: Thu, 29 Aug 2019 10:44:01 -0400 Subject: [PATCH 4/5] Fix function call --- src/fields/Checkboxes.php | 82 ++++++++++++++++++------------------- src/fields/Dropdown.php | 74 ++++++++++++++++----------------- src/fields/Multiselect.php | 80 ++++++++++++++++++------------------ src/fields/Radiobuttons.php | 72 ++++++++++++++++---------------- 4 files changed, 154 insertions(+), 154 deletions(-) diff --git a/src/fields/Checkboxes.php b/src/fields/Checkboxes.php index 204d68d..b6e6d54 100644 --- a/src/fields/Checkboxes.php +++ b/src/fields/Checkboxes.php @@ -38,55 +38,55 @@ public function getContentColumnType(): string public function normalizeValue($value, ElementInterface $element = null): string { - $view = Craft::$app->getView(); - $templateMode = $view->getTemplateMode(); - $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - - $variables['element'] = $element; - $variables['this'] = $this; - - $siteHandle = Craft::$app->request->get('site', null); - $currentSite = Craft::$app->sites->currentSite; - if ($siteHandle && $siteHandle !== $currentSite->handle) { - Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle()); - } - - $options = json_decode('[' . $view->renderString($this->checkboxOptions, $variables) . ']', true); - - $view->setTemplateMode($templateMode); - if ($siteHandle !== $currentSite->handle) { - Craft::$app->sites->setCurrentSite($currentSite); - } - - if ($this->isFresh($element)) : - foreach ($options as $key => $option) : - if (!empty($option['default'])) : - $value[] = $option['value']; - endif; - endforeach; - endif; - - if (is_array($value)) : - $value = json_encode($value); - endif; - - return (is_null($value) ? '' : $value); + $view = Craft::$app->getView(); + $templateMode = $view->getTemplateMode(); + $view->setTemplateMode($view::TEMPLATE_MODE_SITE); + + $variables['element'] = $element; + $variables['this'] = $this; + + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle($siteHandle)); + } + + $options = json_decode('[' . $view->renderString($this->checkboxOptions, $variables) . ']', true); + + $view->setTemplateMode($templateMode); + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } + + if ($this->isFresh($element)) : + foreach ($options as $key => $option) : + if (!empty($option['default'])) : + $value[] = $option['value']; + endif; + endforeach; + endif; + + if (is_array($value)) : + $value = json_encode($value); + endif; + + return (is_null($value) ? '' : $value); } public function getInputHtml($value, ElementInterface $element = null): string { - $view = Craft::$app->getView(); - $templateMode = $view->getTemplateMode(); - $view->setTemplateMode($view::TEMPLATE_MODE_SITE); + $view = Craft::$app->getView(); + $templateMode = $view->getTemplateMode(); + $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - $variables['element'] = $element; - $variables['this'] = $this; + $variables['element'] = $element; + $variables['this'] = $this; - $options = json_decode('[' . $view->renderString($this->checkboxOptions, $variables) . ']', true); + $options = json_decode('[' . $view->renderString($this->checkboxOptions, $variables) . ']', true); - $view->setTemplateMode($templateMode); + $view->setTemplateMode($templateMode); - return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/checkboxGroup', [ + return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/checkboxGroup', [ 'name' => $this->handle, 'values' => $value, 'options' => $options diff --git a/src/fields/Dropdown.php b/src/fields/Dropdown.php index e978aac..0a8c4a2 100644 --- a/src/fields/Dropdown.php +++ b/src/fields/Dropdown.php @@ -39,51 +39,51 @@ public function getContentColumnType(): string public function normalizeValue($value, ElementInterface $element = null): string { - $view = Craft::$app->getView(); - $templateMode = $view->getTemplateMode(); - $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - - $variables['element'] = $element; - $variables['this'] = $this; - - $siteHandle = Craft::$app->request->get('site', null); - $currentSite = Craft::$app->sites->currentSite; - if ($siteHandle && $siteHandle !== $currentSite->handle) { - Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle()); - } - - $options = json_decode('[' . $view->renderString($this->dropdownOptions, $variables) . ']', true); - - $view->setTemplateMode($templateMode); - if ($siteHandle !== $currentSite->handle) { - Craft::$app->sites->setCurrentSite($currentSite); - } - - if ($this->isFresh($element)) : - foreach ($options as $key => $option) : - if (!empty($option['default'])) : - $value = $option['value']; - endif; - endforeach; - endif; - - return (is_null($value) ? '' : $value); + $view = Craft::$app->getView(); + $templateMode = $view->getTemplateMode(); + $view->setTemplateMode($view::TEMPLATE_MODE_SITE); + + $variables['element'] = $element; + $variables['this'] = $this; + + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle($siteHandle)); + } + + $options = json_decode('[' . $view->renderString($this->dropdownOptions, $variables) . ']', true); + + $view->setTemplateMode($templateMode); + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } + + if ($this->isFresh($element) ) : + foreach ($options as $key => $option) : + if (!empty($option['default'])) : + $value = $option['value']; + endif; + endforeach; + endif; + + return (is_null($value) ? '' : $value); } public function getInputHtml($value, ElementInterface $element = null): string { - $view = Craft::$app->getView(); - $templateMode = $view->getTemplateMode(); - $view->setTemplateMode($view::TEMPLATE_MODE_SITE); + $view = Craft::$app->getView(); + $templateMode = $view->getTemplateMode(); + $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - $variables['element'] = $element; - $variables['this'] = $this; + $variables['element'] = $element; + $variables['this'] = $this; - $options = json_decode('[' . $view->renderString($this->dropdownOptions, $variables) . ']', true); + $options = json_decode('[' . $view->renderString($this->dropdownOptions, $variables) . ']', true); - $view->setTemplateMode($templateMode); + $view->setTemplateMode($templateMode); - return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/select', [ + return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/select', [ 'name' => $this->handle, 'value' => $value, 'options' => $options diff --git a/src/fields/Multiselect.php b/src/fields/Multiselect.php index 2c6f3ce..ec9afd0 100644 --- a/src/fields/Multiselect.php +++ b/src/fields/Multiselect.php @@ -40,53 +40,53 @@ public function getContentColumnType(): string public function normalizeValue($value, ElementInterface $element = null): string { - $view = Craft::$app->getView(); - $templateMode = $view->getTemplateMode(); - $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - - $variables['element'] = $element; - $variables['this'] = $this; - - $siteHandle = Craft::$app->request->get('site', null); - $currentSite = Craft::$app->sites->currentSite; - if ($siteHandle && $siteHandle !== $currentSite->handle) { - Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle()); - } - - $options = json_decode('[' . $view->renderString($this->multiselectOptions, $variables) . ']', true); - - $view->setTemplateMode($templateMode); - if ($siteHandle !== $currentSite->handle) { - Craft::$app->sites->setCurrentSite($currentSite); - } - - if ($this->isFresh($element)) : - foreach ($options as $key => $option) : - if (!empty($option['default'])) : - $value[] = $option['value']; - endif; - endforeach; - endif; - - if (is_array($value)) : - $value = json_encode($value); - endif; - - return (is_null($value) ? '' : $value); + $view = Craft::$app->getView(); + $templateMode = $view->getTemplateMode(); + $view->setTemplateMode($view::TEMPLATE_MODE_SITE); + + $variables['element'] = $element; + $variables['this'] = $this; + + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle($siteHandle)); + } + + $options = json_decode('[' . $view->renderString($this->multiselectOptions, $variables) . ']', true); + + $view->setTemplateMode($templateMode); + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } + + if ($this->isFresh($element)) : + foreach ($options as $key => $option) : + if (!empty($option['default'])) : + $value[] = $option['value']; + endif; + endforeach; + endif; + + if (is_array($value)) : + $value = json_encode($value); + endif; + + return (is_null($value) ? '' : $value); } public function getInputHtml($value, ElementInterface $element = null): string { - $view = Craft::$app->getView(); - $templateMode = $view->getTemplateMode(); - $view->setTemplateMode($view::TEMPLATE_MODE_SITE); + $view = Craft::$app->getView(); + $templateMode = $view->getTemplateMode(); + $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - $variables['element'] = $element; - $variables['this'] = $this; + $variables['element'] = $element; + $variables['this'] = $this; - $options = json_decode('[' . $view->renderString($this->multiselectOptions, $variables) . ']', true); + $options = json_decode('[' . $view->renderString($this->multiselectOptions, $variables) . ']', true); - $view->setTemplateMode($templateMode); + $view->setTemplateMode($templateMode); return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/multiselect', [ 'name' => $this->handle, diff --git a/src/fields/Radiobuttons.php b/src/fields/Radiobuttons.php index 39c558d..3c49cd6 100644 --- a/src/fields/Radiobuttons.php +++ b/src/fields/Radiobuttons.php @@ -39,49 +39,49 @@ public function getContentColumnType(): string public function normalizeValue($value, ElementInterface $element = null): string { - $view = Craft::$app->getView(); - $templateMode = $view->getTemplateMode(); - $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - - $variables['element'] = $element; - $variables['this'] = $this; - - $siteHandle = Craft::$app->request->get('site', null); - $currentSite = Craft::$app->sites->currentSite; - if ($siteHandle && $siteHandle !== $currentSite->handle) { - Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle()); - } - - $options = json_decode('[' . $view->renderString($this->radioOptions, $variables) . ']', true); - - $view->setTemplateMode($templateMode); - if ($siteHandle !== $currentSite->handle) { - Craft::$app->sites->setCurrentSite($currentSite); - } - - if ($this->isFresh($element)) : - foreach ($options as $key => $option) : - if (!empty($option['default'])) : - $value = $option['value']; - endif; - endforeach; - endif; - - return (is_null($value) ? '' : $value); + $view = Craft::$app->getView(); + $templateMode = $view->getTemplateMode(); + $view->setTemplateMode($view::TEMPLATE_MODE_SITE); + + $variables['element'] = $element; + $variables['this'] = $this; + + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle($siteHandle)); + } + + $options = json_decode('[' . $view->renderString($this->radioOptions, $variables) . ']', true); + + $view->setTemplateMode($templateMode); + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } + + if ($this->isFresh($element) ) : + foreach ($options as $key => $option) : + if (!empty($option['default'])) : + $value = $option['value']; + endif; + endforeach; + endif; + + return (is_null($value) ? '' : $value); } public function getInputHtml($value, ElementInterface $element = null): string { - $view = Craft::$app->getView(); - $templateMode = $view->getTemplateMode(); - $view->setTemplateMode($view::TEMPLATE_MODE_SITE); + $view = Craft::$app->getView(); + $templateMode = $view->getTemplateMode(); + $view->setTemplateMode($view::TEMPLATE_MODE_SITE); - $variables['element'] = $element; - $variables['this'] = $this; + $variables['element'] = $element; + $variables['this'] = $this; - $options = json_decode('[' . $view->renderString($this->radioOptions, $variables) . ']', true); + $options = json_decode('[' . $view->renderString($this->radioOptions, $variables) . ']', true); - $view->setTemplateMode($templateMode); + $view->setTemplateMode($templateMode); return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/radioGroup', [ 'name' => $this->handle, From 68cc9be8c64ad2465375560ca8c98f6e3694078b Mon Sep 17 00:00:00 2001 From: "alexgilbertdesign@gmail.com" Date: Thu, 29 Aug 2019 10:48:02 -0400 Subject: [PATCH 5/5] Had to add it to input html too --- src/fields/Checkboxes.php | 9 +++++++++ src/fields/Dropdown.php | 9 +++++++++ src/fields/Multiselect.php | 9 +++++++++ src/fields/Radiobuttons.php | 9 +++++++++ 4 files changed, 36 insertions(+) diff --git a/src/fields/Checkboxes.php b/src/fields/Checkboxes.php index b6e6d54..9de46d0 100644 --- a/src/fields/Checkboxes.php +++ b/src/fields/Checkboxes.php @@ -82,9 +82,18 @@ public function getInputHtml($value, ElementInterface $element = null): string $variables['element'] = $element; $variables['this'] = $this; + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle($siteHandle)); + } + $options = json_decode('[' . $view->renderString($this->checkboxOptions, $variables) . ']', true); $view->setTemplateMode($templateMode); + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/checkboxGroup', [ 'name' => $this->handle, diff --git a/src/fields/Dropdown.php b/src/fields/Dropdown.php index 0a8c4a2..bc81525 100644 --- a/src/fields/Dropdown.php +++ b/src/fields/Dropdown.php @@ -79,9 +79,18 @@ public function getInputHtml($value, ElementInterface $element = null): string $variables['element'] = $element; $variables['this'] = $this; + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle($siteHandle)); + } + $options = json_decode('[' . $view->renderString($this->dropdownOptions, $variables) . ']', true); $view->setTemplateMode($templateMode); + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/select', [ 'name' => $this->handle, diff --git a/src/fields/Multiselect.php b/src/fields/Multiselect.php index ec9afd0..3069b54 100644 --- a/src/fields/Multiselect.php +++ b/src/fields/Multiselect.php @@ -83,10 +83,19 @@ public function getInputHtml($value, ElementInterface $element = null): string $variables['element'] = $element; $variables['this'] = $this; + + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle($siteHandle)); + } $options = json_decode('[' . $view->renderString($this->multiselectOptions, $variables) . ']', true); $view->setTemplateMode($templateMode); + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/multiselect', [ 'name' => $this->handle, diff --git a/src/fields/Radiobuttons.php b/src/fields/Radiobuttons.php index 3c49cd6..f98c8c6 100644 --- a/src/fields/Radiobuttons.php +++ b/src/fields/Radiobuttons.php @@ -79,9 +79,18 @@ public function getInputHtml($value, ElementInterface $element = null): string $variables['element'] = $element; $variables['this'] = $this; + $siteHandle = Craft::$app->request->get('site', null); + $currentSite = Craft::$app->sites->currentSite; + if ($siteHandle && $siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite(Craft::$app->sites->getSiteByHandle($siteHandle)); + } + $options = json_decode('[' . $view->renderString($this->radioOptions, $variables) . ']', true); $view->setTemplateMode($templateMode); + if ($siteHandle !== $currentSite->handle) { + Craft::$app->sites->setCurrentSite($currentSite); + } return Craft::$app->getView()->renderTemplate('craft-dynamic-fields/_includes/forms/radioGroup', [ 'name' => $this->handle,