Skip to content

Commit

Permalink
Craft 4 compatibility, refactoring, add twig template rendering, sort…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
kringkaste committed Apr 29, 2022
1 parent 3eeb12b commit c562aef
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 96 deletions.
18 changes: 16 additions & 2 deletions CHANGELOG.md
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Read-only Field for Craft CMS 3.x
# Read-only Field for Craft CMS

![Icon](resources/readonly.png)

Expand All @@ -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

Expand Down
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": {
Expand All @@ -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"
}
}
Binary file modified resources/readonly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions src/Readonly.php → src/ReadonlyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace codemonauts\readonly;

use Craft;
use craft\base\Plugin;
use craft\events\RegisterComponentTypesEvent;
use craft\services\Fields;
use yii\base\Event;
use codemonauts\readonly\fields\Readonly as ReadonlyField;
use codemonauts\readonly\feedme\Readonly as ReadonlyFeedme;
use codemonauts\readonly\fields\ReadonlyField;
use codemonauts\readonly\feedme\ReadonlyField as ReadonlyFeedme;
use craft\feedme\events\RegisterFeedMeFieldsEvent;
use craft\feedme\services\Fields as FeedMeFields;

class Readonly extends Plugin
class ReadonlyPlugin extends Plugin
{
public string $schemaVersion = '1.0.1';

public function init()
{
parent::init();
Expand All @@ -22,7 +25,7 @@ public function init()
});

// Register field for feed-me plugin if installed
if (\Craft::$app->plugins->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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/feedme/Readonly.php → src/feedme/ReadonlyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
78 changes: 0 additions & 78 deletions src/fields/Readonly.php

This file was deleted.

128 changes: 128 additions & 0 deletions src/fields/ReadonlyField.php
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;
}
}
10 changes: 10 additions & 0 deletions src/icon-mask.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions src/migrations/m220429_104217_change_class_name.php
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;
}
}
2 changes: 1 addition & 1 deletion src/templates/input.twig
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 }}" />
10 changes: 10 additions & 0 deletions src/templates/settings.twig
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')
}) }}
2 changes: 2 additions & 0 deletions src/translations/de/readonly.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];

0 comments on commit c562aef

Please sign in to comment.