Account created on 21 November 2011, about 13 years ago
#

Recent comments

πŸ‡ΊπŸ‡ΈUnited States luke adams

Just wound up here as well after updating to 10.3. Seems like BigPipe is the issue.

Note: If you're hosted on Pantheon, you can simply disable BigPipe module to resolve the issue, no patching or custom code required.

Ref: https://docs.pantheon.io/modules-known-issues#bigpipe

πŸ‡ΊπŸ‡ΈUnited States luke adams

This bit of code, or something similar, should be a candidate for this module:

https://www.drupal.org/project/advagg/issues/3196959#comment-15691865 πŸ› Disable "Convert absolute paths to be protocol relative paths." on AMP pages to avoid validation error Needs review

By overriding the the advagg JsOptimize Asset, if advagg is present, a conditional can be added to check for ampproject URLs and return the path before the protocol is stripped off.

πŸ‡ΊπŸ‡ΈUnited States luke adams

I just ran into this today and figured out a solution that seems to be working nicely.

In my case I extended the advagg JsOptimizer with my own service decorator and class as follows.

my_module.services.yml

services:
  my_module.js_optimizer:
    class: Drupal\my_module\Asset\AmpJsOptimizer
    public: false
    decorates: advagg.optimizer.js
    decoration_priority: 10
    arguments: [ '@config.factory', '@event_dispatcher', '@cache.advagg', '@my_module.js_optimizer.inner' ]

/my_module/src/Asset/AmpJsOptimizer.php

<?php

namespace Drupal\my_module\Asset;

use Drupal\advagg\Asset\JsOptimizer;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;

/**
 * Extends the JsOptimizer class.
 */
class AmpJsOptimizer extends JsOptimizer {

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, ContainerAwareEventDispatcher $event_dispatcher, CacheBackendInterface $cache) {
    $this->extension = 'js';
    parent::__construct($config_factory, $event_dispatcher, $cache);
  }

  /**
   * Override the convertPathProtoclRelative method from Advagg Optimizer.
   *
   * @param string $path
   *   Path to check.
   *
   * @return string
   *   The converted path or the original path if already protocol relative.
   */
  protected function convertPathProtocolRelative($path) {
    if (str_contains($path, 'ampproject.org')) {
      return $path;
    }

    if (strpos($path, 'https://') === 0) {
      $path = substr($path, 6);
    }
    elseif (strpos($path, 'http://') === 0) {
      $path = substr($path, 5);
    }
    return $path;
  }

}

This has let me override the `convertPathProtocolRelative()` method only (defined in `advagg/src/Asset/AssetOptimizer`), to add a check for ampproject URL, and if so, just return the path un-altered. I've checked that the rest of the methods being called from the parent Classes are all running as they should, and I'm seeing my ampproject URLs coming through with full protocols.

First time writing a decorator, so if something looks weird to anyone who's done this before, please let me know!

πŸ‡ΊπŸ‡ΈUnited States luke adams

I know this post is a little old, however, if you're just trying to load the jquery.min.js file from Google's CDN, the following will work. Can be placed in any custom module you've got.


/**
 * Implements hook_js_alter().
 */
function MY_MODULE_js_alter(&$javascript) {
  $module_handler = \Drupal::service('module_handler');
  $advagg_exists = $module_handler->moduleExists('advagg_cdn');

  if ($advagg_exists) {
    return;
  }

  // Modified from advagg_cdn module.  This module was removed from 6.0 version.
  // Get JQuery from Google cdn.
  $jquery_lib = 'core/assets/vendor/jquery/jquery.min.js';
  if (!empty($javascript[$jquery_lib])) {
    $url = 'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js';
    $javascript[$jquery_lib]['type'] = 'external';
    $javascript[$jquery_lib]['data'] = $url;
    $javascript[$jquery_lib]['version'] = '3.5.1';
  }
}
πŸ‡ΊπŸ‡ΈUnited States luke adams

@justcaldwell good call, I'll check that out, thanks!

@Anybody, I'll check out Entity Field Condition, seems like it may solve my issue. I did write some code that allows me to do the behavior I mentioned, for both sections and blocks. However, it's a wee bit crude as it only allows for one condition per section/block, naturally as soon as I wrote it I came across the need to have multiple conditions on the same section/block!

@danflanagan8, Thanks for the plug on the Boolean module, perhaps there's an opportunity to collaborate on it!

πŸ‡ΊπŸ‡ΈUnited States luke adams

I think another very useful set of functionality here would be for empty vs non-empty node fields.

ie. If node_type:field_a is empty, do not render this block, and if node_type:field_a is !empty, do render this block.

Production build 0.71.5 2024