Skip to content

Commit b287da7

Browse files
committed
Add author to post
1 parent b6f31a3 commit b287da7

File tree

22 files changed

+712
-36
lines changed

22 files changed

+712
-36
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function _prepareForm()
138138
'hidden',
139139
['name' => 'store_ids[]', 'value' => $this->_storeManager->getStore(true)->getId()]
140140
);
141-
$model->setStoreIds([$this->_storeManager->getStore(true)->getId()]);
141+
$model->setStoreIds($this->_storeManager->getStore(true)->getId());
142142
}
143143

144144
$field = $fieldset->addField(

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ class Main extends \Magento\Backend\Block\Widget\Form\Generic implements \Magent
2323
*/
2424
protected $_categoryOption;
2525

26+
/**
27+
* @var \Magefan\Blog\Model\Config\Source\Author
28+
*/
29+
protected $_authoryOption;
30+
2631
/**
2732
* @param \Magento\Backend\Block\Template\Context $context
2833
* @param \Magento\Framework\Registry $registry
2934
* @param \Magento\Framework\Data\FormFactory $formFactory
3035
* @param \Magento\Store\Model\System\Store $systemStore
3136
* @param \Magefan\Blog\Model\Config\Source\Category $categoryOption
37+
* @param \Magefan\Blog\Model\Config\Source\Author $authoryOption
3238
* @param array $data
3339
*/
3440
public function __construct(
@@ -37,10 +43,12 @@ public function __construct(
3743
\Magento\Framework\Data\FormFactory $formFactory,
3844
\Magento\Store\Model\System\Store $systemStore,
3945
\Magefan\Blog\Model\Config\Source\Category $categoryOption,
46+
\Magefan\Blog\Model\Config\Source\Author $authoryOption,
4047
array $data = []
4148
) {
4249
$this->_systemStore = $systemStore;
4350
$this->_categoryOption = $categoryOption;
51+
$this->_authoryOption = $authoryOption;
4452
parent::__construct($context, $registry, $formFactory, $data);
4553
}
4654

@@ -112,6 +120,18 @@ protected function _prepareForm()
112120
$model->setData('is_active', $isElementDisabled ? '0' : '1');
113121
}
114122

123+
$field = $fieldset->addField(
124+
'author_id',
125+
'select',
126+
[
127+
'name' => 'post[author_id]',
128+
'label' => __('Author'),
129+
'title' => __('Author'),
130+
'values' => $this->_authoryOption->toOptionArray(),
131+
'disabled' => $isElementDisabled,
132+
]
133+
);
134+
115135
/**
116136
* Check is single store mode
117137
*/
@@ -138,7 +158,7 @@ protected function _prepareForm()
138158
'hidden',
139159
['name' => 'post[store_ids][]', 'value' => $this->_storeManager->getStore(true)->getId()]
140160
);
141-
$model->setStoreIds([$this->_storeManager->getStore(true)->getId()]);
161+
$model->setStoreIds($this->_storeManager->getStore(true)->getId());
142162
}
143163

144164
$field = $fieldset->addField(

Block/Author/PostList.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\Author;
10+
11+
use Magento\Store\Model\ScopeInterface;
12+
13+
/**
14+
* Blog author posts list
15+
*/
16+
class PostList extends \Magefan\Blog\Block\Post\PostList
17+
{
18+
/**
19+
* Prepare posts collection
20+
*
21+
* @return void
22+
*/
23+
protected function _preparePostCollection()
24+
{
25+
parent::_preparePostCollection();
26+
if ($author = $this->getAuthor()) {
27+
$this->_postCollection->addAuthorFilter($author);
28+
}
29+
}
30+
31+
/**
32+
* Retrieve author instance
33+
*
34+
* @return \Magefan\Blog\Model\Author
35+
*/
36+
public function getAuthor()
37+
{
38+
return $this->_coreRegistry->registry('current_blog_author');
39+
}
40+
41+
/**
42+
* Preparing global layout
43+
*
44+
* @return $this
45+
*/
46+
protected function _prepareLayout()
47+
{
48+
if ($author = $this->getAuthor()) {
49+
$this->_addBreadcrumbs($author);
50+
$this->pageConfig->addBodyClass('blog-author-' . $author->getIdentifier());
51+
$this->pageConfig->getTitle()->set($author->getTitle());
52+
//$this->pageConfig->setKeywords($author->getMetaKeywords());
53+
//$this->pageConfig->setDescription($author->getMetaDescription());
54+
}
55+
56+
return parent::_prepareLayout();
57+
}
58+
59+
/**
60+
* Prepare breadcrumbs
61+
*
62+
* @param \Magefan\Blog\Model\Author $author
63+
* @throws \Magento\Framework\Exception\LocalizedException
64+
* @return void
65+
*/
66+
protected function _addBreadcrumbs($author)
67+
{
68+
if ($this->_scopeConfig->getValue('web/default/show_cms_breadcrumbs', ScopeInterface::SCOPE_STORE)
69+
&& ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs'))
70+
) {
71+
$breadcrumbsBlock->addCrumb(
72+
'home',
73+
[
74+
'label' => __('Home'),
75+
'title' => __('Go to Home Page'),
76+
'link' => $this->_storeManager->getStore()->getBaseUrl()
77+
]
78+
);
79+
80+
$breadcrumbsBlock->addCrumb(
81+
'blog',
82+
[
83+
'label' => __('Blog'),
84+
'title' => __('Go to Blog Home Page'),
85+
'link' => $this->_url->getBaseUrl()
86+
]
87+
);
88+
89+
$breadcrumbsBlock->addCrumb('blog_author',[
90+
'label' => $author->getTitle(),
91+
'title' => $author->getTitle()
92+
]);
93+
}
94+
}
95+
}

Block/Post/Info.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,28 @@ public function getPostedOn($format = 'Y-m-d H:i:s')
3232
return $this->getPost()->getPublishDate($format);
3333
}
3434

