Skip to content

Commit d1a7891

Browse files
committed
Add recent posts widget to CMS + Add related posts to product page
1 parent ee2a6d1 commit d1a7891

File tree

20 files changed

+576
-110
lines changed

20 files changed

+576
-110
lines changed

Block/Adminhtml/Category/Edit/Tab/Main.php

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,28 @@ class Main extends \Magento\Backend\Block\Widget\Form\Generic implements
2020
protected $_systemStore;
2121

2222
/**
23-
* @var \Magefan\Blog\Model\ResourceModel\Category\Collection
23+
* @var \Magefan\Blog\Model\Config\Source\Category
2424
*/
25-
protected $_categoryCollection;
25+
protected $_categoryOption;
2626

2727
/**
2828
* @param \Magento\Backend\Block\Template\Context $context
2929
* @param \Magento\Framework\Registry $registry
3030
* @param \Magento\Framework\Data\FormFactory $formFactory
3131
* @param \Magento\Store\Model\System\Store $systemStore
32+
* @param \Magefan\Blog\Model\Config\Source\Category $categoryOption
3233
* @param array $data
3334
*/
3435
public function __construct(
3536
\Magento\Backend\Block\Template\Context $context,
3637
\Magento\Framework\Registry $registry,
3738
\Magento\Framework\Data\FormFactory $formFactory,
3839
\Magento\Store\Model\System\Store $systemStore,
39-
\Magefan\Blog\Model\ResourceModel\Category\Collection $categoryCollection,
40+
\Magefan\Blog\Model\Config\Source\Category $categoryOption,
4041
array $data = []
4142
) {
4243
$this->_systemStore = $systemStore;
43-
$this->_categoryCollection = $categoryCollection;
44+
$this->_categoryOption = $categoryOption;
4445
parent::__construct($context, $registry, $formFactory, $data);
4546
}
4647

@@ -140,35 +141,18 @@ protected function _prepareForm()
140141
$model->setStoreIds([$this->_storeManager->getStore(true)->getId()]);
141142
}
142143

143-
$categories[] = ['label' => __('Please select'), 'value' => 0];
144-
$collection = $this->_categoryCollection
145-
->addFieldToFilter('category_id', array('neq' => $model->getId()))
146-
->setOrder('position')
147-
->getTreeOrderedArray();
148-
149-
foreach($collection as $item) {
150-
if (!$model->isChild($item)) {
151-
$categories[] = array(
152-
'label' => $this->_getSpaces($item->getLevel()).' '.$item->getTitle() . ($item->getIsActive() ? '' : ' ('.__('Disabled').')' ),
153-
'value' => ($item->getPath() ? $item->getPath().'/' : '') . $item->getId() ,
154-
);
155-
}
156-
}
157-
158-
if (count($categories)) {
159-
$field = $fieldset->addField(
160-
'path',
161-
'select',
162-
[
163-
'name' => 'path',
164-
'label' => __('Parent Category'),
165-
'title' => __('Parent Category'),
166-
'values' => $categories,
167-
'disabled' => $isElementDisabled,
168-
'style' => 'width:100%',
169-
]
170-
);
171-
}
144+
$field = $fieldset->addField(
145+
'path',
146+
'select',
147+
[
148+
'name' => 'path',
149+
'label' => __('Parent Category'),
150+
'title' => __('Parent Category'),
151+
'values' => $this->_categoryOption->toOptionArray(),
152+
'disabled' => $isElementDisabled,
153+
'style' => 'width:100%',
154+
]
155+
);
172156

173157
$fieldset->addField(
174158
'position',
@@ -189,21 +173,6 @@ protected function _prepareForm()
189173
return parent::_prepareForm();
190174
}
191175

192-
/**
193-
* Generate spaces
194-
* @param int $n
195-
* @return string
196-
*/
197-
protected function _getSpaces($n)
198-
{
199-
$s = '';
200-
for($i = 0; $i < $n; $i++) {
201-
$s .= '--- ';
202-
}
203-
204-
return $s;
205-
}
206-
207176
/**
208177
* Prepare label for tab
209178
*

Block/Adminhtml/Post/Edit/Tab/Main.php

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,28 @@ class Main extends \Magento\Backend\Block\Widget\Form\Generic implements \Magent
1919
protected $_systemStore;
2020

2121
/**
22-
* @var \Magefan\Blog\Model\ResourceModel\Category\Collection
22+
* @var \Magefan\Blog\Model\Config\Source\Category
2323
*/
24-
protected $_categoryCollection;
24+
protected $_categoryOption;
2525

2626
/**
2727
* @param \Magento\Backend\Block\Template\Context $context
2828
* @param \Magento\Framework\Registry $registry
2929
* @param \Magento\Framework\Data\FormFactory $formFactory
3030
* @param \Magento\Store\Model\System\Store $systemStore
31+
* @param \Magefan\Blog\Model\Config\Source\Category $categoryOption
3132
* @param array $data
3233
*/
3334
public function __construct(
3435
\Magento\Backend\Block\Template\Context $context,
3536
\Magento\Framework\Registry $registry,
3637
\Magento\Framework\Data\FormFactory $formFactory,
3738
\Magento\Store\Model\System\Store $systemStore,
38-
\Magefan\Blog\Model\ResourceModel\Category\Collection $categoryCollection,
39+
\Magefan\Blog\Model\Config\Source\Category $categoryOption,
3940
array $data = []
4041
) {
4142
$this->_systemStore = $systemStore;
42-
$this->_categoryCollection = $categoryCollection;
43+
$this->_categoryOption = $categoryOption;
4344
parent::__construct($context, $registry, $formFactory, $data);
4445
}
4546

