Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
facets
Commits
9f937187
Commit
9f937187
authored
Apr 01, 2017
by
git
Committed by
borisson_
Apr 01, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2862703
by alan-ps: Add Reset Facets Button to summary
parent
5bac28df
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
181 additions
and
0 deletions
+181
-0
modules/facets_summary/src/Plugin/facets_summary/processor/ResetFacetsProcessor.php
.../Plugin/facets_summary/processor/ResetFacetsProcessor.php
+108
-0
modules/facets_summary/tests/src/Functional/IntegrationTest.php
...s/facets_summary/tests/src/Functional/IntegrationTest.php
+73
-0
No files found.
modules/facets_summary/src/Plugin/facets_summary/processor/ResetFacetsProcessor.php
0 → 100644
View file @
9f937187
<?php
namespace
Drupal\facets_summary\Plugin\facets_summary\processor
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Link
;
use
Drupal\facets_summary
\
FacetsSummaryInterface
;
use
Drupal\facets_summary
\
Processor\BuildProcessorInterface
;
use
Drupal\facets_summary
\
Processor\ProcessorPluginBase
;
/**
* Provides a processor that allows to reset facet filters.
*
* @SummaryProcessor(
* id = "reset_facets",
* label = @Translation("Adds reset facets link."),
* description = @Translation("When checked, this facet will add a link to reset enabled facets."),
* stages = {
* "build" = 50
* }
* )
*/
class
ResetFacetsProcessor
extends
ProcessorPluginBase
implements
BuildProcessorInterface
{
/**
* {@inheritdoc}
*/
public
function
build
(
FacetsSummaryInterface
$facets_summary
,
array
$build
,
array
$facets
)
{
$conf
=
$facets_summary
->
getProcessorConfigs
()[
$this
->
getPluginId
()];
// Do nothing if there are no selected facets or reset text is empty.
if
(
empty
(
$build
[
'#items'
])
||
empty
(
$conf
[
'settings'
][
'link_text'
]))
{
return
$build
;
}
$request
=
\
Drupal
::
requestStack
()
->
getMasterRequest
();
$query_params
=
$request
->
query
->
all
();
// Bypass all active facets and remove them from the query parameters array.
foreach
(
$facets
as
$facet
)
{
$url_alias
=
$facet
->
getUrlAlias
();
$filter_key
=
$facet
->
getFacetSourceConfig
()
->
getFilterKey
()
?:
'f'
;
if
(
isset
(
$query_params
[
$filter_key
]))
{
foreach
(
$query_params
[
$filter_key
]
as
$delta
=>
$param
)
{
if
(
strpos
(
$param
,
$url_alias
.
':'
)
!==
FALSE
)
{
unset
(
$query_params
[
$filter_key
][
$delta
]);
}
}
if
(
!
$query_params
[
$filter_key
])
{
unset
(
$query_params
[
$filter_key
]);
}
}
}
// Lets use any first facet to get correct url.
$results
=
reset
(
$facets
)
->
getResults
();
/** @var \Drupal\Core\Url $first_item_url */
$first_item_url
=
reset
(
$results
)
->
getUrl
();
$first_item_url
=
clone
(
$first_item_url
);
$first_item_url
->
setOptions
([
'query'
=>
$query_params
]);
$item
=
(
new
Link
(
$conf
[
'settings'
][
'link_text'
],
$first_item_url
))
->
toRenderable
();
array_unshift
(
$build
[
'#items'
],
$item
);
return
$build
;
}
/**
* {@inheritdoc}
*/
public
function
buildConfigurationForm
(
array
$form
,
FormStateInterface
$form_state
,
FacetsSummaryInterface
$facets_summary
)
{
// By default, there should be no config form.
$processors
=
$facets_summary
->
getProcessors
();
$config
=
isset
(
$processors
[
$this
->
getPluginId
()])
?
$processors
[
$this
->
getPluginId
()]
:
NULL
;
$build
[
'link_text'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Reset facets link text'
),
'#default_value'
=>
!
is_null
(
$config
)
?
$config
->
getConfiguration
()[
'link_text'
]
:
$this
->
defaultConfiguration
()[
'link_text'
],
];
return
$build
;
}
/**
* {@inheritdoc}
*/
public
function
defaultConfiguration
()
{
return
[
'link_text'
=>
''
];
}
/**
* {@inheritdoc}
*/
public
function
isHidden
()
{
return
FALSE
;
}
/**
* {@inheritdoc}
*/
public
function
isLocked
()
{
return
FALSE
;
}
}
modules/facets_summary/tests/src/Functional/IntegrationTest.php
View file @
9f937187
...
...
@@ -3,6 +3,7 @@
namespace
Drupal\Tests\facets_summary\Functional
;
use
Drupal\Tests\facets\Functional\FacetsTestBase
;
use
Drupal\facets_summary
\
Entity\FacetsSummary
;
use
Drupal\views\Views
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
...
...
@@ -86,6 +87,8 @@ class IntegrationTest extends FacetsTestBase {
// Enable a facet and check it's status after saving.
$this
->
drupalPostForm
(
NULL
,
[
'facets[llama][checked]'
=>
TRUE
],
'Save'
);
$this
->
assertFieldChecked
(
'edit-facets-llama-checked'
);
$this
->
configureResetFacetsProcessor
();
}
/**
...
...
@@ -170,6 +173,8 @@ class IntegrationTest extends FacetsTestBase {
->
findById
(
'block-'
.
$block
->
id
())
->
findAll
(
'css'
,
'li'
);
$this
->
assertCount
(
2
,
$list_items
);
$this
->
checkResetFacetsProcessor
();
}
/**
...
...
@@ -204,4 +209,72 @@ class IntegrationTest extends FacetsTestBase {
$this
->
assertEquals
(
'none'
,
$current_cache
[
'type'
]);
}
/**
* Tests configuring reset facets processor.
*/
protected
function
configureResetFacetsProcessor
()
{
$this
->
assertSession
()
->
checkboxNotChecked
(
'edit-facets-summary-settings-reset-facets-status'
);
$this
->
drupalPostForm
(
NULL
,
[
'facets_summary_settings[reset_facets][status]'
=>
TRUE
],
'Save'
);
$this
->
assertSession
()
->
checkboxChecked
(
'edit-facets-summary-settings-reset-facets-status'
);
$this
->
assertSession
()
->
pageTextContains
(
t
(
'Facets Summary Owl has been updated.'
));
$this
->
assertSession
()
->
fieldExists
(
'facets_summary_settings[reset_facets][settings][link_text]'
);
$this
->
drupalPostForm
(
NULL
,
[
'facets_summary_settings[reset_facets][settings][link_text]'
=>
'Reset facets'
],
'Save'
);
$this
->
assertSession
()
->
pageTextContains
(
t
(
'Facets Summary Owl has been updated.'
));
$this
->
assertSession
()
->
fieldValueEquals
(
'facets_summary_settings[reset_facets][settings][link_text]'
,
'Reset facets'
);
}
/**
* Tests reset facets processor.
*/
protected
function
checkResetFacetsProcessor
()
{
// Create new facets summary.
FacetsSummary
::
create
([
'id'
=>
'reset_facets'
,
'name'
=>
t
(
'Reset facets summary'
),
'facet_source_id'
=>
'search_api:views_page__search_api_test_view__page_1'
,
'facets'
=>
[
'giraffe'
=>
[
'checked'
=>
1
,
'label'
=>
'Giraffe'
,
'separator'
=>
','
,
'weight'
=>
0
,
'show_count'
=>
0
,
],
'llama'
=>
[
'checked'
=>
1
,
'label'
=>
'Llama'
,
'separator'
=>
','
,
'weight'
=>
0
,
'show_count'
=>
0
,
],
],
'processor_configs'
=>
[
'reset_facets'
=>
[
'processor_id'
=>
'reset_facets'
,
'weights'
=>
[
'build'
=>
-
10
],
'settings'
=>
[
'link_text'
=>
'Reset facets'
],
],
],
])
->
save
();
// Clear the cache after the new facet summary entity was created.
$this
->
resetAll
();
// Place a block and test reset facets processor.
$this
->
drupalPlaceBlock
(
'facets_summary_block:reset_facets'
,
[
'region'
=>
'footer'
,
'id'
=>
'reset-facets'
]);
$this
->
drupalGet
(
'search-api-test-fulltext'
);
$this
->
assertSession
()
->
pageTextContains
(
t
(
'Displaying 5 search results'
));
$this
->
assertSession
()
->
pageTextNotContains
(
t
(
'Reset facets'
));
$this
->
clickLink
(
'apple'
);
$this
->
assertSession
()
->
pageTextContains
(
t
(
'Displaying 2 search results'
));
$this
->
assertSession
()
->
pageTextContains
(
t
(
'Reset facets'
));
$this
->
clickLink
(
t
(
'Reset facets'
));
$this
->
assertSession
()
->
pageTextContains
(
t
(
'Displaying 5 search results'
));
$this
->
assertSession
()
->
pageTextNotContains
(
t
(
'Reset facets'
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment