Hi,
I get the feeling that the ID's on the facet links are not consistent.

The goal is to highlight specific facet links using custom CSS. At the moment, I can in fact take the id (e.g. facetapi-link--147) from the facet HTML output and apply the CSS to it and it works fine. However, as soon as I select a filter, most of the ID's change. I am trying to achieve that each facet links keeps it HTML ID.

State 1 (unfiltered)

<li id="item166">
  <div class="facetapi-facet facetapi-inactive">
    <a href="/filter/?f[0]=kompetenz%3A439" rel="nofollow" class="facetapi-inactive" id="facetapi-link--120" title=" weltweit anwenden ">weltweit (26)</a>
  </div>
</li>

State 2 (filtered)

<li id="item160">
  <div class="facetapi-facet facetapi-inactive">
    <a href="/filter/?f[0]=im_field_rechtsform%3A6&f[1]=kompetenz%3A439" rel="nofollow" class="facetapi-inactive" id="facetapi-link--114" title=" weltweit anwenden ">weltweit (24)</a>
  </div>
</li>

I played around without the sorting options which seem to be responsible for assigning the ID (?) but didn't really get to a solution.

Best for me would be if the facet links had the taxonomy-tid added to the markup, or at least some kind of value that stays consistent throughout the search and the filtering process. Eventhough I read through the code already, I'm more on the frontend design end thus didn't really find a way to patch the code myself.

Perhaps I just missed out an option in the configuration only, though. So I appreciate any hints :-)

EDIT. The title was a little misleading, I didn't mean "distinct" but rather "consistent".

Comments

Daniel Schaefer’s picture

Issue summary: View changes

The title was a little misleading, I didn't mean "distinct" but rather "consistent".

redhatusr’s picture

I have the same problem trying to add icons to facet links with css. Is there any progress on this problem?

redhatusr’s picture

I am not programmer but I am trying to do some of programmers work in order to styling facet links (add icons etc...)

Trying to add term id as a css class to every facet links.
The term id exist in path of every facet link.
But I am not sure if the real path is in $item['#path']

The original widget_links.inc line 120

      // If the item has no children, it is a leaf.
      if (empty($item['#item_children'])) {
        $row['class'][] = 'leaf';
      }

The modified widget_links.inc

      // If the item has no children, it is a leaf.
      if (empty($item['#item_children'])) {

        // add new the class
        $row['class'][] = 'leaf' . ' ' . $item['#path'];
      }

What mouse hover says on firefox
http://second.virtual/collection/women?f[0]=field_frame_shape%3A77

What firebug show me
<a id="facetapi-link--29" class="facetapi-inactive" rel="nofollow" href="/collection/women?f[0]=field_frame_shape%3A77">

What $item['#path'] adds to list

<li class="leaf collection/149">
<li class="leaf collection/149">
<li class="leaf collection/149">

-IF- we findout where is the real path we can extraxt the term id from path and add it as a class, different for every facet link.
like this example:

      // If the item has no children, it is a leaf.
      if (empty($item['#item_children'])) {

        // extract term id from path 
        $termid = substr(strrchr($item['#path'], ":"), 1);


        // add new the class
        $row['class'][] = 'leaf' . ' ' . $termid;
      }

any ideas where is the real path in widget_links.inc ?

redhatusr’s picture

i found the term ID inside the $item['#query'] but i don't know how to extract it

var_dump($item['#query']);