@@ -140,26 +141,14 @@ protected function _prepareForm()
140141
$model->setStoreIds([$this->_storeManager->getStore(true)->getId()]);
141142
}
142143

143-
$categories[] = ['label' => __('Please select'), 'value' => 0];
144-
$collection = $this->_categoryCollection
145-
->setOrder('position')
146-
->getTreeOrderedArray();
147-
148-
foreach($collection as $item) {
149-
$categories[] = array(
150-
'label' => $this->_getSpaces($item->getLevel()).' '.$item->getTitle() . ($item->getIsActive() ? '' : ' ('.__('Disabled').')' ),
151-
'value' => $item->getId() ,
152-
);
153-
}
154-
155144
$field = $fieldset->addField(
156145
'categories',
157146
'multiselect',
158147
[
159148
'name' => 'post[categories][]',
160149
'label' => __('Categories'),
161150
'title' => __('Categories'),
162-
'values' => $categories,
151+
'values' => $this->_categoryOption->toOptionArray(),
163152
'disabled' => $isElementDisabled,
164153
'style' => 'width:100%',
165154
]
@@ -204,21 +193,6 @@ protected function _prepareForm()
204193
return parent::_prepareForm();
205194
}
206195

207-
/**
208-
* Generate spaces
209-
* @param int $n
210-
* @return string
211-
*/
212-
protected function _getSpaces($n)
213-
{
214-
$s = '';
215-
for($i = 0; $i < $n; $i++) {
216-
$s .= '--- ';
217-
}
218-
219-
return $s;
220-
}
221-
222196
/**
223197
* Prepare label for tab
224198
*
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Block\Catalog\Product;
10+
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Framework\View\Element\AbstractBlock;
13+
14+
/**
15+
* Blog post related posts block
16+
*/
17+
class RelatedPosts extends \Magefan\Blog\Block\Post\PostList\AbstractList
18+
{
19+
20+
/**
21+
* Prepare posts collection
22+
*
23+
* @return void
24+
*/
25+
protected function _preparePostCollection()
26+
{
27+
$pageSize = (int) $this->_scopeConfig->getValue(
28+
'mfblog/product_page/number_of_related_posts',
29+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
30+
);
31+
if (!$pageSize) {
32+
$pageSize = 5;
33+
}
34+
$this->setPageSize($pageSize);
35+
36+
parent::_preparePostCollection();
37+
38+
$product = $this->getProduct();
39+
$this->_postCollection->getSelect()->joinLeft(
40+
['rl' => $product->getResource()->getTable('magefan_blog_post_relatedproduct')],
41+
'main_table.post_id = rl.post_id',
42+
['position']
43+
)->where(
44+
'rl.related_id = ?',
45+
$product->getId()
46+
);
47+
}
48+
49+
/**
50+
* Retrieve true if Display Related Posts enabled
51+
* @return boolean
52+
*/
53+
public function displayPosts()
54+
{
55+
return (bool) $this->_scopeConfig->getValue(
56+
'mfblog/product_page/related_posts_enabled',
57+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
58+
);
59+
}
60+
61+
/**
62+
* Retrieve posts instance
63+
*
64+
* @return \Magefan\Blog\Model\Category
65+
*/
66+
public function getProduct()
67+
{
68+
if (!$this->hasData('product')) {
69+
$this->setData('product',
70+
$this->_coreRegistry->registry('current_product')
71+
);
72+
}
73+
return $this->getData('product');
74+
}
75+
76+
/**
77+
* Get Block Identities
78+
* @return Array
79+
*/
80+
public function getIdentities()
81+
{
82+
return [\Magento\Catalog\Model\Product::CACHE_TAG . '_relatedposts_'.$this->getPost()->getId() ];
83+
}
84+
}

Block/Post/Info.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ class Info extends \Magento\Framework\View\Element\Template
2222
protected $_template = 'post/info.phtml';
2323

2424
/**
25+
* DEPRECATED METHOD!!!!
2526
* Retrieve formated posted date
2627
* @var string
2728
* @return string
2829
*/
2930
public function getPostedOn($format = 'Y-m-d H:i:s')
3031
{
31-
return date($format, strtotime($this->getPost()->getData('publish_time')));
32+
return $this->getPost()->getPublishDate($format);
3233
}
3334

3435
}

Block/Post/View/RelatedPosts.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,21 @@
1616
*/
1717
class RelatedPosts extends \Magefan\Blog\Block\Post\PostList\AbstractList
1818
{
19-
/**
20-
* @return void
21-
*/
22-
public function _construct()
23-
{
24-
$this->setPageSize(5);
25-
return parent::_construct();
26-
}
27-
2819
/**
2920
* Prepare posts collection
3021
*
3122
* @return void
3223
*/
3324
protected function _preparePostCollection()
3425
{
26+
$pageSize = (int) $this->_scopeConfig->getValue(
27+
'mfblog/post_view/related_posts/number_of_posts',
28+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
29+
);
30+
3531
$this->_postCollection = $this->getPost()->getRelatedPosts()
3632
->addActiveFilter()
37-
->setPageSize(
38-
(int) $this->_scopeConfig->getValue(
39-
'mfblog/post_view/related_posts/number_of_posts',
40-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
41-
)
42-
);
33+
->setPageSize($pageSize ?: 5);
4334

4435
$this->_postCollection->getSelect()->order('rl.position', 'ASC');
4536
}

0 commit comments

Comments
 (0)