Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed Mar 13, 2019
0 parents commit 1dad310
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 0 deletions.
16 changes: 16 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The MIT License (MIT)

Copyright (c) 2018 Codemonauts

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Empty file added README.md
Empty file.
41 changes: 41 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "codemonauts/craft-readonly-field",
"description": "Craft 3 read-only plaintext field",
"version": "1.0.0",
"type": "craft-plugin",
"minimum-stability": "dev",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin"
],
"support": {
"docs": "https://github.com/codemonauts/craft-readonly-field/blob/master/README.md",
"issues": "https://github.com/codemonauts/craft-readonly-field/issues"
},
"license": "MIT",
"autoload": {
"psr-4": {
"codemonauts\\readonly\\": "src/"
}
},
"authors": [
{
"name": "Codemonauts",
"homepage": "https://www.codemonauts.com"
}
],
"require": {
"craftcms/cms": "^3.0.0"
},
"extra": {
"handle": "readonly",
"name": "Read-only Field",
"developer": "Codemonauts",
"developerUrl": "https://www.codemonauts.com",
"hasCpSection": false,
"hasSettings": false,
"class": "codemonauts\\readonly\\Readonly"
}
}
20 changes: 20 additions & 0 deletions src/Readonly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace codemonauts\readonly;

use \craft\base\Plugin;
use craft\events\RegisterComponentTypesEvent;
use craft\services\Fields;
use yii\base\Event;
use codemonauts\readonly\fields\Readonly as ReadonlyField;

class Readonly extends Plugin
{
public function init()
{
parent::init();

Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
$event->types[] = ReadonlyField::class;
});
}
}
81 changes: 81 additions & 0 deletions src/fields/Readonly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
namespace codemonauts\readonly\fields;

use Craft;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\PreviewableFieldInterface;
use LitEmoji\LitEmoji;
use yii\db\Schema;

class Readonly extends Field implements PreviewableFieldInterface
{
// Static
// =========================================================================

/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('readonly', 'Read-only Field');
}

/**
* @var string The type of database column the field should have in the content table
*/
public $columnType = Schema::TYPE_STRING;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function __construct(array $config = [])
{
parent::__construct($config);
}

/**
* @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,
'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;
}
}
1 change: 1 addition & 0 deletions src/templates/input.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>{{ value }}</strong><input name="{{ name }}" type="hidden" value="{{ value }}" />
5 changes: 5 additions & 0 deletions src/translations/de/readonly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'Read-only Field' => 'Schreibgeschütztes Feld',
];

0 comments on commit 1dad310

Please sign in to comment.