added classes to links generated by views

This commit is contained in:
Bachir Soussi Chiadmi 2021-06-24 13:42:51 +02:00
parent 1d29f1e5f1
commit 28f21f1797
2 changed files with 67 additions and 1 deletions

View File

@ -62,3 +62,46 @@ function popsu_preprocess_views_view_unformatted(&$variables) {
// function popsu_preprocess_block(&$variables) {
// $t="t";
// }
function popsu_preprocess_views_view_field(&$variables){
// check if link to entoty option is true
$settings = $variables['field']->options['settings'];
if ($settings['link_to_entity']) {
// get the entity and build the classes
$entity = $variables['row']->_entity;
// build new classes
$new_classes = array(
$entity->getEntityTypeId(),
$entity->getEntityTypeId().'-'.$entity->bundle(),
$entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id()
);
// get the entity link and url
$link = $entity->toLink();
$url = $entity->toLink()->getUrl();
// set the active_class option to the url
$url->setOption('set_active_class', true);
// add new classes to the url (without overwriting existing once)
$options = $url->getOptions();
if($attributes = $options['attributes']){
if($classes = $attributes['class']){
$attributes['class'] += $new_classes;
}else{
$attributes['class'] = $new_classes;
}
}else{
$attributes = array(
"class" => $new_classes
);
}
$url->setOption('attributes', $attributes);
// update the url of the link
$link->setUrl($url);
// overwrite the output
$variables['output'] = $link->toRenderable();
}
}

View File

@ -0,0 +1,23 @@
{#
/**
* @file
* Default theme implementation for a single field in a view.
*
* Available variables:
* - view: The view that the field belongs to.
* - field: The field handler that can process the input.
* - row: The raw result of the database query that generated this field.
* - output: The processed output that will normally be used.
*
* When fetching output from the row this construct should be used:
* data = row[field.field_alias]
*
* The above will guarantee that you'll always get the correct data, regardless
* of any changes in the aliasing that might happen if the view is modified.
*
* @see template_preprocess_views_view_field()
*
* @ingroup themeable
*/
#}
{{ output -}}