-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Craft 4 compatibility, refactoring, add twig template rendering, sort…
…able
- Loading branch information
1 parent
3eeb12b
commit c562aef
Showing
13 changed files
with
217 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace codemonauts\readonly\fields; | ||
|
||
use Craft; | ||
use craft\base\ElementInterface; | ||
use craft\base\Field; | ||
use craft\base\PreviewableFieldInterface; | ||
use craft\base\SortableFieldInterface; | ||
use craft\fields\conditions\TextFieldConditionRule; | ||
use craft\helpers\Html; | ||
use craft\helpers\StringHelper; | ||
use LitEmoji\LitEmoji; | ||
use Twig\Error\Error; | ||
use yii\db\Schema; | ||
|
||
class ReadonlyField extends Field implements PreviewableFieldInterface, SortableFieldInterface | ||
{ | ||
/** | ||
* @var string The type of database column the field should have in the content table | ||
*/ | ||
public string $columnType = Schema::TYPE_STRING; | ||
|
||
/** | ||
* @var string|null The template to render the value | ||
*/ | ||
public ?string $template = null; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function displayName(): string | ||
{ | ||
return Craft::t('readonly', 'Read-only Field'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getContentColumnType(): string | ||
{ | ||
return $this->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; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace codemonauts\readonly\migrations; | ||
|
||
use Craft; | ||
use craft\db\Migration; | ||
use craft\db\Table; | ||
|
||
/** | ||
* m220429_104217_change_class_name migration. | ||
*/ | ||
class m220429_104217_change_class_name extends Migration | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeUp(): bool | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<strong>{{ value }}</strong> | ||
<strong>{{ renderedValue }}</strong> | ||
<input name="{{ name }}" type="hidden" value="{{ value }}" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters