Skip to content

Commit

Permalink
Merge pull request #2 from DejaBallard/feat-shortcode
Browse files Browse the repository at this point in the history
adding shortcode to allow watermarking of images within wysiwyg
  • Loading branch information
gurucomkz authored Apr 23, 2021
2 parents 2883ff1 + 1b7cc19 commit 834135b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
use SilverStripe\View\Parsers\ShortcodeParser;
use Gurucomkz\Watermark\Shortcode;
ShortcodeParser::get('default')->register('watermark', [Shortcode::class, 'Watermark']);

3 changes: 3 additions & 0 deletions _config/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ SilverStripe\Assets\Storage\DBFile:
SilverStripe\Assets\Image:
extensions:
- Gurucomkz\Watermark\ImageExtension
SilverStripe\AssetAdmin\Forms\ImageFormFactory:
extensions:
- Gurucomkz\Watermark\ImageFormFactoryExtension

SilverStripe\SiteConfig\SiteConfig:
extensions:
Expand Down
43 changes: 43 additions & 0 deletions src/Gurucomkz/Watermark/ImageFormFactoryExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Gurucomkz\Watermark;

use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HTMLReadonlyField;
/**
* FocusPoint Asset Form Factory extension.
* Extends the CMS detail form to allow focus point selection.
*
* @extends Extension
*/
class ImageFormFactoryExtension extends Extension
{

/**
* Add FocusPoint field for selecting focus.
*/
public function updateFormFields(FieldList $fields, $controller, $formName, $context)
{
$image = isset($context['Record']) ? $context['Record'] : null;
if ($image && $image->appCategory() === 'image') {
$wmField = HTMLReadonlyField::create(
'Watermark Shortcode',
)->setValue($this->WatermarkShortCode($image->ID));

$titleField = $fields->fieldByName('Editor.Details.Title');
if ($titleField) {
if ($titleField->isReadonly()) $wmField = $wmField->performReadonlyTransformation();
$fields->insertAfter(
'Title',
$wmField
);
}

}
}

public function WatermarkShortCode($ID){
return "[watermark id=" . $ID . "]";
}
}
28 changes: 28 additions & 0 deletions src/Gurucomkz/Watermark/Shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Gurucomkz\Watermark;

use PageController;
use SilverStripe\Control\Controller;
use SilverStripe\Assets\Image;

class Shortcode
{

public static function Watermark($arguments, $content = null)
{

if (!isset($arguments['file'])) {
return '';
}

$image = Image::get()->filter("Name", $arguments['file'])->first();
if (!isset($image)) {
return '';
}
if (isset($arguments['position'])) {
return $image->Watermark($arguments['position']);
} else {
return $image->Watermark();
}
}
}

0 comments on commit 834135b

Please sign in to comment.