Skip to content

Commit

Permalink
SilverStripe 4 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cocker authored and James Cocker committed Apr 13, 2018
1 parent 3a48391 commit 8983724
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 83 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ This module has been designed to have just the minimum required features, to avo
* James Cocker ([email protected])

## Requirements
* Silverstripe 3.1+
* Silverstripe 4.1+
* Use the 3.1 branch for SilverStripe 3.1 support

## Installation Instructions

Expand All @@ -25,9 +26,9 @@ This module has been designed to have just the minimum required features, to avo

* A News Article has a Headline, Date, Article & Image (optional)
* *Add New News Article* button on Holder CMS page.
* Articles listed on holder page, with summary, date, image
* Articles listed on holder page, with date and title
* News Articles are automatically moved to a News Holder if created elsewhere in the sitetree accidentally.
* Lightbox compatability
* Lightbox compatibility
* RSS feed of articles
* Articles are paginated on the News Holder

Expand Down
8 changes: 4 additions & 4 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NewsHolder:
PurpleSpider\BasicNews\NewsHolder:
extensions:
- 'CustomLumberjack'
NewsArticle:
show_in_sitetree: false
- PurpleSpider\BasicNews\CustomLumberjack
PurpleSpider\BasicNews\NewsArticle:
show_in_sitetree: false
File renamed without changes.
File renamed without changes
File renamed without changes
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "purplespider/basic-news",
"description": "Basic News Holder & News Article functionality",
"type": "silverstripe-module",
"type": "silverstripe-vendormodule",
"homepage": "https://github.com/purplespider/silverstripe-basic-news",
"keywords": ["silverstripe", "news", "article"],
"license": "BSD-3-Clause",
Expand All @@ -13,9 +13,14 @@
"issues": "https://github.com/purplespider/silverstripe-basic-news/issues"
},
"require": {
"silverstripe/framework": "~3.1",
"silverstripe/cms": "~3.1",
"silverstripe/lumberjack": "1.3.1"
"silverstripe/framework": "~4.1",
"silverstripe/cms": "~4.1",
"silverstripe/lumberjack": "2.*"
},
"replace": {"silverstripe/basic-news": "*"}
"replace": {"silverstripe/basic-news": "*"},
"extra": {
"expose": [
"client/dist"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php

namespace PurpleSpider\BasicNews;

use SilverStripe\Lumberjack\Model\Lumberjack;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\Tab;
use SilverStripe\Dev\Debug;
use SilverStripe\CMS\Model\SiteTree;

class CustomLumberjack extends Lumberjack {

/**
Expand All @@ -19,8 +29,18 @@ public function updateCMSFields(FieldList $fields) {
);

$tab = new Tab('ChildPages', $this->getLumberjackTitle(), $gridField);

// BEGIN CUSTOMISATION

// $fields->insertAfter($tab, 'Main');
$fields->insertBefore($tab, 'Main'); // Only modified line

if (SiteTree::get()->filter('ParentID',$this->owner->ID)->count()){
$fields->insertBefore($tab, 'Main');
} else {
$fields->insertAfter($tab, 'Main');
}
// END CUSTOMISATION

}
}

Expand Down
77 changes: 44 additions & 33 deletions code/NewsArticle.php → src/Model/NewsArticle.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
<?php

namespace PurpleSpider\BasicNews;

use Page;





use SilverStripe\Assets\Image;
use SilverStripe\ORM\FieldType\DBDate;
use SilverStripe\Forms\DateField;
use SilverStripe\AssetAdmin\Forms\UploadField;
use PurpleSpider\BasicNews\NewsHolder;
use SilverStripe\Control\Director;
use SilverStripe\View\Requirements;
use PageController;



class NewsArticle extends Page
{

private static $db = array(
'Date' => 'Date'
'Date' => DBDate::class
);

private static $has_one = array(
'AttachedImage' => 'Image'
'AttachedImage' => Image::class
);

public static $defaults = array(
private static $defaults = array(
'ShowInMenus' => false
);

public static $summary_fields = array(
"Date" => "Date",
private static $summary_fields = array(
"Date.Nice" => "Date",
"Title" => "Title"
);

public static $default_sort = "Date DESC, Created DESC";
public static $default_parent = "news";
public static $description = 'An individual news item displayed on a News holder page';
public static $singular_name = 'News Article';
public static $plural_name = 'News Articles';
public static $icon = "basic-news/images/newspaper";
public static $allowed_children = array();
public static $can_be_root = false;
private static $default_sort = "Date DESC, Created DESC";
private static $default_parent = "news";
private static $description = 'An individual news item displayed on a News holder page';
private static $singular_name = 'News Article';
private static $plural_name = 'News Articles';
private static $icon = "purplespider/basic-news:client/dist/images/newspaper-file.gif";
private static $allowed_children = array();
private static $can_be_root = false;
private static $table_name = "NewsArticle";

public function populateDefaults()
{
Expand All @@ -39,19 +59,19 @@ public function getCMSFields()
{
$this->beforeUpdateCMSFields(function ($fields) {

$datefield = new DateField('Date', 'Date (DD/MM/YYYY)');
$datefield->setConfig('showcalendar', true);
$datefield->setConfig('showdropdown', true);
$datefield->setConfig('dateformat', 'dd/MM/yyyy');
$datefield = new DateField('Date', 'Article Date');
// $datefield->setConfig('showcalendar', true);
// $datefield->setConfig('showdropdown', true);
// $datefield->setConfig('dateformat', 'dd/MM/yyyy');

$fields->addFieldToTab('Root.Main', $datefield, 'Content');

$image = new UploadField('AttachedImage', 'Main Image');
$image = new UploadField('AttachedImage', 'Featured Image');
$image->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
$image->setConfig('allowedMaxFileNumber', 1);
// $image->setConfig('allowedMaxFileNumber', 1);
$image->setFolderName('Managed/NewsImages');
$image->setCanPreviewFolder(false);
$image->setRightTitle("Displayed to the right of the content in the main article, where it can be clicked to enlarge. <br />A thumbnail also appears next to the article summary on the main News page.");
// $image->setCanPreviewFolder(false);
$image->setDescription("Displayed to the right of the content in the main article, where it can be clicked to enlarge. <br />A thumbnail also appears next to the article summary on the main News page.");
$fields->addFieldToTab('Root.Main', $image, "Content");

});
Expand All @@ -71,7 +91,7 @@ public function onBeforeWrite()
$this->MenuTitle = $this->Date.": ".$this->Title;

// Move to News holder if created under something else
if ($this->Parent()->ClassName != "NewsHolder") {
if ($this->Parent()->ClassName != "PurpleSpider\BasicNews\NewsHolder") {
$this->ParentID = NewsHolder::get()->first()->ID;
}

Expand All @@ -85,18 +105,9 @@ public function onBeforeWrite()
}


class NewsArticle_Controller extends Page_Controller
class NewsArticle_Controller extends PageController
{

public function init()
{
if (Director::fileExists(project() . "/css/news.css")) {
Requirements::css(project() . "/css/news.css");
} else {
Requirements::css("basic-news/css/news.css");
}
parent::init();
}


// Provides a resized image with the max width provided
public function ArticleImageSized($maxwidth = 250)
Expand Down
37 changes: 17 additions & 20 deletions code/NewsHolder.php → src/Model/NewsHolder.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
<?php

namespace PurpleSpider\BasicNews;

use Page;
use PurpleSpider\BasicNews\NewsArticle;
use SilverStripe\Control\RSS\RSSFeed;
use SilverStripe\Control\Director;
use SilverStripe\View\Requirements;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\ORM\PaginatedList;
use PageController;
use SilverStripe\Forms\Tab;

class NewsHolder extends Page
{

public function getCMSFields()
{
$intro = null;

$this->beforeUpdateCMSFields(function ($fields) {
$fields->renameField("Content", "Intro Content");
});

$fields = parent::getCMSFields();

if ($intro) {
$intro->setRightTitle("Appears at the top of the main ".$this->Title." page, above the list of articles.");
}

$fields->dataFieldByName("Content")->setDescription("This content appears at the top of the main ".$this->Title." page, above the list of news articles.");

return $fields;
}
Expand All @@ -34,9 +43,9 @@ public function getLumberjackPagesForGridfield($excluded = array())
}

// Only allows certain children to be created
private static $allowed_children = array('NewsArticle');
private static $allowed_children = array(NewsArticle::class);
private static $description = 'Holds News Article pages';
private static $icon = "basic-news/images/newspaper-page";
private static $icon = "purplespider/basic-news:client/dist/images/newspaper-page-file.gif";

public function stageChildren($showAll = false)
{
Expand All @@ -51,24 +60,12 @@ public function init()
}


class NewsHolder_Controller extends Page_Controller
class NewsHolder_Controller extends PageController
{

private static $allowed_actions = array(
"rss"
);

public function init()
{
RSSFeed::linkToFeed($this->Link() . "rss");
if (Director::fileExists(project() . "/css/news.css")) {
Requirements::css(project() . "/css/news.css");
} else {
Requirements::css("basic-news/css/news.css");
}

parent::init();
}

public function rss()
{
Expand Down
16 changes: 0 additions & 16 deletions templates/Layout/NewsArticle.ss

This file was deleted.

18 changes: 18 additions & 0 deletions templates/PurpleSpider/BasicNews/Layout/NewsArticle.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% require css("purplespider/basic-news: client/dist/css/basic-news.css") %>

<div class="NewsArticle">

<h1>$Title</h1>
<p class="date">$Date.Long</p>

<% if AttachedImage %>
<% if AttachedImage.Width >= '250' %><a href="$AttachedImage.FitMax(1200,1200).URL" class="lightbox"><% end_if %>
<img class="right" src="$AttachedImage.ScaleMaxWidth(250).URL" />
<% if AttachedImage.Width >= '250' %></a><% end_if %>
<% end_if %>

$Content

<p class="back"><a href="$Parent.Link">Back to News</a></p>

</div>
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<% require css("purplespider/basic-news: client/dist/css/basic-news.css") %>


<h1>$Title</h1>

$Content
Expand All @@ -10,7 +13,7 @@ $Content
<div class="hr"></div>
<% if AttachedImage %>
<a href="$Link">
<% loop AttachedImage.setWidth(150) %><img class="right" src="$URL" width="$Width" height="$Height" alt="$Title" /><% end_loop %>
<% loop AttachedImage.ScaleMaxWidth(150) %><img class="right" src="$URL" width="$Width" height="$Height" alt="$Title" /><% end_loop %>
</a>
<% end_if %>
<h2><a href="$Link">$Title</a></h2>
Expand Down

0 comments on commit 8983724

Please sign in to comment.