Skip to content

Commit

Permalink
Added Shortcode method (#35) (#36)
Browse files Browse the repository at this point in the history
* Added Shortcode method
- Added shortcode method to allow for shortcodes in editors to show the newsletter form

Co-authored-by: SnowB1 <[email protected]>

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: SnowB1 <[email protected]>
  • Loading branch information
3 people authored Dec 21, 2022
1 parent beb1d04 commit f45b125
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Page;
use PageController;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\View\Parsers\ShortcodeParser;

SiteConfig::add_extension(NewsletterSettings::class);
Page::add_extension(NewsletterPageExtender::class);
PageController::add_extension(NewsletterPageControllerExtender::class);

ShortcodeParser::get('default')->register('newsletterform', [NewsletterShortcodeProvider::class, 'handle_shortcode']);
4 changes: 4 additions & 0 deletions src/NewsletterPageExtender.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class NewsletterPageExtender extends DataExtension
"NewsletterShowHide" => true,
];

private static $casting = [
'NewsletterShortcode' => 'HTMLText'
];

public function populateDefaults()
{
$this->owner->NewsletterShowHide = true;
Expand Down
34 changes: 34 additions & 0 deletions src/NewsletterShortcodeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Werkbot\Newsletter;

use SilverStripe\Control\Controller;
use SilverStripe\View\Parsers\ShortcodeParser;

class NewsletterShortcodeProvider extends ShortcodeParser{
/*
[newsletter,class="form newsletter-form"]
*/
public static function handle_shortcode($args, $content, $parser, $shortcode, $extra = array()){
$PopupContent = '';
// Add Content before form
if ($content){
$PopupContent = $parser->parse($content);
}
// If class add wrapper div
if (isset($arguments['class'])){
$PopupContent .= '<div class="'.$arguments['class'].'">';
}
// Get Form HTML
$PopupContent .= Controller::curr()->NewsletterForm()
->setHTMLID("NewsletterForm".rand(0,100))
->setFormAction(Controller::join_links(Controller::curr()->owner->Link(), 'NewsletterForm'))
->forTemplate();
// End wrapper div
if (isset($arguments['class'])){
$PopupContent .= '</div>';
}
// Return full html
return $PopupContent;
}
}

0 comments on commit f45b125

Please sign in to comment.