You are here

function masonry_views_preprocess_views_view_masonry in Masonry Views 8

Preprocess function for views-view-masonry.html.twig

File

./masonry_views.module, line 15
Provides a Views plugin for displaying content in a Masonry layout.

Code

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,
  ]);
}