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
b7cbb72b
Commit
b7cbb72b
authored
Mar 11, 2017
by
joachim
Committed by
borisson_
Mar 11, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2747593
by borisson_, joachim: provide a facet clone form
parent
71abbb92
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
191 additions
and
0 deletions
+191
-0
facets.routing.yml
facets.routing.yml
+7
-0
src/Entity/Facet.php
src/Entity/Facet.php
+2
-0
src/FacetListBuilder.php
src/FacetListBuilder.php
+7
-0
src/Form/FacetCloneForm.php
src/Form/FacetCloneForm.php
+149
-0
tests/src/Functional/IntegrationTest.php
tests/src/Functional/IntegrationTest.php
+26
-0
No files found.
facets.routing.yml
View file @
b7cbb72b
...
...
@@ -34,6 +34,13 @@ entity.facets_facet.settings_form:
requirements
:
_entity_access
:
'
facets_facet.edit'
entity.facets_facet.clone_form
:
path
:
'
/admin/config/search/facets/{facets_facet}/clone'
defaults
:
_entity_form
:
'
facets_facet.clone'
requirements
:
_entity_access
:
'
facets_facet.edit'
entity.facets_facet_source.edit_form
:
path
:
'
/admin/config/search/facets/facet-sources/{facets_facet_source}/edit'
defaults
:
...
...
src/Entity/Facet.php
View file @
b7cbb72b
...
...
@@ -20,6 +20,7 @@ use Drupal\facets\FacetInterface;
* "default" = "Drupal\facets\Form\FacetSettingsForm",
* "edit" = "Drupal\facets\Form\FacetForm",
* "settings" = "Drupal\facets\Form\FacetSettingsForm",
* "clone" = "Drupal\facets\Form\FacetCloneForm",
* "delete" = "Drupal\facets\Form\FacetDeleteConfirmForm",
* },
* },
...
...
@@ -56,6 +57,7 @@ use Drupal\facets\FacetInterface;
* "add-form" = "/admin/config/search/facets/add-facet",
* "edit-form" = "/admin/config/search/facets/{facets_facet}/edit",
* "settings-form" = "/admin/config/search/facets/{facets_facet}/settings",
* "clone-form" = "/admin/config/search/facets/{facets_facet}/clone",
* "delete-form" = "/admin/config/search/facets/{facets_facet}/delete",
* }
* )
...
...
src/FacetListBuilder.php
View file @
b7cbb72b
...
...
@@ -45,6 +45,13 @@ class FacetListBuilder extends ConfigEntityListBuilder {
'url'
=>
$entity
->
toUrl
(
'settings-form'
),
);
}
if
(
$entity
->
access
(
'update'
)
&&
$entity
->
hasLinkTemplate
(
'clone-form'
))
{
$operations
[
'clone'
]
=
array
(
'title'
=>
$this
->
t
(
'Clone facet'
),
'weight'
=>
90
,
'url'
=>
$entity
->
toUrl
(
'clone-form'
),
);
}
if
(
$entity
->
access
(
'delete'
)
&&
$entity
->
hasLinkTemplate
(
'delete-form'
))
{
$operations
[
'delete'
]
=
array
(
'title'
=>
$this
->
t
(
'Delete'
),
...
...
src/Form/FacetCloneForm.php
0 → 100644
View file @
b7cbb72b
<?php
namespace
Drupal\facets\Form
;
use
Drupal\Core\Entity\EntityForm
;
use
Drupal\Core\Entity\EntityStorageInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\facets\FacetSource\FacetSourcePluginManager
;
use
Drupal\search_api
\
Display\DisplayPluginManager
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Provides a form for creating and editing facets.
*/
class
FacetCloneForm
extends
EntityForm
{
/**
* Facet source plugin manager.
*
* @var \Drupal\facets\FacetSource\FacetSourcePluginManager
*/
protected
$facetSourcePluginManager
;
/**
* The facet entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected
$facetStorage
;
/**
* Search API display source plugin manager.
*
* @var \Drupal\search_api\Display\DisplayPluginManager
*/
protected
$displayPluginManager
;
public
function
__construct
(
FacetSourcePluginManager
$facetSourcePluginManager
,
DisplayPluginManager
$displayPluginManager
,
EntityStorageInterface
$facetStorage
)
{
$this
->
facetSourcePluginManager
=
$facetSourcePluginManager
;
$this
->
displayPluginManager
=
$displayPluginManager
;
$this
->
facetStorage
=
$facetStorage
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
)
{
return
new
static
(
$container
->
get
(
'plugin.manager.facets.facet_source'
),
$container
->
get
(
'plugin.manager.search_api.display'
),
$container
->
get
(
'entity_type.manager'
)
->
getStorage
(
'facets_facet'
)
);
}
/**
* {@inheritdoc}
*/
public
function
form
(
array
$form
,
FormStateInterface
$form_state
)
{
/** @var \Drupal\facets\FacetInterface $facet */
$facet
=
$this
->
getEntity
();
if
(
strpos
(
$facet
->
getFacetSourceId
(),
'search_api:'
)
===
FALSE
)
{
// We don't know how to clone other kinds of facets.
drupal_set_message
(
'We can only clone Search API based facets.'
);
return
[];
}
/** @var \Drupal\search_api\Display\DisplayInterface[] $facet_source_definitions */
$facet_source_definitions
=
$this
->
facetSourcePluginManager
->
getDefinitions
();
// Get the base table from the facet source's view.
$facet_source_id
=
$facet
->
getFacetSourceId
();
$search_api_display_id
=
$facet_source_definitions
[
$facet_source_id
][
'display_id'
];
/** @var \Drupal\search_api\Display\DisplayInterface $current_display */
$current_display
=
$this
->
displayPluginManager
->
createInstance
(
$search_api_display_id
);
$current_index
=
$current_display
->
getIndex
()
->
id
();
// Create a list of all other search api displays that have the same index.
$options
=
[];
foreach
(
$facet_source_definitions
as
$source
)
{
$current_display
=
$this
->
displayPluginManager
->
createInstance
(
$source
[
'display_id'
]);
if
(
$current_display
->
getIndex
()
->
id
()
!==
$current_index
)
{
continue
;
}
$options
[
$source
[
'id'
]]
=
$source
[
'label'
];
}
$form
[
'destination_facet_source'
]
=
[
'#type'
=>
'radios'
,
'#title'
=>
$this
->
t
(
"Clone the facet to this facet source:"
),
'#options'
=>
$options
,
'#required'
=>
TRUE
,
];
$form
[
'name'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Name'
),
'#description'
=>
$this
->
t
(
'The administrative name used for this facet.'
),
'#default_value'
=>
$this
->
t
(
'Duplicate of @label'
,
[
'@label'
=>
$this
->
entity
->
label
()]),
'#required'
=>
TRUE
,
];
$form
[
'id'
]
=
[
'#type'
=>
'machine_name'
,
'#maxlength'
=>
50
,
'#required'
=>
TRUE
,
'#default_value'
=>
''
,
'#machine_name'
=>
[
'exists'
=>
[
$this
->
facetStorage
,
'load'
],
'source'
=>
[
'name'
],
],
];
return
$form
;
}
/**
* {@inheritdoc}
*/
protected
function
actions
(
array
$form
,
FormStateInterface
$form_state
)
{
$actions
[
'submit'
]
=
[
'#type'
=>
'submit'
,
'#value'
=>
$this
->
t
(
'Duplicate'
),
];
return
$actions
;
}
/**
* {@inheritdoc}
*/
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
/** @var \Drupal\facets\FacetInterface $facet */
$facet
=
$this
->
entity
->
createDuplicate
();
$facet
->
set
(
'name'
,
$form_state
->
getValue
(
'name'
));
$facet
->
set
(
'id'
,
$form_state
->
getValue
(
'id'
));
$facet
->
set
(
'facet_source_id'
,
$form_state
->
getValue
(
'destination_facet_source'
));
$facet
->
save
();
drupal_set_message
(
$this
->
t
(
'Facet cloned to :label'
,
[
':label'
=>
$facet
->
label
()]));
// Redirect the user to the view admin form.
$form_state
->
setRedirectUrl
(
$this
->
entity
->
toUrl
(
'edit-form'
));
}
}
tests/src/Functional/IntegrationTest.php
View file @
b7cbb72b
...
...
@@ -743,6 +743,32 @@ class IntegrationTest extends FacetsTestBase {
$this
->
assertFacetLabel
(
'item_category (0)'
);
}
/**
* Tests cloning of a facet.
*/
public
function
testClone
()
{
$id
=
"western_screech_owl"
;
$name
=
"Western screech owl"
;
$this
->
createFacet
(
$name
,
$id
);
$this
->
drupalGet
(
'admin/config/search/facets'
);
$this
->
assertText
(
'Western screech owl'
);
$this
->
assertLink
(
'Clone facet'
);
$this
->
clickLink
(
'Clone facet'
);
$clone_edit
=
[
'destination_facet_source'
=>
'search_api:views_block__search_api_test_view__block_1'
,
'name'
=>
'Eastern screech owl'
,
'id'
=>
'eastern_screech_owl'
,
];
$this
->
submitForm
(
$clone_edit
,
'Duplicate'
);
$this
->
assertText
(
'Facet cloned to Eastern screech owl'
);
$this
->
drupalGet
(
'admin/config/search/facets'
);
$this
->
assertText
(
'Western screech owl'
);
$this
->
assertText
(
'Eastern screech owl'
);
}
/**
* Configures empty behavior option to show a text on empty results.
*
...
...
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