0

I am sorry if this question was asked before, but I am getting hard time with a concept of changing url parameters that was created by GET form in Drupal 8. Here what I have done so far and what I want to do.

http://example.com/advancedsearchresult?keyword=&title=&description=&author=&location=&from=0&to=0&collection=0&media=all

so as you can see media=all and it displays:

enter image description here

It displays my query results with all the media type such as PHOTO,VIDEO,DOCUMENT OR AUDIO.

So here what I want to do, I want my URL to be updated to the specific media type and display that media type results entirely when the user clicks that result number. For instance when the user clicks 42 results next to PHOTO, I would like to update my url to media=photo.

I tried to solve it on template level in twig via:

 https://chromatichq.com/blog/creating-links-within-twig-templates-using-path-and-url 

But I wasn't successful.

Can anyone lead me to a good solution for this?For your information, I am very new to Symfony and Drupal 8(I hope my question is not vague)

Update: Sharing my code On my service I run my query in form with:

public function getResults() {

$query = $this->entityQuery->get('node');
$query->condition('type', 'item')
      ->condition('status', NODE_PUBLISHED);

if (!empty($this->getKeywords())) {
  $query->condition('field_keywords.target_id', $this->getKeywords(), 'IN');
}

if (!empty($this->getTitle())) {
  $query->condition('title', '%'.$this->getTitle().'%' ,'LIKE');
}

if (!empty($this->getDescription())) {
  $query->condition('field_physical_description.value', '%'.$this->getDescription().'%' ,'LIKE');
}

if (!empty($this->getAuthor())) {
  $query->condition('field_author_photographer.value', '%'.$this->getAuthor().'%' ,'LIKE');
}

if (!empty($this->getLocation())) {
  $query->condition('field_location.value', '%'.$this->getLocation().'%' ,'LIKE');
}

if ( $this->getFrom() != '0'){
  $query->condition('field_date.value',  $this->getFrom(), '>=');
}

if ( $this->getTo() != '0'){
  $query->condition('field_date.value',  $this->getTo(), '>=');
}

if ( $this->getCollection() != '0') {
  $query->condition('field_collection.target_id', $this->getCollection(),'IN');
}

if ( $this->getMedia() != 'all') {
  $query->condition('field_media_type.value', $this->getMedia());
  $query->pager(32);
}

return $query->execute();

}

then call pass these results to my controller here:

public function result_page() {
$this->getParameters();
$results = $this->advancedsearchSearchResult->getResults();

$nodes = \Drupal\node\Entity\Node::loadMultiple($results);

$search_result = [];

foreach ($nodes as $key=>$node) {
  $media_value = $node->get('field_media_type')->getValue();
  $media_string = $media_value[0]['value'];
  $search_result[$media_string]['title'] = $media_string;
  $search_result[$media_string]['results'][] = node_view($node, 'teaser');
}

$results = [
  '#theme' => 'advancedsearch',
  '#search_result' => $search_result,
  '#attached' => [
    'library' => [
      'advancedsearch/advancedsearch'
    ],
  ],
];

 return $results;
}

private function getParameters() {
$params = \Drupal::request()->query->all();

$detail_url = Url::fromRoute('advancedsearch.search_result_controller_result_page',$params);

$this->advancedsearchSearchResult->setKeywords($params['keyword']);
$this->advancedsearchSearchResult->setTitle($params['title']);
$this->advancedsearchSearchResult->setDescription($params['description']);
$this->advancedsearchSearchResult->setAuthor($params['author']);
$this->advancedsearchSearchResult->setLocation($params['location']);
$this->advancedsearchSearchResult->setFrom($params['from']);
$this->advancedsearchSearchResult->setTo($params['to']);
$this->advancedsearchSearchResult->setCollection($params['collection']);
$this->advancedsearchSearchResult->setMedia($params['media']);

}

then pass that variable into twig template.

  • Are you using the Views module to get results? – Andrew Nim Mar 17 '17 at 8:03
  • Are you you using views? – Andrew Nim Mar 17 '17 at 8:41
  • @AndrewNim I am not using views module – Mr.T Mar 17 '17 at 14:44
  • So it it really hard to understand what you have on your site to make a good advice as i don't see your code. Is it some custom form you build? To use the Views module would be a really good idea if you need just some filtering. If you are using some custom sql query you have to rebuild your form submit code. – Andrew Nim Mar 17 '17 at 16:13
  • I will update my question... – Mr.T Mar 17 '17 at 16:26
1

In my opinion too much of code.

All you need is just to get parameters from URL $params = \Drupal::request()->query->all(); . Pass parameters to your getResults() function to make query with parameters as conditions. Return results to the Controller and and pass them to twig template.

You don't need setters and getters for parameters here. Just pass them to the query and use in conditions:

private function getResults($parameters) {
//...
$query->condition('field_media_type.value', $parameters['media']);
//...

}

public function result_page() {
//...
$parameters = \Drupal::request()->query->all();
$results = $this->getResults($parameters);
//... 
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.