Skip to content

Instantly share code, notes, and snippets.

@leanderlindahl
Last active September 8, 2015 09:29
Show Gist options
  • Save leanderlindahl/041d27b4abcd413f1fd5 to your computer and use it in GitHub Desktop.
Save leanderlindahl/041d27b4abcd413f1fd5 to your computer and use it in GitHub Desktop.
<?php
function popolo_custom_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
/**
* Apply the form_alter to a specific form #id
* the form #id can be found through inspecting the markup
*/
if($form['#id'] == 'views-exposed-form-filter-articles-page-1') {
/**
* Include a js and css, which was defined in popolo_custom.libraries.yml
*/
$form['#attached']['library'][] = 'popolo_custom/popolo_custom.enable';
$form['#attached']['library'][] = 'popolo_custom/popolo_custom.forms';
/**
* Extract the options from the Views Exposed Filter <select>-list
*/
$links = $form['field_tags_target_id']['#options'];
/**
* Iterate over the options ($links) to build an array ($pop_array) of links
*/
$i = 0; // Initiate counter/index
$pop_array = array();
foreach ($links as $tid => $term_name) {
if ($tid == 'All') {
$pop_array[$i]['#markup'] = '<span class="filter-tab"><a href="" class="active" id="' .$tid . '">' . $term_name . '</a></span>';
}
else {
$pop_array[$i]['#markup'] = '<span class="filter-tab"><a href="" id="' .$tid . '">' . $term_name . '</a></span>';
}
$i++; // Increase counter/index
}
/**
* Create the item-list the form should render
*/
$form['links'] = [
'#theme' => 'item_list',
'#items' => $pop_array,
'#attributes' => array('class' => array('pop-list')),
];
}
else {
//nothing...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment