Skip to content

Commit

Permalink
feat: add images and less field to home template
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Sep 24, 2023
1 parent 734178d commit 1010b44
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
63 changes: 63 additions & 0 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class RockFrontend extends WireData implements Module, ConfigurableModule
const field_favicon = self::prefix . "favicon";
const field_ogimage = self::prefix . "ogimage";
const field_footerlinks = self::prefix . "footerlinks";
const field_images = self::prefix . "images";
const field_less = self::prefix . "less";

/** @var WireData */
public $alfredCache;
Expand Down Expand Up @@ -191,11 +193,25 @@ public function init()
$this->addHookBefore('TemplateFile::render', $this, "autoPrepend");
$this->addHookAfter("InputfieldForm::processInput", $this, "createWebfontsFile");
$this->addHookBefore("Inputfield::render", $this, "addFooterlinksNote");
$this->addHookAfter("Pages::saved", $this, "createAndSaveLESS");

// health checks
$this->checkHealth();
}

public function createAndSaveLESS(HookEvent $event): void
{
/** @var Page $page */
$page = $event->arguments(0);
$field = $page->fields->get(self::field_less);
if (!$field) return;
$code = $page->getUnformatted(self::field_less);

$path = $page->filesManager->path() . "custom.less";
if (!$code) $this->wire->files->unlink($path);
else $this->wire->files->filePutContents($path, $code);
}

public function ready()
{
$this->liveReload();
Expand Down Expand Up @@ -1298,6 +1314,8 @@ public function manifest()

public function migrate()
{
$this->migrateLess();
$this->migrateImages();
$this->migrateFavicon();
$this->migrateOgImage();
$this->migrateFooterlinks();
Expand Down Expand Up @@ -1349,6 +1367,32 @@ private function migrateFooterlinks()
$rm->addFieldToTemplate(self::field_footerlinks, 'home');
}

private function migrateImages()
{
if (!in_array("images", $this->migrations)) return;
$rm = $this->rm();
$rm->migrate([
'fields' => [
self::field_images => [
'type' => 'image',
'label' => 'Media',
'maxFiles' => 1,
'descriptionRows' => 0,
'columnWidth' => 50,
'extensions' => 'png jpg jpeg svg',
'okExtensions' => ['svg'],
'maxSize' => 3, // max 3 megapixels
'icon' => 'picture-o',
'outputFormat' => FieldtypeFile::outputFormatArray,
'description' => 'Images that can be included somewhere (eg in Hanna Codes).',
'tags' => self::tags,
'notes' => 'API: $home->images()->get("name=foo.jpg");',
],
],
]);
$rm->addFieldToTemplate(self::field_images, 'home');
}

private function mgirateLatteTranslations()
{
if (!in_array("lattetranslations", $this->migrations)) return;
Expand All @@ -1359,6 +1403,23 @@ private function mgirateLatteTranslations()
$rm->setModuleConfig("ProcessLanguageTranslator", ['extensions' => "$ext latte"]);
}

private function migrateLess()
{
if (!in_array("less", $this->migrations)) return;
$rm = $this->rm();
$rm->migrate([
'fields' => [
self::field_less => [
'type' => 'textarea',
'label' => 'LESS',
'rows' => 5,
'icon' => 'css3',
],
],
]);
$rm->addFieldToTemplate(self::field_less, 'home');
}

private function migrateOgImage()
{
if (!in_array("ogimage", $this->migrations)) return;
Expand Down Expand Up @@ -2239,9 +2300,11 @@ private function configSettings($inputfields)
$f->label = "Migrations";
$f->addOption('favicon', 'favicon - Create an image field for a favicon and add it to the home template');
$f->addOption('ogimage', 'ogimage - Create an image field for an og:image and add it to the home template');
$f->addOption('images', 'images - Create an image field for general image uploads and add it to the home template');
$f->addOption('footerlinks', 'footerlinks - Create a page field for selecting pages for the footer menu and add it to the home template');
$url = $this->wire->pages->get(2)->url . "module/edit?name=ProcessLanguageTranslator";
$f->addOption('lattetranslations', "lattetranslations - Add latte extension to translatable files in [ProcessLanguageTranslator]($url)");
$f->addOption('less', "less - Add textarea to inject custom LESS/CSS");
$f->value = (array)$this->migrations;
$f->notes = "Note that removing a checkbox does not undo an already executed migration!";
$fs->add($f);
Expand Down
12 changes: 10 additions & 2 deletions classes/AssetsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ protected function addInfo($opt)
*
* @return self
*/
public function add($file, $suffix = '', $properties = [])
{
public function add(
$file,
$suffix = '',
$properties = [],
$quiet = false,
) {
$debug = $this->getDebugNote($file);
if (is_string($file)) $file = new Asset($file, $suffix);

// early quiet exit if file does not exist and quiet parameter is set
if (!is_file($file->path) and $quiet) return $this;

foreach ($properties as $k => $v) $file->$k = $v;
// prevent adding file multiple times
if ($exists = $this->get('path=' . $file->path)) {
Expand Down

0 comments on commit 1010b44

Please sign in to comment.