You are here

public function ParagraphsGridLayoutPlugin::preprocess in Paragraphs Collection 8

Adds a default set of helper variables for preprocessors and templates.

This preprocess function is the first in the sequence of preprocessing functions that are called when preparing variables of a paragraph.

Parameters

array $variables: An associative array containing:

  • elements: An array of elements to display in view mode.
  • paragraph: The paragraph object.
  • view_mode: The view mode.

Overrides ParagraphsBehaviorBase::preprocess

File

src/Plugin/paragraphs/Behavior/ParagraphsGridLayoutPlugin.php, line 258

Class

ParagraphsGridLayoutPlugin
Provides a way to define grid based layouts.

Namespace

Drupal\paragraphs_collection\Plugin\paragraphs\Behavior

Code

public function preprocess(&$variables) {
  $config = $this
    ->getConfiguration();
  if (!($layout = $variables['paragraph']
    ->getBehaviorSetting($this
    ->getPluginId(), 'layout'))) {
    return;
  }
  $layout_config = $this->gridLayoutDiscovery
    ->getLayout($layout);
  $field = $config['paragraph_reference_field'];

  // Add the the wrapper class.
  $variables['content'][$field]['#attributes']['class'] = $layout_config['wrapper_classes'];

  // Add children classes.
  $i = 0;
  $total_columns = isset($layout_config['columns']) ? count($layout_config['columns']) : 0;
  if ($total_columns > 0 && isset($variables['content'][$field]['#items'])) {
    foreach ($variables['content'][$field]['#items'] as $item) {

      // If there are more elements than columns we start over.
      if ($total_columns === $i) {
        $i = 0;
      }
      $item->_attributes = [
        'class' => $layout_config['columns'][$i]['classes'],
      ];
      $i++;
    }
  }
}