35+
/**
36+
* Retrieve 1 if display author information is enabled
37+
* @return int
38+
*/
39+
public function authorEnabled()
40+
{
41+
return (int) $this->_scopeConfig->getValue(
42+
'mfblog/author/enabled',
43+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
44+
);
45+
}
46+
47+
/**
48+
* Retrieve 1 if author page is enabled
49+
* @return int
50+
*/
51+
public function authorPageEnabled()
52+
{
53+
return (int) $this->_scopeConfig->getValue(
54+
'mfblog/author/page_enabled',
55+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
56+
);
57+
}
58+
3559
}

Controller/Adminhtml/Post/Save.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Save extends \Magefan\Blog\Controller\Adminhtml\Post
2222
*/
2323
protected function _beforeSave($model, $request)
2424
{
25-
/* prepare publish date */
25+
/* Prepare publish date */
2626
$dateFilter = $this->_objectManager->create('Magento\Framework\Stdlib\DateTime\Filter\Date');
2727
$data = $model->getData();
2828

@@ -34,7 +34,13 @@ protected function _beforeSave($model, $request)
3434
$data = $inputFilter->getUnescaped();
3535
$model->setData($data);
3636

37-
/* prepare relative links */
37+
/* Prepare author */
38+
if (!$model->getAuthorId()) {
39+
$authSession = $this->_objectManager->get('Magento\Backend\Model\Auth\Session');
40+
$model->setAuthorId($authSession->getUser()->getId());
41+
}
42+
43+
/* Prepare relative links */
3844
if ($links = $request->getPost('links')) {
3945

4046
$jsHelper = $this->_objectManager->create('Magento\Backend\Helper\Js');
@@ -51,7 +57,7 @@ protected function _beforeSave($model, $request)
5157
}
5258
}
5359

54-
/* prepare featured image */
60+
/* Prepare featured image */
5561
$imageField = 'featured_img';
5662
$fileSystem = $this->_objectManager->create('Magento\Framework\Filesystem');
5763
$mediaDirectory = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
@@ -77,7 +83,7 @@ protected function _beforeSave($model, $request)
7783
$model->setData($imageField, Post::BASE_MEDIA_PATH . $result['file']);
7884
} catch (\Exception $e) {
7985
if ($e->getCode() != \Magento\Framework\File\Uploader::TMP_NAME_EMPTY) {
80-
throw new FrameworkException($e->getMessage());
86+
throw new \Exception($e->getMessage());
8187
}
8288
}
8389
}

Controller/Author/View.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Controller\Author;
10+
11+
use \Magento\Store\Model\ScopeInterface;
12+
13+
/**
14+
* Blog author posts view
15+
*/
16+
class View extends \Magento\Framework\App\Action\Action
17+
{
18+
/**
19+
* View blog author action
20+
*
21+
* @return \Magento\Framework\Controller\ResultInterface
22+
*/
23+
public function execute()
24+
{
25+
$config = $this->_objectManager->get('\Magento\Framework\App\Config\ScopeConfigInterface');
26+
27+
$enabled = (int) $config->getValue('mfblog/author/enabled',
28+
ScopeInterface::SCOPE_STORE);
29+
$pageEnabled = (int) $config->getValue('mfblog/author/page_enabled',
30+
ScopeInterface::SCOPE_STORE);
31+
32+
if (!$enabled || !$pageEnabled) {
33+
$this->_forward('index', 'noroute', 'cms');
34+
return;
35+
}
36+
37+
$author = $this->_initCategory();
38+
if (!$author) {
39+
$this->_forward('index', 'noroute', 'cms');
40+
return;
41+
}
42+
43+
$this->_objectManager->get('\Magento\Framework\Registry')->register('current_blog_author', $author);
44+
45+
$this->_view->loadLayout();
46+
$this->_view->renderLayout();
47+
}
48+
49+
/**
50+
* Init author
51+
*
52+
* @return \Magefan\Blog\Model\Author || false
53+
*/
54+
protected function _initCategory()
55+
{
56+
$id = $this->getRequest()->getParam('id');
57+
58+
$author = $this->_objectManager->create('Magefan\Blog\Model\Author')->load($id);
59+
60+
if (!$author->getId()) {
61+
return false;
62+
}
63+
64+
return $author;
65+
}
66+
67+
}

0 commit comments

Comments
 (0)