array(1) { ["f"]=> array(1) { [0]=> string(29) "field_product%3Afield_color:5" } } 
array(1) { ["f"]=> array(1) { [0]=> string(31) "field_product%3Afield_color:156" } } 
array(1) { ["f"]=> array(1) { [0]=> string(29) "field_product%3Afield_color:3" } } 
array(1) { ["f"]=> array(1) { [0]=> string(31) "field_product%3Afield_color:154" } } 
array(1) { ["f"]=> array(1) { [0]=> string(31) "field_product%3Afield_color:145" } } 
array(1) { ["f"]=> array(1) { [0]=> string(30) "field_product%3Afield_color:14" } } 
array(1) { ["f"]=> array(1) { [0]=> string(30) "field_product%3Afield_color:16" } } 
array(1) { ["f"]=> array(1) { [0]=> string(20) "field_collection:149" } } 
array(1) { ["f"]=> array(1) { [0]=> string(15) "field_gender:73" } } 
array(1) { ["f"]=> array(1) { [0]=> string(20) "field_frame_shape:75" } } 
array(1) { ["f"]=> array(1) { [0]=> string(20) "field_frame_shape:85" } } 
array(1) { ["f"]=> array(1) { [0]=> string(20) "field_frame_shape:77" } } 
array(1) { ["f"]=> array(1) { [0]=> string(20) "field_frame_shape:78" } } 
array(1) { ["f"]=> array(1) { [0]=> string(20) "field_frame_shape:79" } } 
array(1) { ["f"]=> array(1) { [0]=> string(19) "field_face_shape:97" } } 
array(1) { ["f"]=> array(1) { [0]=> string(19) "field_face_shape:96" } } 
array(1) { ["f"]=> array(1) { [0]=> string(19) "field_face_shape:98" } } 
array(1) { ["f"]=> array(1) { [0]=> string(14) "field_brand:90" } } 
array(1) { ["f"]=> array(1) { [0]=> string(15) "field_brand:158" } } 
array(1) { ["f"]=> array(1) { [0]=> string(14) "field_brand:94" } } 
array(1) { ["f"]=> array(1) { [0]=> string(14) "field_brand:95" } } 
redhatusr’s picture

this add the tem ID to facet links when none of them are selected

      // Initializes variables passed to theme hook.
      $variables = array(
        'text' => $item['#markup'],
        'path' => $item['#path'],
        'count' => $item['#count'],
        'options' => array(
          'attributes' => $attributes,
          'html' => $item['#html'],
          'query' => $item['#query'],
        ),
      );
		
		// *********** NEW CODE *****************
		// THIS WILL EXTRACT TERM ID FROM PATH
		$termid = '';	  
		$newArray = array();
		foreach ($item['#query'] as $key => $value) {
			$newArray2 = array();
			foreach ($value as $key => $value) {
			$termid = 'f' . substr(strrchr($value, ":"), 1);
			}	
		}
		// *********** END OF NEW CODE *********	  
      // Adds the facetapi-zero-results class to items that have no results.
      if (!$item['#count']) {
        $variables['options']['attributes']['class'][] = 'facetapi-zero-results';
      }

      // Add an ID to identify this link.
      $variables['options']['attributes']['id'] = drupal_html_id('facetapi-link');

      // If the item has no children, it is a leaf.
      if (empty($item['#item_children'])) {
	  	// *********** NEW CODE *****************
		// THIS WILL ADD THE TERMID AS A CLASS TO EVERY FACET LINK
                $row['class'][] = 'leaf' . ' ' . $termid;
		// *********** END OF NEW CODE *********	 
      }
      else {
        // If the item is active or the "show_expanded" setting is selected,
        // show this item as expanded so we see its children.
kopeboy’s picture

Version: 7.x-1.3 » 7.x-1.5

Hi guys, thank you for the work you are doing.

I had just reopened this issue when I found this one: https://www.drupal.org/node/1020372#comment-9044645

I have a question for you: d9media said he as this kind of IDs <li id="item166">, which would be a good start for me, but I don't have those in my HTML..
why?

Aivee’s picture

Hey, I tried the code redhatusr wrote, but unfortunately it didn't output term ids in classes. The value in $termid was empty.

Anyone who tried it with success?

As someone whose coding skills are minimal, I can't really figure what's going on in the mentioned code, however this would be a really useful feature. I would like to do some css magic for facet with colors (I know they are working on fancy attributes to work with this, but I'm using images for colors).

cmseasy’s picture

Used #4 succesfull. I am getting <li class="leaf f1195"> in the link with f1195 as the unique taxonomy id.

cmseasy’s picture

And a warning when using #4:
Warning: Invalid argument supplied for foreach() in FacetapiWidgetLinks->buildListItems() (regel 126 van /home/tnportaal/domains/groeninfo.com/public_html/drupal-7/sites/all/modules/facetapi/plugins/facetapi/widget_links.inc).

That is the first foreach line in

		// *********** NEW CODE *****************
		// THIS WILL EXTRACT TERM ID FROM PATH
		$termid = '';	  
		$newArray = array();
		foreach ($item['#query'] as $key => $value) {
			$newArray2 = array();
			foreach ($value as $key => $value) {
			$termid = 'f' . substr(strrchr($value, ":"), 1);
			}	
		}