template_preprocess_views_view_grid(&$variables)
Prepares variables for views grid style templates.
Default template: views-view-grid.html.twig.
Parameters
array $variables: An associative array containing:
- view: The view object.
- rows: An array of row items. Each row is an array of content.
File
- core/modules/views/views.theme.inc, line 675
- Preprocessors and helper functions to make theming easier.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | function template_preprocess_views_view_grid(& $variables ) { $options = $variables [ 'options' ] = $variables [ 'view' ]->style_plugin->options; $horizontal = ( $options [ 'alignment' ] === 'horizontal' ); $col = 0; $row = 0; $items = array (); $remainders = count ( $variables [ 'rows' ]) % $options [ 'columns' ]; $num_rows = floor ( count ( $variables [ 'rows' ]) / $options [ 'columns' ]); // Iterate over each rendered views result row. foreach ( $variables [ 'rows' ] as $result_index => $item ) { // Add the item. if ( $horizontal ) { $items [ $row ][ 'content' ][ $col ][ 'content' ] = $item ; } else { $items [ $col ][ 'content' ][ $row ][ 'content' ] = $item ; } // Create attributes for rows. if (! $horizontal || ( $horizontal && empty ( $items [ $row ][ 'attributes' ]))) { $row_attributes = array ( 'class' => array ()); // Add custom row classes. $row_class = array_filter ( explode ( ' ' , $variables [ 'view' ]->style_plugin->getCustomClass( $result_index , 'row' ))); if (! empty ( $row_class )) { $row_attributes [ 'class' ] = array_merge ( $row_attributes [ 'class' ], $row_class ); } // Add row attributes to the item. if ( $horizontal ) { $items [ $row ][ 'attributes' ] = new Attribute( $row_attributes ); } else { $items [ $col ][ 'content' ][ $row ][ 'attributes' ] = new Attribute( $row_attributes ); } } // Create attributes for columns. if ( $horizontal || (! $horizontal && empty ( $items [ $col ][ 'attributes' ]))) { $col_attributes = array ( 'class' => array ()); // Add default views column classes. // Add custom column classes. $col_class = array_filter ( explode ( ' ' , $variables [ 'view' ]->style_plugin->getCustomClass( $result_index , 'col' ))); if (! empty ( $col_class )) { $col_attributes [ 'class' ] = array_merge ( $col_attributes [ 'class' ], $col_class ); } // Add automatic width for columns. if ( $options [ 'automatic_width' ]) { $col_attributes [ 'style' ] = 'width: ' . (100 / $options [ 'columns' ]) . '%;' ; } // Add column attributes to the item. if ( $horizontal ) { $items [ $row ][ 'content' ][ $col ][ 'attributes' ] = new Attribute( $col_attributes ); } else { $items [ $col ][ 'attributes' ] = new Attribute( $col_attributes ); } } // Increase, decrease or reset appropriate integers. if ( $horizontal ) { if ( $col == 0 && $col != ( $options [ 'columns' ] - 1)) { $col ++; } elseif ( $col >= ( $options [ 'columns' ] - 1)) { $col = 0; $row ++; } else { $col ++; } } else { $row ++; if (! $remainders && $row == $num_rows ) { $row = 0; $col ++; } elseif ( $remainders && $row == $num_rows + 1) { $row = 0; $col ++; $remainders --; } } } // Add items to the variables array. $variables [ 'items' ] = $items ; } |
Please login to continue.