Skip to content

Commit

Permalink
Add route_path as searchfield
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler committed Mar 5, 2024
1 parent b0dfa50 commit 6eda9e5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
17 changes: 14 additions & 3 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Rest\Exception\MissingParameterException;
use Sulu\Component\Rest\Exception\RestException;
use Sulu\Component\Rest\ListBuilder\FieldDescriptor;
use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface;
use Sulu\Component\Rest\ListBuilder\ListRepresentation;
use Sulu\Component\Rest\ListBuilder\ListRestHelperInterface;
Expand Down Expand Up @@ -173,6 +174,8 @@ protected function getFieldDescriptors(): array
->build(),
'title' => ElasticSearchFieldDescriptor::create('title', 'public.title')
->setSortField('title.raw')
->setSearchField('title')
->setSearchability(FieldDescriptor::SEARCHABILITY_YES)
->build(),
'creatorFullName' => ElasticSearchFieldDescriptor::create('creatorFullName', 'sulu_article.list.creator')
->setSortField('creatorFullName.raw')
Expand Down Expand Up @@ -208,6 +211,8 @@ protected function getFieldDescriptors(): array
->build(),
'routePath' => ElasticSearchFieldDescriptor::create('routePath')
->setVisibility(FieldDescriptorInterface::VISIBILITY_NO)
->setSearchField('route_path.value')
->setSearchability(FieldDescriptor::SEARCHABILITY_YES)
->build(),
];
}
Expand Down Expand Up @@ -248,9 +253,17 @@ public function cgetAction(Request $request): Response
$limit = \count($ids);
}

$fieldDescriptors = $this->getFieldDescriptors();

$searchFields = $this->restHelper->getSearchFields();
if (0 === \count($searchFields)) {
$searchFields = ['title'];
foreach ($fieldDescriptors as $fieldDescriptor) {
if (FieldDescriptorInterface::SEARCHABILITY_YES !== $fieldDescriptor->getSearchability()) {
continue;
}

$searchFields[] = $fieldDescriptor->getSearchField();
}
}

$searchPattern = $this->restHelper->getSearchPattern();
Expand Down Expand Up @@ -332,8 +345,6 @@ public function cgetAction(Request $request): Response
);
}

$fieldDescriptors = $this->getFieldDescriptors();

if ($limit) {
$search->setSize($limit);
$search->setFrom(($page - 1) * $limit);
Expand Down
16 changes: 14 additions & 2 deletions ListBuilder/ElasticSearchFieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,41 @@ public static function create(string $name, string $translation = null): Elastic
*/
private $sortField;

/**
* @var string
*/
private $searchField;

public function __construct(
string $name,
string $sortField = null,
string $translation = null,
string $visibility = FieldDescriptorInterface::VISIBILITY_YES,
string $searchability = FieldDescriptorInterface::SEARCHABILITY_NEVER,
string $type = '',
bool $sortable = true
bool $sortable = true,
string $searchField = '',
) {
$this->sortField = $sortField ? $sortField : $name;
$this->searchField = $searchField;

parent::__construct(
$name,
$translation,
$visibility,
$searchability,
$type,
$sortable
$sortable,
);
}

public function getSortField(): string
{
return $this->sortField;
}

public function getSearchField(): string
{
return $this->searchField;
}
}
15 changes: 14 additions & 1 deletion ListBuilder/ElasticSearchFieldDescriptorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ final class ElasticSearchFieldDescriptorBuilder
*/
private $sortable = false;

/**
* @var string
*/
private $searchField = '';

public function __construct(string $name, string $translation = null)
{
$this->name = $name;
Expand Down Expand Up @@ -85,6 +90,13 @@ public function setType(string $type)
return $this;
}

public function setSearchField(string $searchField): self
{
$this->searchField = $searchField;

return $this;
}

public function build(): ElasticSearchFieldDescriptor
{
return new ElasticSearchFieldDescriptor(
Expand All @@ -94,7 +106,8 @@ public function build(): ElasticSearchFieldDescriptor
$this->visibility,
$this->searchability,
$this->type,
$this->sortable
$this->sortable,
$this->searchField,
);
}
}
5 changes: 5 additions & 0 deletions Resources/config/lists/articles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,10 @@
</params>
</filter>
</property>
<property
name="routePath"
visibility="no"
translation="sulu_article.route"
/>
</properties>
</list>
1 change: 1 addition & 0 deletions Resources/translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"sulu_article.published_state": "Status der Veröffentlichung",
"sulu_article.published": "Veröffentlicht",
"sulu_article.not_published": "Nicht Veröffentlicht",
"sulu_article.route": "Route",
"sulu_activity.resource.articles": "Artikel",
"sulu_activity.resource.articles.translation": "Übersetzung",
"sulu_activity.description.articles.created": "{userFullName} hat den Artikel \"{resourceTitle}\" erstellt",
Expand Down
1 change: 1 addition & 0 deletions Resources/translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"sulu_article.published_state": "Published State",
"sulu_article.published": "Published",
"sulu_article.not_published": "Not Published",
"sulu_article.route": "Route",
"sulu_activity.resource.articles": "Article",
"sulu_activity.resource.articles.translation": "Translation",
"sulu_activity.description.articles.created": "{userFullName} has created the article \"{resourceTitle}\"",
Expand Down

0 comments on commit 6eda9e5

Please sign in to comment.