I want to add a custom help text to my facets. Like there is a facet "filter by color" and i want to add a text like "if you dont know what c color is, read it here link".

any suggestions on how to do this effectively?

CommentFileSizeAuthor
#3 facet_api_custom_text.zip1.32 KBmarcoka

Comments

marcoka’s picture

ok i had an idea to first alter the settings form and add a textfield. then use block view alter to insert this text. i will try this. any better ideas?

kopeboy’s picture

I need this feature too. Any suggestions on how to show a description / help text on a facet block?

marcoka’s picture

StatusFileSize
new1.32 KB

i tried something and wrote a small module. you can try it. it worked when i did my tests.
maybe you or someone else can use this as a starting point.

kopeboy’s picture

Thank you marcoka.

Upon installation I get this error:

Notice: Undefined index: html_text in facet_api_custom_text_block_view_alter() (linea 45 di /srv/bindings/8036422060ee456b87e0ee13c0f08bad/code/sites/all/modules/facet_api_custom_text/facet_api_custom_text.module).

Apart from that it works, but I think you could make a slight improvement. The problem is the text/html gets styled as a list item, this is the html when adding a simple text with a WYSIWYG editor:

<div class="item-list">
  <ul class="custom_facetapi_text">
    <li class="first last">
      <p>Test</p>
    </li>
  </ul>
</div>

Then there is another list div generated by the Facet module with the actual facets.

marcoka’s picture

yes its far from perfect and was just some kidn of experiment. i t needs work. feel free to post updated versions and patches. maybe it will find its way into a custom project or into facetapi bonus module.

danon1981’s picture

This is a very welcome little feature. Actually I'm surprised something like did not already exist. I installed the module and it works but I get errors.
Notice: Trying to get property of non-object in facet_api_custom_text_block_view_alter() line 46 of

I haven't been able to find how to solve this, does someone have a solution?

danon1981’s picture

I figured it had to do with the items that did not have any value so added a condition before defining $custom_text.

if (isset ($data['content']['#settings']->settings['html_text']['value'])) {
		$custom_text = $data['content']['#settings']->settings['html_text']['value'];
                .........etc
}

Going to work on it some more to fit my use case, I might post an updated version. I believe this should be part of facet filtering.

marcoka thanks for this little module!!

marcoka’s picture

i move that to the facet api bonus issue queue. i think that is the right place.

marcoka’s picture

Project: Facet API » Facet API Bonus
Version: 7.x-1.3 » 7.x-1.x-dev
marcoka’s picture

danon is right the code should test that case. i also added a switch that will hide the block completely when you use this: #2089929: Filter plugin to hide already active facets

otherwise the block would be shown with no facets but the custom text.

<?php

/*
 * hook_form_alter
 *
 */

function facet_api_custom_text_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == 'facetapi_facet_display_form') {

    //we need an id to save the textvalue
    $id = $form['#facetapi']['facet']['field'];
    //add custom html textarea
    $form['html_text'] = array(
      '#title' => t('Your custom HTML'),
      '#type' => 'text_format',
      '#description' => t('Custom HTML that will be displayed above the facet'),
      '#format' => 'full_html',
      //'#post_render' => 'ckeditor_link_form_post_render',
      '#rows' => 15,
      '#default_value' => variable_get("html_text_" . $id . "value", '')

    );
    $form['#submit'][] = 'facet_api_custom_text_form_submit';
  }
}

/*
 * saved into a variable. maybe it is better to put it into  database
 */

function facet_api_custom_text_form_submit($form, &$form_state) {
  $id = $form['#facetapi']['facet']['field'];
  $html_text = $form_state['values']['html_text']['value'];
  variable_set("html_text_" . $id . "value", $html_text);
}


/*
 * hook_block_view_alter()
 */

function facet_api_custom_text_block_view_alter(&$data, $block) {


  if ($block->module == 'facetapi') {

/*    if($block->bid == '2443') {
      $data['content'] = '';
    }*/

    //get facet name and check for visible facets
    if(isset($data['content']['#settings']->facet)) {
      $facet_name = $data['content']['#settings']->facet;

      //check if the facet has items, otherwise hide the whole content, title, custom_text
      if(!isset($data['content'][$facet_name]['#items'])) {
        $data['content'] = '';
      }
    }


    //get custom text
    if (isset ($data['content']['#settings']->settings['html_text']['value'])) {
      $custom_text = $data['content']['#settings']->settings['html_text']['value'];
    }

    //add custom text if the facet has visible facets
    if (!empty($custom_text) && isset($data['content'][$facet_name]['#items'])) {

      $html_mode = $data['content']['#settings']->settings['html_text']['format'];
      $markup = check_markup($custom_text, $html_mode);

      //set a wrapper div
      $html_text['custom_html_text']['#theme'] = 'container';
      $html_text['custom_html_text']['#attributes']['class'][0] = 'custom_facetapi_text';
      $html_text['custom_html_text']['#children'] = $markup;

      //we can put the text at the top
      $data['content'] = array_merge($html_text, $data['content']);
    }
  }
}