0

I am stuck with this problem. I created a view (type=term) with exposed filter. Thanks to http://leanderlindahl.se/en/dynamically-filter-content-drupal-view/ I managed to display the exposed filter as a list of "buttons."

Now I want to display fields of content with these taxonomy terms in the exposed filter.

My knowledge of php is very limited, sorry for newbie mistakes. I learn new things every day.

my .module:

function exposedfilter_buttons_form_alter(&$form, &$form_state, $form_id) {
if ($form['#id'] == 'views-exposed-form-test-lektori-page') {
$form['#attached']['js'] = array(
  drupal_get_path('module', 'exposedfilter_buttons') . '/js/exposedfilter_buttons.js',
);
$links = $form['tid']['#options'];
$newlinks = array();
$my_value = views_get_view_result('lektori', 'block_1'); //trying to obtain results of the second view

foreach ($my_value as $key => $result) { //trying to assing results to $key
}
foreach ($links as $tid => $term_name) {
if (taxonomy_term_depth_get_by_tid($tid) == 1) {
   $newlinks[] = array('data' => '<span class="filter-tab"><a href="" id="' .$tid . '">' . $term_name .'  ' . $key . /*so we directly see its value for control */' '. $my_value[$key]->node_field_data_field_workshop_lektor_title . '</a></span>', 'class' => array('pop-filter-label'));
}
}

$prefix = theme(
  'item_list', array(
  'items' => $newlinks,
  'type' => 'ul',
  'attributes' => array('id' => 'pop-filter-list'),
  'container_id' => 'scope-list-wrapper',
));

$form['links'] = array(
  '#type' => 'markup',
  '#value' => $prefix,
  '#markup' => $prefix,
); 
}
// extra submit form handle
$form['#submit'][] = 'exposedfilter_buttons_form_submit';
}
// handle form submition 
function exposedfilter_buttons_form_submit($form, &$form_state){
$values = $form_state['values'];
}

What I need: $my_value[$key]->node_field_data_field_workshop_lektor_title should have $key obtained as a value 0 or 1 or 2.... so it will display correct name according to $key.

Image to make things more clear: enter image description here

Any help is highly appreciated.

2 Answers 2

1

I think you made a mistake in the foreach loop:

foreach ($my_value as $key => $result) { //trying to assing results to $key
}
foreach ($links as $tid => $term_name) {
if (taxonomy_term_depth_get_by_tid($tid) == 1) {
   $newlinks[] = array('data' => '<span class="filter-tab"><a href="" id="' .$tid . '">' . $term_name .'  ' . $key . /*so we directly see its value for control */' '. $my_value[$key]->node_field_data_field_workshop_lektor_title . '</a></span>', 'class' => array('pop-filter-label'));
}

should be something like

$view_results = array_keys($my_value);
foreach ($links as $tid => $term_name) {
  if (taxonomy_term_depth_get_by_tid($tid) == 1) {
    $key = each($view_results);
    $newlinks[] = array('data' => '<span class="filter-tab"><a href="" id="' .$tid . '">' . $term_name .'  ' . $key . /*so we directly see its value for control */' '. $my_value[$key]->node_field_data_field_workshop_lektor_title . '</a></span>', 'class' => array('pop-filter-label'));
  }
}
10
  • Ruben, many thanks. I tried that, but not working. In the exposed filter $key gets value "0" in all 3 lines. More, page gets an error: Undefined variable: depth in taxonomy_term_depth_get_by_tid()
    – user71267
    Commented Jan 11, 2017 at 15:09
  • 1
    Edited my answer a bit, but I think it needs more optimising to actually make it work. Make sure $my_value and $links are actually arrays in the key/value setup that you need. And rename it to something like $view_results for readability. Commented Jan 11, 2017 at 15:23
  • I see - I did not know about array_keys and each($something). When I try your suggested solution, page returns an error: Illegal offset type and notice: Array to string conversion. When I print_r ($view_results), I get Array ( [0] => 0 [1] => 1 [2] => 2 )
    – user71267
    Commented Jan 11, 2017 at 15:37
  • 1
    That's the desired value of the array, since you need the 0, 1 and 2 values only. What array_keys() does is create a new array with only the keys in it. Each() loops through it in a way that foreach does, but on demand. I'm trying to iterate through it simultaneously to looping through the links, in order to create what you want. What line created the error? Commented Jan 11, 2017 at 15:51
  • Line #20 is the problem. Many thanks for your help.
    – user71267
    Commented Jan 11, 2017 at 15:57
1

So this finaly worked for me. Ruben, once more many thanks for your help - it put me on a right path.

$my_value = views_get_view_result('lektori', 'block_1');
$view_results = array_keys($my_value);
foreach ($links as $tid => $term_name) {
if (taxonomy_term_depth_get_by_tid($tid) == 1) {
     $key = each($view_results);
     $actual_key = current($key);
    $newlinks[] = array('data' => '<span class="filter-tab"><a href="" id="' .$tid . '">' . $term_name .' ' . $actual_key . '   '. $my_value[$actual_key]->node_field_data_field_workshop_lektor_title . '</a></span>', 'class' => array('pop-filter-label'));
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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