You are here

function template_preprocess_views_row_insert in Views Row Insert 7

Same name and namespace in other branches
  1. 8 views_row_insert.theme.inc \template_preprocess_views_row_insert()

Implements hook_preprocess_HOOK() for theme_views_row_insert().

@inheritdoc

File

./views_row_insert.module, line 123
Views Row Insert module.

Code

function template_preprocess_views_row_insert(&$vars) {
  $view = $vars['view'];
  $rows = $vars['rows'];
  $style = $view->style_plugin;
  $options = $style->options;
  $use_plugin = isset($options['use_plugin']) ? $options['use_plugin'] : FALSE;
  $data_mode = isset($options['data_mode']) ? $options['data_mode'] : 'block';
  $insert_row = isset($options['custom_row_data']) ? $options['custom_row_data'] : '';
  $block_id = isset($options['block_name']) ? $options['block_name'] : NULL;
  $exclude_classes = isset($options['exclude_classes']) ? $options['exclude_classes'] : FALSE;
  $insert_classes = isset($options['class_name']) ? $options['class_name'] : '';
  if (!empty($insert_classes)) {
    $insert_classes = preg_replace('/[^a-zA-Z0-9-_\\s]/', '', $insert_classes);
  }
  $row_class = !empty($options['row_class']) ? $options['row_class'] : FALSE;
  $rows_number = isset($options['rows_number']) ? $options['rows_number'] : 2;
  $row_header = !empty($options['row_header']) ? TRUE : FALSE;
  $row_footer = !empty($options['row_footer']) ? TRUE : FALSE;
  $row_limit = !empty($options['row_limit_flag']) ? $options['row_limit'] : FALSE;
  if ($row_limit == 0) {
    $row_limit = FALSE;
  }

  // Make sure we are compatible with the previous version.
  if (!empty($options['show_method'])) {
    $row_limit = 1;
  }
  if ($data_mode == 'block') {
    $block_meta = explode('--views_row_insert--', $block_id);
    if (module_exists($block_meta[1])) {
      $block = block_load($block_meta[1], $block_meta[0]);
      $render_block = _block_render_blocks([
        $block,
      ]);
      $render_block = _block_get_renderable_array($render_block);
      $insert_row = drupal_render($render_block);
    }
  }

  // Here we start inserting our row.
  if ($use_plugin) {
    $new_rows = array();
    $k = 0;
    $counter = 0;
    $insert_keys = array();
    if ($row_header) {
      $new_rows[] = $insert_row;
      $insert_keys[] = _views_row_insert_last_key($new_rows);
      if ($row_limit) {
        $counter++;
      }
    }
    foreach ($rows as $id => $row) {
      $k++;
      $new_rows[] = $row;
      if ($k == $rows_number) {
        if (empty($row_limit)) {
          $new_rows[] = $insert_row;
          $insert_keys[] = _views_row_insert_last_key($new_rows);
        }
        elseif ($row_limit > $counter) {
          $new_rows[] = $insert_row;
          $insert_keys[] = _views_row_insert_last_key($new_rows);
          $counter++;
        }
        $k = 0;
      }
    }
    if ($row_footer) {
      if (empty($row_limit)) {
        $new_rows[] = $insert_row;
        $insert_keys[] = _views_row_insert_last_key($new_rows);
      }
      elseif ($row_limit > $counter) {
        $new_rows[] = $insert_row;
        $insert_keys[] = _views_row_insert_last_key($new_rows);
      }
    }
    if (!$exclude_classes) {
      $max = count($new_rows);
    }
    else {
      $max = count($rows);
    }
    foreach ($new_rows as $id => $row) {
      $classes = array();
      if (!in_array($id, $insert_keys)) {
        $classes = _views_row_insert_get_classes($options, $id + 1, $max);
        if ($row_class) {
          $classes[] = $row_class;
        }
      }
      elseif (!$exclude_classes) {
        $classes = _views_row_insert_get_classes($options, $id + 1, $max);
      }
      if (in_array($id, $insert_keys) && !empty($insert_classes)) {
        $classes[] = $insert_classes;
      }
      $vars['processed_rows'][] = _views_row_insert_wrap_row($row, $classes);
    }
  }
  else {
    $max = count($rows);
    foreach ($rows as $id => $row) {
      $classes = _views_row_insert_get_classes($options, $id + 1, $max);
      $vars['processed_rows'][] = _views_row_insert_wrap_row($row, $classes);
    }
  }
}