Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uw_ct_service
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WCMS
uw_ct_service
Commits
fb1d6f1c
Commit
fb1d6f1c
authored
3 years ago
by
Eric Bremner
Browse files
Options
Downloads
Patches
Plain Diff
ISTWCMS-5127: updating services search block
parent
7b3d036b
No related branches found
No related tags found
3 merge requests
!60
ISTWCMS-6095 Update maxlength settings for title, text fields and link fields...
,
!25
ISTWCMS-5779 Click update button under...
,
!8
Feature/istwcms 5127 ebremner services blocks gui
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Form/ServiceSearchForm.php
+8
-198
8 additions, 198 deletions
src/Form/ServiceSearchForm.php
with
8 additions
and
198 deletions
src/Form/ServiceSearchForm.php
+
8
−
198
View file @
fb1d6f1c
...
@@ -5,12 +5,6 @@ namespace Drupal\uw_ct_service\Form;
...
@@ -5,12 +5,6 @@ namespace Drupal\uw_ct_service\Form;
use
Drupal\Core\Form\FormBase
;
use
Drupal\Core\Form\FormBase
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Url
;
use
Drupal\Core\Url
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\Core\Path\CurrentPathStack
;
use
Drupal\Core\Routing\RouteMatchInterface
;
use
Drupal\path_alias
\AliasManager
;
use
Drupal\Core\Messenger\MessengerInterface
;
/**
/**
* Class ServiceSearchForm.
* Class ServiceSearchForm.
...
@@ -19,80 +13,6 @@ use Drupal\Core\Messenger\MessengerInterface;
...
@@ -19,80 +13,6 @@ use Drupal\Core\Messenger\MessengerInterface;
*/
*/
class
ServiceSearchForm
extends
FormBase
{
class
ServiceSearchForm
extends
FormBase
{
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected
$entityTypeManager
;
/**
* The variable for current path.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected
$currentPath
;
/**
* The variable for route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected
$routeMatch
;
/**
* The variable for alias manager.
*
* @var \Drupal\path_alias\AliasManager
*/
protected
$aliasManager
;
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected
$messenger
;
/**
* Class constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Path\CurrentPathStack $currentPath
* The currentPath.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The routeMatch.
* @param \Drupal\path_alias\AliasManager $aliasManager
* The aliasManager.
* @param Drupal\Core\Messenger\MessengerInterface $messenger
* The Drupal messenger.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public
function
__construct
(
EntityTypeManagerInterface
$entityTypeManager
,
CurrentPathStack
$currentPath
,
RouteMatchInterface
$routeMatch
,
AliasManager
$aliasManager
,
MessengerInterface
$messenger
)
{
$this
->
entityTypeManager
=
$entityTypeManager
;
$this
->
currentPath
=
$currentPath
;
$this
->
routeMatch
=
$routeMatch
;
$this
->
aliasManager
=
$aliasManager
;
$this
->
messenger
=
$messenger
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
)
{
// Instantiates this form class.
return
new
static
(
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'path.current'
),
$container
->
get
(
'current_route_match'
),
$container
->
get
(
'path_alias.manager'
),
$container
->
get
(
'messenger'
)
);
}
/**
/**
* Returns a unique string identifying the form.
* Returns a unique string identifying the form.
*
*
...
@@ -114,109 +34,26 @@ class ServiceSearchForm extends FormBase {
...
@@ -114,109 +34,26 @@ class ServiceSearchForm extends FormBase {
* An associative array containing the structure of the form.
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* The current state of the form.
* @param int $tid
* @param string|null $description
* The term id.
* @param string $description
* The description for the input element.
* The description for the input element.
* @param bool $use_placeholder
* @param string|null $placeholder
* A flag to use a placeholder.
* @param string $placeholder
* The placeholder string.
* The placeholder string.
*
*
* @return array
* @return array
* The form structure.
* The form structure.
*/
*/
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
,
int
$tid
=
NULL
,
string
$description
=
NULL
,
bool
$use_placeholder
=
TRUE
,
string
$placeholder
=
NULL
):
array
{
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
,
string
$description
=
NULL
,
string
$placeholder
=
NULL
):
array
{
// Get the current route.
// If there is no placeholder sent, then set it to
$route
=
$this
->
routeMatch
->
getRouteName
();
// search all services.
if
(
!
$placeholder
)
{
// If the route is the all services page, set placeholder to
// search with all services.
if
(
$route
==
'view.uw_view_services.services_page'
)
{
// Set the placeholder for all services.
$placeholder
=
'Search within all services'
;
$placeholder
=
'Search within all services'
;
}
}
// If this is a service term page, then set placeholder to
// search within <service_name>.
elseif
(
$route
==
'entity.taxonomy_term.canonical'
)
{
// Load the term.
$term
=
$this
->
routeMatch
->
getParameter
(
'taxonomy_term'
);
// Ensure that we are going to use a placeholder then set it.
if
(
$use_placeholder
&&
!
$placeholder
)
{
// Set the placeholder for specific catalog.
$placeholder
=
'Search within '
.
$term
->
label
();
}
}
// If this is a node page, we need to set the tid in the form
// state so that it can be used later to load name and path.
elseif
(
$route
==
'entity.node.canonical'
)
{
// If we are to use a placeholder, set it.
if
(
$use_placeholder
&&
!
$placeholder
)
{
// Load the term in from the tid.
$term
=
$this
->
entityTypeManager
->
getStorage
(
'taxonomy_term'
)
->
load
(
$tid
);
// Set the placeholder.
$placeholder
=
'Search within '
.
$term
->
label
();
}
// Set the tid in the form state.
$form_state
->
set
(
'tid'
,
$tid
);
}
// If this is any other route mainly:
// /services/<service_name>/<new,category,audience, etc...>,
// Then we have to get the tid from the URL, but ensure that
// we are not in layout builder or error will be thrown.
else
{
// Ensure that we are not on a layout builder page.
// We do not need the tid on a layout builder page, because
// this form can never get submitted on a layout builder page.
if
(
strpos
(
$route
,
'layout_builder'
)
!==
0
)
{
// Get the current path.
$url
=
$this
->
currentPath
->
getPath
();
// Break the path into parts.
$url
=
explode
(
'/'
,
$url
);
// The tid will always be the 4th element.
$tid
=
$url
[
3
]
??
NULL
;
// One last check to ensure that we have a tid.
// If we do not have a tid, throw a Drupal error.
if
(
is_numeric
(
$tid
))
{
// If we are to use a placeholder, set it.
if
(
$use_placeholder
&&
!
$placeholder
)
{
// Load the term in from the tid.
$term
=
$this
->
entityTypeManager
->
getStorage
(
'taxonomy_term'
)
->
load
(
$tid
);
// Set the placeholder.
$placeholder
=
'Search within '
.
$term
->
label
();
}
// Set the tid into the form state, so it can be used
// in the form submit.
$form_state
->
set
(
'tid'
,
$url
[
3
]);
}
}
}
// The search input.
// The search input.
$form
[
'search_input'
]
=
[
$form
[
'search_input'
]
=
[
'#type'
=>
'textfield'
,
'#type'
=>
'textfield'
,
'#placeholder'
=>
$
use_
placeholder
?
$placeholder
:
NULL
,
'#placeholder'
=>
$placeholder
,
'#description'
=>
$description
??
NULL
,
'#description'
=>
$description
??
NULL
,
'#required'
=>
TRUE
,
'#required'
=>
TRUE
,
];
];
...
@@ -244,35 +81,8 @@ class ServiceSearchForm extends FormBase {
...
@@ -244,35 +81,8 @@ class ServiceSearchForm extends FormBase {
// Get the values of the form state.
// Get the values of the form state.
$values
=
$form_state
->
getValues
();
$values
=
$form_state
->
getValues
();
// Get the tid from form state, if there is not one
// it will be NULL.
$tid
=
$form_state
->
get
(
'tid'
);
// This is an internal URL.
// This is an internal URL.
$url
=
'internal:'
;
$url
=
'internal:/services'
;
// If the tid is NULL, we are on a /catalogs page, so we
// can just build the url from the current route.
// If tid is not NULL, we have to determine if this is a
// search all catalogs or specific and set things accordingly.
if
(
$tid
===
NULL
)
{
// Get the current route as the view for catalog
// search will handle all and specific, based on URL.
$url
.
=
Url
::
fromRoute
(
'<current>'
)
->
toString
();
}
else
{
// If the tid is 0, this is a search all catalogs,
// so set the URL to the /services.
// If not set the search to /services/<service_path>.
if
(
$tid
==
0
)
{
$url
.
=
'/services'
;
}
else
{
$url
.
=
$this
->
aliasManager
->
getAliasByPath
(
'/taxonomy/term/'
.
$tid
);
}
}
// Add the search string.
// Add the search string.
$url
.
=
'/search?search-input='
.
$values
[
'search_input'
];
$url
.
=
'/search?search-input='
.
$values
[
'search_input'
];
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment