1 <?php
2
3 4 5 6 7 8 9
10 class NewsHomepageWidget extends HomepageWidget {
11 static $db = array(
12 'NewsCount' => 'Int',
13 'ShowOnlyImportant' => 'Boolean',
14 );
15
16 static $defaults = array(
17 'NewsCount' => 5,
18 );
19
20 static $has_one = array(
21 'Holder' => 'NewsHolder'
22 );
23
24 public $needObjects = array(
25 'NewsHolder' => 'Page'
26 );
27
28 function getCMSFields() {
29 if (!$this->canCreate()) {
30 return $this->getFailCMSFields();
31 }
32 $fields = parent::getCMSFields();
33 $holders = array();
34 if ($holders = DataObject::get('NewsHolder')) {
35 $holders = $holders->map();
36 }
37 $fields->replaceField('HolderID', new DropdownField('HolderID', $this->fieldLabel('Holder'), $holders, "", null, _t('Widgets.All', 'All')));
38
39 return $fields;
40 }
41
42 function NewsList() {
43 $newsCount = $this->NewsCount;
44 if (!$newsCount) {
45 $newsCount = 5;
46 }
47 if ($this->HolderID && $this->Holder()) {
48 return $this->Holder()->Entries(0, $newsCount, $this->ShowOnlyImportant);
49 }
50 $where = "Status LIKE '%Published%'";
51 if ($this->ShowOnlyImportant) {
52 $where .= " AND Important = 1";
53 }
54 return DataObject::get('NewsEntry', $where, "Date DESC", '', $newsCount);
55 }
56
57 function hasContent() {
58 if ($this->NewsList()) {
59 return true;
60 }
61 return false;
62 }
63
64 function getMoreLink() {
65 if ($this->HolderID && $this->Holder()) {
66 return $this->Holder()->Link();
67 }
68 if ($holder = DataObject::get_one('NewsHolder')) {
69 return $holder->Link();
70 }
71 return false;
72 }
73 }
74
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-