Skip to content

Commit 1dad310

Browse files
committed
initial commit
0 parents  commit 1dad310

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed

LICENSE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Codemonauts
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8+
persons to whom the Software is furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11+
Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Whitespace-only changes.

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "codemonauts/craft-readonly-field",
3+
"description": "Craft 3 read-only plaintext field",
4+
"version": "1.0.0",
5+
"type": "craft-plugin",
6+
"minimum-stability": "dev",
7+
"keywords": [
8+
"craft",
9+
"cms",
10+
"craftcms",
11+
"craft-plugin"
12+
],
13+
"support": {
14+
"docs": "https://github.com/codemonauts/craft-readonly-field/blob/master/README.md",
15+
"issues": "https://github.com/codemonauts/craft-readonly-field/issues"
16+
},
17+
"license": "MIT",
18+
"autoload": {
19+
"psr-4": {
20+
"codemonauts\\readonly\\": "src/"
21+
}
22+
},
23+
"authors": [
24+
{
25+
"name": "Codemonauts",
26+
"homepage": "https://www.codemonauts.com"
27+
}
28+
],
29+
"require": {
30+
"craftcms/cms": "^3.0.0"
31+
},
32+
"extra": {
33+
"handle": "readonly",
34+
"name": "Read-only Field",
35+
"developer": "Codemonauts",
36+
"developerUrl": "https://www.codemonauts.com",
37+
"hasCpSection": false,
38+
"hasSettings": false,
39+
"class": "codemonauts\\readonly\\Readonly"
40+
}
41+
}

src/Readonly.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace codemonauts\readonly;
3+
4+
use \craft\base\Plugin;
5+
use craft\events\RegisterComponentTypesEvent;
6+
use craft\services\Fields;
7+
use yii\base\Event;
8+
use codemonauts\readonly\fields\Readonly as ReadonlyField;
9+
10+
class Readonly extends Plugin
11+
{
12+
public function init()
13+
{
14+
parent::init();
15+
16+
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
17+
$event->types[] = ReadonlyField::class;
18+
});
19+
}
20+
}

src/fields/Readonly.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
namespace codemonauts\readonly\fields;
3+
4+
use Craft;
5+
use craft\base\ElementInterface;
6+
use craft\base\Field;
7+
use craft\base\PreviewableFieldInterface;
8+
use LitEmoji\LitEmoji;
9+
use yii\db\Schema;
10+
11+
class Readonly extends Field implements PreviewableFieldInterface
12+
{
13+
// Static
14+
// =========================================================================
15+
16+
/**
17+
* @inheritdoc
18+
*/
19+
public static function displayName(): string
20+
{
21+
return Craft::t('readonly', 'Read-only Field');
22+
}
23+
24+
/**
25+
* @var string The type of database column the field should have in the content table
26+
*/
27+
public $columnType = Schema::TYPE_STRING;
28+
29+
// Public Methods
30+
// =========================================================================
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function __construct(array $config = [])
36+
{
37+
parent::__construct($config);
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function getContentColumnType(): string
44+
{
45+
return $this->columnType;
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function getInputHtml($value, ElementInterface $element = null): string
52+
{
53+
return Craft::$app->getView()->renderTemplate('readonly/input',
54+
[
55+
'name' => $this->handle,
56+
'value' => $value,
57+
'field' => $this,
58+
]);
59+
}
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
public function serializeValue($value, ElementInterface $element = null)
65+
{
66+
if ($value !== null) {
67+
$value = LitEmoji::unicodeToShortcode($value);
68+
}
69+
return $value;
70+
}
71+
72+
/**
73+
* @inheritdoc
74+
*/
75+
public function getSearchKeywords($value, ElementInterface $element): string
76+
{
77+
$value = (string)$value;
78+
$value = LitEmoji::unicodeToShortcode($value);
79+
return $value;
80+
}
81+
}

src/templates/input.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<strong>{{ value }}</strong><input name="{{ name }}" type="hidden" value="{{ value }}" />

src/translations/de/readonly.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'Read-only Field' => 'Schreibgeschütztes Feld',
5+
];

0 commit comments

Comments
 (0)