masonry_views.module in Masonry Views 8
Same filename and directory in other branches
Provides a Views plugin for displaying content in a Masonry layout.
Sponsored by: www.freelance-drupal.com
File
masonry_views.moduleView source
<?php
/**
* @file
* Provides a Views plugin for displaying content in a Masonry layout.
*
* Sponsored by: www.freelance-drupal.com
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Template\Attribute;
/**
* Preprocess function for views-view-masonry.html.twig
*/
function masonry_views_preprocess_views_view_masonry(&$variables) {
// Get view options
$view = $variables['view'];
$rows = $variables['rows'];
$style = $view->style_plugin;
$options = $style->options;
// Build the view container.
$container = '.masonry-layout-' . Html::cleanCssIdentifier($view->storage
->id());
// If grouping enabled, style it.
if (!empty($options['grouping'])) {
$variables['grouping'] = TRUE;
static $groupid;
$container .= ' .masonry-group-' . ++$groupid;
$variables['grouping_class'] = "masonry-group masonry-group-{$groupid}";
}
// Add rows default and custom CSS classes.
$variables['default_row_class'] = !empty($options['default_row_class']);
foreach ($rows as $id => $row) {
$variables['rows'][$id] = [];
$variables['rows'][$id]['content'] = $row;
$variables['rows'][$id]['attributes'] = new Attribute();
$variables['rows'][$id]['attributes']
->addClass("masonry-item");
if ($row_class = $view->style_plugin
->getRowClass($id)) {
$variables['rows'][$id]['attributes']
->addClass($row_class);
}
}
// Display content in a Masonry layout.
$item_selector = '.masonry-item';
\Drupal::service('masonry.service')
->applyMasonryDisplay($variables, $container, $item_selector, $options, [
'masonry_view',
'masonry_view_' . $view->storage
->id(),
'masonry_view_display_' . $view->current_display,
]);
}
/**
* Implements hook_preprocess_views_view().
*/
function masonry_views_preprocess_views_view(&$variables) {
$view = $variables['view'];
$style = $view->style_plugin;
if ($style
->getPluginId() === 'masonry' && !empty($variables['rows'])) {
$variables['rows']['#theme_wrappers']['container']['#attributes']['data-drupal-masonry-layout'] = TRUE;
$variables['rows']['#theme_wrappers']['container']['#attributes']['class'][] = 'masonry-layout-' . Html::cleanCssIdentifier($view->storage
->id());
}
}
Functions
Name | Description |
---|---|
masonry_views_preprocess_views_view | Implements hook_preprocess_views_view(). |
masonry_views_preprocess_views_view_masonry | Preprocess function for views-view-masonry.html.twig |