Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11681 blog search filter option #611

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Block/Adminhtml/System/Config/Form/InfoBlogExtra.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ protected function getSectionsJson(): string
'mfblog_post_view_comments_admin_send_to',
'mfblog_post_view_comments_admin_email_template',
'mfblog_blog_search',
'mfblog_sidebar_contents'
'mfblog_sidebar_contents',
'mfblog_sidebar_search_display_filters',
'mfblog_sidebar_search_available_filters'
]);
return $sections;
}
Expand Down
3 changes: 2 additions & 1 deletion Block/Search/PostList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
class PostList extends \Magefan\Blog\Block\Post\PostList
{

/**
* Retrieve query
* @return string
Expand All @@ -39,6 +40,7 @@ protected function _preparePostCollection()
self::POSTS_SORT_FIELD_BY_PUBLISH_TIME,
\Magento\Framework\Api\SortOrder::SORT_DESC
);
$this->_eventManager->dispatch('blog_search_prepare_post_collection_after', ['post_collection' => $this->_postCollection]);
}

/**
Expand Down Expand Up @@ -80,7 +82,6 @@ protected function _prepareLayout()
$this->escapeHtml($title)
);
}

return parent::_prepareLayout();
}

Expand Down
43 changes: 43 additions & 0 deletions Model/Config/Source/AvailableFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Blog\Model\Config\Source;

class AvailableFilters implements \Magento\Framework\Option\ArrayInterface
{
/**
* Options int
*
* @return array
*/

public function toOptionArray()
{
return [
['value' => 'category', 'label' => __('Category')],
['value' => 'author', 'label' => __('Author')],
['value' => 'tag', 'label' => __('Tag')],
['value' => 'publication_date', 'label' => __('Publication Date (From/To)')]
];
}


/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
$array = [];
foreach ($this->toOptionArray() as $item) {
$array[$item['value']] = $item['label'];
}
return $array;
}
}
29 changes: 29 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,20 @@
<field id="sort_order" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Sort Order</label>
</field>

<field id="display_filters" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Display Filters (Blog Extra)</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="available_filters" translate="label" type="multiselect" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Available Filters (Blog Extra)</label>
<source_model>Magefan\Blog\Model\Config\Source\AvailableFilters</source_model>
<depends>
<field id="enabled">1</field>
<field id="display_filters">1</field>
</depends>
</field>

</group>

<group id="contents" translate="label" type="text" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down Expand Up @@ -1040,6 +1054,21 @@
<label>Include Blog Search Into Catalog Search (BlogExtra)</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="display_filters" translate="label comment" type="select" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Display Filters (Blog Extra)</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="enable_blog_search">1</field>
</depends>
</field>
<field id="available_filters" translate="label" type="multiselect" sortOrder="145" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Available Filters (Blog Extra)</label>
<source_model>Magefan\Blog\Model\Config\Source\AvailableFilters</source_model>
<depends>
<field id="enable_blog_search">1</field>
<field id="display_filters">1</field>
</depends>
</field>
</group>

<group id="developer" translate="label" type="text" sortOrder="990" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
4 changes: 4 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@

<blog_search>
<enable_blog_search>0</enable_blog_search>
<display_filters>0</display_filters>
<available_filters>category,author,tag,publication_date</available_filters>
</blog_search>

<sidebar>
<search>
<enabled>1</enabled>
<sort_order>10</sort_order>
<display_filters>0</display_filters>
<available_filters>category,author,tag,publication_date</available_filters>
</search>
<categories>
<enabled>1</enabled>
Expand Down
4 changes: 3 additions & 1 deletion view/frontend/layout/blog_default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
<attribute name="class" value="blog-page" />
<referenceContainer name="sidebar.additional">
<block class="Magefan\Blog\Block\Sidebar" name="blog.sidebar" template="sidebar.phtml">
<block class="Magefan\Blog\Block\Sidebar\Search" name="blog.sidebar.search" template="sidebar/search.phtml" />
<block class="Magefan\Blog\Block\Sidebar\Search" name="blog.sidebar.search" template="sidebar/search.phtml" >
<container name="blog.sidebar.form.additional" as="form_additional"/>
</block>
<block class="Magefan\Blog\Block\Sidebar\Categories" name="blog.sidebar.category" template="sidebar/categories.phtml">
<action method="setCacheLifetime">
<argument name="time" xsi:type="string">86400</argument>
Expand Down
15 changes: 10 additions & 5 deletions view/frontend/templates/sidebar/search.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<label class="label" for="blog_search">
<span><?= $block->escapeHtml(__('Search')) ?></span>
</label>
<div class="control">
<div class="control mfsearch">
<input id="blog_search"
type="text"
name="q"
Expand All @@ -35,6 +35,7 @@
aria-haspopup="false"
aria-autocomplete="both"
autocomplete="off">
<?= $block->getChildHtml('form_additional') ?>
</div>
</div>
<div class="actions">
Expand All @@ -44,22 +45,26 @@
<span><?= $block->escapeHtml(__('Search')) ?></span>
</button>
</div>

</form>
</div>
</div>

<?php $script = "
document.addEventListener('DOMContentLoaded', function() {
var form = document.getElementById('blog_search_mini_form');
form.addEventListener('submit', function(event) {
event.preventDefault();
var value = document.getElementById('blog_search').value;
var bs = document.getElementById('blog_search');
var value = bs.value;
if (value) {
bs.remove();
var a = form.action;
window.location = a + ( ('/' != a.charAt(a.length - 1)) ? '/' : '' ) + encodeURI(value) + '/';
form.action = a + ( ('/' != a.charAt(a.length - 1)) ? '/' : '' ) + encodeURI(value) + '/';
}
return false;
form.submit()
});
});
});
"; ?>
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>