populating search form filters value from url query params
This commit is contained in:
parent
0702f3bf13
commit
c5fe8c0c92
|
@ -24,6 +24,18 @@ class MaterioSapiSearchForm extends FormBase {
|
||||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||||
$lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
$lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||||
|
|
||||||
|
$keys = \Drupal::request()->get('keys');
|
||||||
|
$term = \Drupal::request()->get('term');
|
||||||
|
$filters = \Drupal::request()->get('filters');
|
||||||
|
|
||||||
|
$form['#cache'] = array(
|
||||||
|
'contexts' => array(
|
||||||
|
'url.query_args:keys',
|
||||||
|
'url.query_args:term',
|
||||||
|
'url.query_args:filters',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
$form['search'] = [
|
$form['search'] = [
|
||||||
'#type' => 'textfield',
|
'#type' => 'textfield',
|
||||||
// '#title' => $this->t('Search'),
|
// '#title' => $this->t('Search'),
|
||||||
|
@ -48,12 +60,21 @@ class MaterioSapiSearchForm extends FormBase {
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// $filters_array = [];
|
||||||
|
$filters_classes = [];
|
||||||
|
if($filters){
|
||||||
|
$filters_array = explode(',',$filters);
|
||||||
|
$filters_classes[] = 'open';
|
||||||
|
}
|
||||||
$form['filters'] = [
|
$form['filters'] = [
|
||||||
'#type' => 'fieldset',
|
'#type' => 'fieldset',
|
||||||
'#title' => t('filters'),
|
'#title' => t('filters'),
|
||||||
'#legend_attributes' => [
|
'#legend_attributes' => [
|
||||||
"class" => ["test-attribute"],
|
"class" => ["test-attribute"],
|
||||||
'@click.prevent' => "onClickFilters",
|
'@click.prevent' => "onClickFilters",
|
||||||
|
],
|
||||||
|
'#attributes' => [
|
||||||
|
"class" => $filters_classes
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -73,18 +94,23 @@ class MaterioSapiSearchForm extends FormBase {
|
||||||
}
|
}
|
||||||
$options = array($term->getName());
|
$options = array($term->getName());
|
||||||
$childs = $term->get('field_terms')->getValue();
|
$childs = $term->get('field_terms')->getValue();
|
||||||
|
$selected = null;
|
||||||
foreach ($childs as $child) {
|
foreach ($childs as $child) {
|
||||||
$child_term = \Drupal\taxonomy\Entity\Term::load($child['target_id']);
|
$child_term = \Drupal\taxonomy\Entity\Term::load($child['target_id']);
|
||||||
if($child_term->hasTranslation($lang)){
|
if($child_term->hasTranslation($lang)){
|
||||||
$child_term = \Drupal::service('entity.repository')->getTranslationFromContext($child_term, $lang);
|
$child_term = \Drupal::service('entity.repository')->getTranslationFromContext($child_term, $lang);
|
||||||
}
|
}
|
||||||
$options[$child_term->id()] = $child_term->getName();
|
$options[$child_term->id()] = $child_term->getName();
|
||||||
|
if (isset($filters_array) && in_array($child_term->id(), $filters_array)) {
|
||||||
|
$selected = $child_term->id();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$form['filters']['filter-'.$tid] = array(
|
$form['filters']['filter-'.$tid] = array(
|
||||||
'#type' => 'select',
|
'#type' => 'select',
|
||||||
// '#title' => $term->getName(),
|
// '#title' => $term->getName(),
|
||||||
// '#multiple' => true,
|
// '#multiple' => true,
|
||||||
'#options' => $options
|
'#options' => $options,
|
||||||
|
'#value' => $selected
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
|
@ -7,6 +7,8 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import qs from 'querystring-es3'
|
||||||
|
|
||||||
import SearchForm from 'vuejs/components/Form/SearchForm'
|
import SearchForm from 'vuejs/components/Form/SearchForm'
|
||||||
import { mapState, mapActions } from 'vuex'
|
import { mapState, mapActions } from 'vuex'
|
||||||
import MA from 'vuejs/api/ma-axios'
|
import MA from 'vuejs/api/ma-axios'
|
||||||
|
@ -20,7 +22,10 @@ export default {
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
...mapState({
|
...mapState({
|
||||||
canSearch: state => state.User.canSearch
|
canSearch: state => state.User.canSearch,
|
||||||
|
keys: state => state.Search.keys,
|
||||||
|
term: state => state.Search.term,
|
||||||
|
filters: state => state.Search.filters
|
||||||
}),
|
}),
|
||||||
displayform(){
|
displayform(){
|
||||||
// console.log('computed displayform')
|
// console.log('computed displayform')
|
||||||
|
@ -44,7 +49,17 @@ export default {
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
getSearchForm(){
|
getSearchForm(){
|
||||||
MA.get(`/materio_sapi/search_form`)
|
console.log('getSearchForm')
|
||||||
|
// var urlParams = new URLSearchParams(window.location.search);
|
||||||
|
// var urlParamsKeys = urlParams.keys()
|
||||||
|
const params = {
|
||||||
|
keys: this.keys,
|
||||||
|
term: this.term,
|
||||||
|
filters: this.filters
|
||||||
|
}
|
||||||
|
console.log('Search getSearchForm params', params)
|
||||||
|
const q = qs.stringify(params)
|
||||||
|
MA.get(`/materio_sapi/search_form?`+q)
|
||||||
.then(({data}) => {
|
.then(({data}) => {
|
||||||
// console.log('getSearchForm')
|
// console.log('getSearchForm')
|
||||||
this.form = data.rendered
|
this.form = data.rendered
|
||||||
|
|
Loading…
Reference in New Issue