From c562aef6667dcbb98ce82cb6e435f026243d8a18 Mon Sep 17 00:00:00 2001 From: Tom Schulze Date: Fri, 29 Apr 2022 15:12:47 +0200 Subject: [PATCH] Craft 4 compatibility, refactoring, add twig template rendering, sortable --- CHANGELOG.md | 18 ++- README.md | 4 +- composer.json | 12 +- resources/readonly.png | Bin 4900 -> 5245 bytes src/{Readonly.php => ReadonlyPlugin.php} | 11 +- .../{Readonly.php => ReadonlyField.php} | 4 +- src/fields/Readonly.php | 78 ----------- src/fields/ReadonlyField.php | 128 ++++++++++++++++++ src/icon-mask.svg | 10 ++ .../m220429_104217_change_class_name.php | 34 +++++ src/templates/input.twig | 2 +- src/templates/settings.twig | 10 ++ src/translations/de/readonly.php | 2 + 13 files changed, 217 insertions(+), 96 deletions(-) rename src/{Readonly.php => ReadonlyPlugin.php} (74%) rename src/feedme/{Readonly.php => ReadonlyField.php} (86%) delete mode 100644 src/fields/Readonly.php create mode 100644 src/fields/ReadonlyField.php create mode 100644 src/icon-mask.svg create mode 100644 src/migrations/m220429_104217_change_class_name.php create mode 100644 src/templates/settings.twig diff --git a/CHANGELOG.md b/CHANGELOG.md index 202cafa..25a9b20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,13 +1,27 @@ # Read-only Field Changelog -## Unreleased +## 2.0.0 - 2022-04-29 + +### Added + +- Craft CMS 4 compatibility +- php 8.1 compatibility +- Support for Craft CMS 4 condition builder +- Twig formatting of value in CP +- The field is now sortable + ### Changed -- Fixed background color of icon + +- Requires Craft CMS >= 4.0 and php >= 8.0 ## 1.0.1 - 2019-09-14 + ### Added +- - Support for Feed-Me plugin ## 1.0.0 - 2019-04-06 + ### Added +- - Initial release diff --git a/README.md b/README.md index b5464c0..bcfbe05 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Read-only Field for Craft CMS 3.x +# Read-only Field for Craft CMS ![Icon](resources/readonly.png) @@ -10,7 +10,7 @@ Sometimes you add content to Craft entries (for example via an API) that should ## Requirements - * Craft CMS >= 3.0.0 + * Craft CMS >= 4.0.0 ## Installation diff --git a/composer.json b/composer.json index 2436610..06b46cc 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "codemonauts/craft-readonly-field", "description": "Craft CMS plugin to add a simple, read-only plaintext field.", - "version": "1.0.1", + "version": "2.0.0", "type": "craft-plugin", "keywords": [ "craft", @@ -24,7 +24,8 @@ "issues": "https://github.com/codemonauts/craft-readonly-field/issues" }, "require": { - "craftcms/cms": "^3.0.0" + "craftcms/cms": "^4.0.0-alpha", + "php": "^8.0" }, "autoload": { "psr-4": { @@ -33,10 +34,7 @@ }, "extra": { "handle": "readonly", - "class": "codemonauts\\readonly\\Readonly", - "name": "Read-only Field", - "description": "Simple, read-only plaintext field.", - "hasCpSection": false, - "hasSettings": false + "class": "codemonauts\\readonly\\ReadonlyPlugin", + "name": "Read-only Field" } } diff --git a/resources/readonly.png b/resources/readonly.png index 2164cef04a0a6e386d14d30eb6c15bea1e26c66b..ac51d1d0ec73d73eacddd063673d7281bb04afc1 100644 GIT binary patch delta 5244 zcmV-?6oc!eCjBUoFn<6zWmrjOO-%qQ0000800D<-00aO40096102%-Q00002paK8{ z000010001hpaTE|000010001h00000Yeptb000y7Nkl?BY6fBpZfL25hlE_ga201DMwzk5l$hB(g=@Hc8h=SVVqkptLDu@c4YAaE+fD)(| zHeRY0QEQK&tw4fulZzmi1VVPt`%Ut1_A)y=GdtI1|L58LXa4yw-}k-m{P&shQ3krl z>46RbI$9heUoSs}dqKsX_~ql*Chpe`=j;yPcO>q&59gmFkBvmW4Pk1vVJgl17GW9X ze3_ul0=l=1D1TfMCK>{TilI{{i}c!sGCo5YZ-VKgm+1%ro~DORIZk!=!vIquK#o(&yP;gz;3(vH zq!oSdfuGSG2kn;0RswL(zJPoad=#DoMXj2wvD=1bm4`_t-%Jc2m)JRCu#ErCT18owyJ|p;=+9D zJ;kbd14{@{Nm4G>QkNm~3R|s}p@o42uV5|v+A$;@Gh0Q|39x`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zkGiUbdmt`_qnKppJ?>!Q`p82s-UIDiD5hj88>xeZ-46h^9;fGGTs6n6)ec3-fTKp zx2b6X&-`!TNV9UbeSLZ18R4(uqRW39J@3aqKHGm^Bmv^>bLW++qCYJ<%Rx;s(Epdi zs8d!CRbj(Z6gxyvO}IjUI9&3R5&x!>-xaHlfO|w&Bxa%@Tf;u}<%bx^3~cOGjNfPS zPjRo|?|-sSIqSc-D3Sn&I^c{WwSwNhn>O9!GvPz&T}=c)U;u2?@{ z;4AaV!r70p|73?x{baDnSJP$1k#n>-;|;1jULo8Jc)!W3aE0Z4)3)D7t-5KO&!MiM z5JiSb1>@enNpvT2sm1~T-zTeOKi1##sW2T?5!avvVv^!0zH=g#=CSjee3+sfFv|TASlOgQbQ7SS z&8}H>(}z^JEMG^om-;IK`GJf<GkPUAu6)Vpk*gA92MN z;D4WZl;p?GpEkbX`%4@O26}*jk1O+)mWgxr(zf-{A&EK z^fvnVCwz8?cZrvh|6tDz5deY4wQWh0znE#sY~o;v4%hn)LVghZi|2Y{<~BaOm{zg; zmTuQD0T46#`={j0qLwKMx)Tz4QJn=~V}IBhAD$VayAmiBIePMN4Eo7j?Hxr@;YIHG zS|B+ig(j`IlUlTE>K^v>q5YSs1VHo{{J{tvKb%;5k|J+NSpZ$}*^8!|zQ)pY0wD2q zRkgHk_QzB(KTk@R$UTh~=y7X58vo%m)dM_Qg13YKLIPM_X6ths=)0%ZxNFo-i+}Yj z_8S!)MMHjXxhEQ4#U_^04UYKPTRplugins->isPluginEnabled('feed-me')) { + if (Craft::$app->plugins->isPluginEnabled('feed-me')) { Event::on(FeedMeFields::class, FeedMeFields::EVENT_REGISTER_FEED_ME_FIELDS, function(RegisterFeedMeFieldsEvent $e) { $e->fields[] = ReadonlyFeedme::class; } diff --git a/src/feedme/Readonly.php b/src/feedme/ReadonlyField.php similarity index 86% rename from src/feedme/Readonly.php rename to src/feedme/ReadonlyField.php index 348389d..5771907 100644 --- a/src/feedme/Readonly.php +++ b/src/feedme/ReadonlyField.php @@ -5,10 +5,10 @@ use craft\feedme\base\Field; use craft\feedme\base\FieldInterface; -class Readonly extends Field implements FieldInterface +class ReadonlyField extends Field implements FieldInterface { public static $name = 'Read-only Field'; - public static $class = 'codemonauts\readonly\fields\Readonly'; + public static $class = 'codemonauts\readonly\fields\ReadonlyField'; public function getMappingTemplate(): string { diff --git a/src/fields/Readonly.php b/src/fields/Readonly.php deleted file mode 100644 index ea45b4a..0000000 --- a/src/fields/Readonly.php +++ /dev/null @@ -1,78 +0,0 @@ -columnType; - } - - /** - * @inheritdoc - */ - public function getInputHtml($value, ElementInterface $element = null): string - { - return Craft::$app->getView()->renderTemplate('readonly/input', - [ - 'name' => $this->handle, - 'value' => $value, - 'field' => $this, - ]); - } - - /** - * @inheritdoc - */ - public function serializeValue($value, ElementInterface $element = null) - { - if ($value !== null) { - $value = LitEmoji::unicodeToShortcode($value); - } - - return $value; - } - - /** - * @inheritdoc - */ - public function getSearchKeywords($value, ElementInterface $element): string - { - $value = (string)$value; - $value = LitEmoji::unicodeToShortcode($value); - - return $value; - } -} diff --git a/src/fields/ReadonlyField.php b/src/fields/ReadonlyField.php new file mode 100644 index 0000000..ca6fb19 --- /dev/null +++ b/src/fields/ReadonlyField.php @@ -0,0 +1,128 @@ +columnType; + } + + /** + * @inheritdoc + */ + public function getInputHtml($value, ElementInterface $element = null): string + { + return Craft::$app->getView()->renderTemplate('readonly/input', [ + 'name' => $this->handle, + 'value' => $value, + 'renderedValue' => $this->renderValue($value, $element), + 'field' => $this, + ]); + } + + /** + * @inheritdoc + */ + public function serializeValue($value, ElementInterface $element = null): mixed + { + if ($value !== null) { + $value = LitEmoji::unicodeToShortcode($value); + } + + return $value; + } + + /** + * @inheritdoc + */ + public function getSearchKeywords($value, ElementInterface $element): string + { + $value = (string)$value; + return LitEmoji::unicodeToShortcode($value); + } + + /** + * @inheritdoc + */ + public function getElementConditionRuleType(): ?string + { + return TextFieldConditionRule::class; + } + + /** + * @inheritdoc + */ + public function getSettingsHtml(): ?string + { + return Craft::$app->getView()->renderTemplate('readonly/settings', [ + 'field' => $this, + ]); + } + + /** + * @inheritdoc + */ + public function getTableAttributeHtml(mixed $value, ElementInterface $element): string + { + return Html::encode(StringHelper::stripHtml($this->renderValue((string)$value, $element))); + } + + /** + * @param string $value + * @param \craft\base\ElementInterface $element + * + * @return string + * @throws \Twig\Error\LoaderError + * @throws \Twig\Error\SyntaxError + */ + private function renderValue(?string $value, ElementInterface $element): string + { + if ($this->template !== null) { + try { + $value = Craft::$app->getView()->renderString($this->template, [ + 'value' => $value, + 'element' => $element, + ]); + } catch (Error) { + Craft::error('Error rendering template of read-only field with handle "' . $this->handle . '".', 'readonly'); + } + } + + return $value; + } +} diff --git a/src/icon-mask.svg b/src/icon-mask.svg new file mode 100644 index 0000000..ae43d72 --- /dev/null +++ b/src/icon-mask.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/migrations/m220429_104217_change_class_name.php b/src/migrations/m220429_104217_change_class_name.php new file mode 100644 index 0000000..e1d0adb --- /dev/null +++ b/src/migrations/m220429_104217_change_class_name.php @@ -0,0 +1,34 @@ +getDb()->createCommand() + ->update(Table::FIELDS, ['type' => 'codemonauts\readonly\fields\ReadonlyField'], ['type' => 'codemonauts\readonly\fields\Readonly']) + ->execute(); + + return true; + } + + /** + * @inheritdoc + */ + public function safeDown(): bool + { + echo "m220429_104217_change_class_name cannot be reverted.\n"; + return false; + } +} diff --git a/src/templates/input.twig b/src/templates/input.twig index b6968c0..b977676 100644 --- a/src/templates/input.twig +++ b/src/templates/input.twig @@ -1,2 +1,2 @@ -{{ value }} +{{ renderedValue }} diff --git a/src/templates/settings.twig b/src/templates/settings.twig new file mode 100644 index 0000000..9aed44e --- /dev/null +++ b/src/templates/settings.twig @@ -0,0 +1,10 @@ +{% import "_includes/forms" as forms %} + +{{ forms.textField({ + label: "Template String"|t('readonly'), + instructions: "Twig template string for displaying the value in CP. If not set, the value is printed. You can use {{ value }} and {{ element }}."|t('readonly'), + id: 'template', + name: 'template', + value: field.template, + errors: field.getErrors('template') +}) }} \ No newline at end of file diff --git a/src/translations/de/readonly.php b/src/translations/de/readonly.php index ec66f66..9a46a43 100644 --- a/src/translations/de/readonly.php +++ b/src/translations/de/readonly.php @@ -2,4 +2,6 @@ return [ 'Read-only Field' => 'Schreibgeschütztes Feld', + 'Template String' => 'Template String', + 'Twig template string for displaying the value in CP. If not set, the value is printed. You can use {{ value }} and {{ element }}.' => 'Twig Template für die Ausgabe im CP. Wenn nicht gesetzt wird der Wert ausgegeben. Es stehen {{ value }} und {{ element }} zur Verfügung.', ];