From 51b9628dfc7fcc9520856bb1aeedeb6b4a60c798 Mon Sep 17 00:00:00 2001 From: nateiler Date: Mon, 8 Apr 2019 20:53:49 -0600 Subject: [PATCH] set default text (if nothing is present) --- composer.json | 2 +- .../fieldtypes/Link/types/email/settings.twig | 9 ++++++++ .../fieldtypes/Link/types/settings.twig | 10 ++++++++- .../fieldtypes/Link/types/url/settings.twig | 11 +++++++++- src/types/Email.php | 22 +++++++++++++++++-- src/types/Url.php | 22 +++++++++---------- 6 files changed, 60 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 207d7fa..d3d955f 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "flipboxfactory/craft-link", "description": "Link Field Type for Craft CMS", - "version": "1.1.3", + "version": "1.1.2", "type": "craft-plugin", "keywords": [ "flipbox", diff --git a/src/templates/_components/fieldtypes/Link/types/email/settings.twig b/src/templates/_components/fieldtypes/Link/types/email/settings.twig index 6238ed7..08c7521 100644 --- a/src/templates/_components/fieldtypes/Link/types/email/settings.twig +++ b/src/templates/_components/fieldtypes/Link/types/email/settings.twig @@ -11,4 +11,13 @@ id: 'placeholder', value: type.settings.placeholder }) }} +{% endblock %} + +{% block notRequiredTextToggle %} + {{ forms.lightswitchField({ + label: 'Use Email as default text'|t('link'), + name: 'useEmailAsDefaultText', + id: 'useEmailAsDefaultText', + on: type.settings.useEmailAsDefaultText, + }) }} {% endblock %} \ No newline at end of file diff --git a/src/templates/_components/fieldtypes/Link/types/settings.twig b/src/templates/_components/fieldtypes/Link/types/settings.twig index 4de8e80..08f318e 100644 --- a/src/templates/_components/fieldtypes/Link/types/settings.twig +++ b/src/templates/_components/fieldtypes/Link/types/settings.twig @@ -25,8 +25,16 @@ instructions: 'Require the user defined text field to be populated'|t('link'), name: 'requireText', id: 'requireText', - on: type.settings.requireText + on: type.settings.requireText, + toggle: "text-require-text-field", + reverseToggle: "text-not-require-text-field" }) }} +
+ {% block requiredTextToggle %}{% endblock %} +
+
+ {% block notRequiredTextToggle %}{% endblock %} +
{{ forms.lightswitchField({ diff --git a/src/templates/_components/fieldtypes/Link/types/url/settings.twig b/src/templates/_components/fieldtypes/Link/types/url/settings.twig index 6c6c106..395e34c 100644 --- a/src/templates/_components/fieldtypes/Link/types/url/settings.twig +++ b/src/templates/_components/fieldtypes/Link/types/url/settings.twig @@ -11,4 +11,13 @@ id: 'placeholder', value: type.settings.placeholder }) }} -{% endblock %} \ No newline at end of file +{% endblock %} + +{% block notRequiredTextToggle %} + {{ forms.lightswitchField({ + label: 'Use URL as default text'|t('link'), + name: 'useUrlAsDefaultText', + id: 'useUrlAsDefaultText', + on: type.settings.useUrlAsDefaultText, + }) }} +{% endblock %} diff --git a/src/types/Email.php b/src/types/Email.php index f9bed4e..e855eab 100644 --- a/src/types/Email.php +++ b/src/types/Email.php @@ -32,7 +32,12 @@ class Email extends AbstractType const INPUT_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/input'; /** - * @var + * @var bool + */ + public $useEmailAsDefaultText = true; + + /** + * @var string */ public $email; @@ -49,6 +54,18 @@ public static function displayName(): string return Link::t('Email'); } + /** + * @return string|null + */ + public function getText() + { + if ($this->allowText && $this->overrideText !== null) { + return $this->overrideText; + } + + return $this->useEmailAsDefaultText ? $this->email : null; + } + /** * @return string */ @@ -65,7 +82,8 @@ public function settings(): array return array_merge( parent::settings(), [ - 'placeholder' + 'placeholder', + 'useEmailAsDefaultText' ] ); } diff --git a/src/types/Url.php b/src/types/Url.php index f3e0409..bd4b06e 100644 --- a/src/types/Url.php +++ b/src/types/Url.php @@ -32,7 +32,12 @@ class Url extends AbstractType const INPUT_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/input'; /** - * @var + * @var bool + */ + public $useUrlAsDefaultText = true; + + /** + * @var string */ public $url; @@ -54,17 +59,11 @@ public static function displayName(): string */ public function getText() { - if ($this->allowText) { - if ($this->overrideText !== null) { - return $this->overrideText; - } - - if (!$this->requireText) { - return null; - } + if ($this->allowText && $this->overrideText !== null) { + return $this->overrideText; } - return $this->getUrl(); + return $this->useUrlAsDefaultText ? $this->getUrl() : null; } /** @@ -83,7 +82,8 @@ public function settings(): array return array_merge( parent::settings(), [ - 'placeholder' + 'placeholder', + 'useUrlAsDefaultText' ] ); }