You are here

function paragraphs_grid_preprocess_field in Paragraphs grid 8

Implements hook_preprocess_field().

File

./paragraphs_grid.module, line 82
Contains paragraphs_grid.module.

Code

function paragraphs_grid_preprocess_field(&$variables) {
  if ($variables['field_type'] == 'entity_reference_revisions' && $variables['element']['#formatter'] == 'paragraphs_grid_formatter') {

    // Get module configuration.
    $module_config = \Drupal::config('paragraphs_grid.settings');

    // Get configured grid-type-definition.
    $grid_type = $module_config
      ->get('gridtype');
    $grid_config = $grid_type ? \Drupal::config($grid_type) : NULL;

    // Set the grid wrapper classes.
    $variables['container_attributes'] = isset($variables['element']['#container_attributes']) ? $variables['element']['#container_attributes'] : new Attribute([]);
    $variables['row_attributes'] = new Attribute([]);
    if ($grid_config) {

      // Set grid row classes.
      $wrap_row = $grid_config
        ->get('wrapper.row');
      $variables['row_attributes']
        ->addClass($wrap_row['options']);

      // Set grid cell classes.
      foreach ($variables['items'] as &$item) {

        // Get cell fallback class.
        $fallback = $grid_config
          ->get('cell-fallback') ?: '';

        // Set cell classes.
        $field_grid = _paragraphs_grid_get_field_by_type($item['content']['#paragraph'], 'grid_field_type');
        $field_grid_val = $field_grid ? $field_grid->value : '';
        $classes = $field_grid_val ? explode(' ', $field_grid_val) : [];
        $classes = count($classes) ? $classes : [
          $fallback,
        ];

        /* @var Drupal\Core\Template\Attribute $item['attributes'] */
        $item['attributes']
          ->addClass($classes);
      }
    }
  }
}