Skip to content

Commit

Permalink
Add field layout elements
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Aug 16, 2021
1 parent 886dd4d commit 5116495
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/SiteModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

use sgroup\sitemodule\base\Module;
use sgroup\sitemodule\base\PluginTrait;
use sgroup\sitemodule\fieldlayoutelements\Note;
use sgroup\sitemodule\fieldlayoutelements\Spacer;
use sgroup\sitemodule\twigextensions\Extension;
use sgroup\sitemodule\variables\Variable;

use Craft;
use craft\events\DefineFieldLayoutElementsEvent;
use craft\models\FieldLayout;
use craft\web\Application;
use craft\web\twig\variables\CraftVariable;

Expand All @@ -32,6 +36,7 @@ public function init()
$this->_checkOffline();
$this->_setPluginComponents();
$this->_registerTwigExtensions();
$this->_registerFieldLayoutElements();

// Prevent code from firing too early before Craft is bootstrapped
Craft::$app->on(Application::EVENT_INIT, function() {
Expand All @@ -54,4 +59,12 @@ private function _registerTwigExtensions()
{
Craft::$app->view->registerTwigExtension(new Extension);
}

private function _registerFieldLayoutElements()
{
Event::on(FieldLayout::class, FieldLayout::EVENT_DEFINE_UI_ELEMENTS, function(DefineFieldLayoutElementsEvent $event) {
$event->elements[] = Note::class;
$event->elements[] = Spacer::class;
});
}
}
49 changes: 49 additions & 0 deletions src/fieldlayoutelements/Note.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace sgroup\sitemodule\fieldlayoutelements;

use Craft;
use craft\base\ElementInterface;
use craft\fieldlayoutelements\BaseUiElement;
use craft\helpers\Cp;
use craft\helpers\Html;

class Note extends BaseUiElement
{
// Properties
// =========================================================================

public $note;


// Protected Methods
// =========================================================================

protected function selectorLabel(): string
{
return $this->note ?: Craft::t('app', 'Note');
}

protected function selectorIcon()
{
return '@appicons/hash.svg';
}


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

public function settingsHtml()
{
return Cp::textFieldHtml([
'label' => Craft::t('app', 'Note'),
'id' => 'note',
'name' => 'note',
'value' => $this->note,
]);
}

public function formHtml(ElementInterface $element = null, bool $static = false)
{
return Html::tag('p', Html::encode(Craft::t('site', $this->note)));
}
}
37 changes: 37 additions & 0 deletions src/fieldlayoutelements/Spacer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace sgroup\sitemodule\fieldlayoutelements;

use Craft;
use craft\base\ElementInterface;
use craft\fieldlayoutelements\BaseUiElement;
use craft\helpers\Cp;
use craft\helpers\Html;

class Spacer extends BaseUiElement
{
// Protected Methods
// =========================================================================

protected function selectorLabel(): string
{
return Craft::t('app', 'Spacer');
}


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

public function settingsHtml()
{
return Cp::textFieldHtml([
'label' => Craft::t('app', 'Spacer'),
'id' => 'spacer',
'name' => 'spacer',
]);
}

public function formHtml(ElementInterface $element = null, bool $static = false)
{
return '';
}
}

0 comments on commit 5116495

Please sign in to comment.