advagg_scripts_scope_anywhere causing universal display of attachment <div> in all Views

Created on 22 February 2024, about 1 year ago

Problem/Motivation

The advagg_scripts_scope_anywhere feature in AdvAgg is causing an issue on Views where the attachment <div> is displaying on all views, regardless of whether they have an attachment or not.

views/theme/theme.inc
/**
 * Preprocess the primary theme implementation for a view.
 */
function template_preprocess_views_view(&$vars) {
  ...

  $vars['attachment_before'] = !empty($view->attachment_before) ? $view->attachment_before : '';
  $vars['attachment_after'] = !empty($view->attachment_after) ? $view->attachment_after : '';
views/theme/views-view.tpl.php
  <?php if ($attachment_before): ?>
    <div class="attachment attachment-before">
      <?php print $attachment_before; ?>
    </div>
  <?php endif; ?>


  <?php if ($attachment_after): ?>
    <div class="attachment attachment-after">
      <?php print $attachment_after; ?>
    </div>
  <?php endif; ?>

Currently attachment_before and attachment_after are always set.

/**
 * Implements hook_views_pre_render().
 */
function advagg_views_pre_render(&$view) {
  // Do not run hook if AdvAgg is disabled.
  if (!advagg_enabled()) {
    return;
  }
  // Do not run hook if setting is disabled.
  if (!variable_get('advagg_scripts_scope_anywhere', ADVAGG_SCRIPTS_SCOPE_ANYWHERE)) {
    return;
  }

  $info = "{$view->name}:{$view->current_display}";
  $prefix = "<!-- AdvAgg view:prefix:$info tag -->";
  $suffix = "<!-- AdvAgg view:suffix:$info tag -->";
  if (!isset($view->attachment_before)) {
    $view->attachment_before = '';
  }
  $view->attachment_before .= $prefix;
  if (!isset($view->attachment_after)) {
    $view->attachment_after = '';
  }
  $view->attachment_after .= $suffix;
}

Steps to reproduce

  1. Enable advagg_scripts_scope_anywhere via the UI at admin/config/development/performance/advagg or via Drush (drush vset advagg_scripts_scope_anywhere TRUE).
  2. Create multiple views, some with attachments and some without.
  3. Observe that the empty attachment
    elements (<div class="attachment attachment-before"></div><div class="attachment attachment-after"></div>) are displaying on all views, regardless of attachment presence.

    Proposed resolution

    Adjust the advagg_views_pre_render function to ensure that the attachment is only added for views where attachments are explicitly defined (e.g., not empty).

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States xlin1003

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024