manta_ray_media/random_hero_bg 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

manta_ray_media/random_hero_bg

最新稳定版本:1.0.2

Composer 安装命令:

composer require manta_ray_media/random_hero_bg

包简介

Pass array of strings and get back single string with cleared cache

关键字:

README 文档

README

To solve the problem with the caching for anonymous users, we have been working on 3 different files

pubmlst/web/themes/custom/pubmlst/pubmlst.theme
pubmlst/web/modules/custom/random_hero_bg/

node--front.html.twig

We have different random background colours to apply on the hero section, to randomise the colours without cache the results all the time, we used a module with the randomise the colours

{% set hero_rand_bg = ['bg-blue','bg-purple','bg-teal','bg-navy'] | randomBackground(hero_rand_test) %}

random_hero_bg custom module

On RandomHeroBG.php we have two functions, one function to clear first the cache for Anonymous users

{
  \Drupal::service('page_cache_kill_switch')->trigger();
  \Drupal::service('cache.render')->invalidateAll();
  
  // clear caches for anonymous 
  Cache::invalidateTags(['HIT']);
}

And then another function to delete the random background colour before randomise again the values.

public function randomBackground(array $input, $number)
{
  $this->clearCache();
  $key = array_rand($input);
  return $input[$key];
}

To double check if it is working, check if the number is changing with this code inside the randomBackground Functions

var_dump($number, $key);

Check this link for more information about cache-tags

pubmlst.theme

Add a preprocess function to clear caches for this specific node type, cleaning the session cache and the variables used on the random

function pubmlst_preprocess_node__front(&$variables){
  $variables['#cache'] = [
    'max-age' => 0,
    'contexts' => ['session']
  ];
  $variables['hero_rand_bg']['#cache']['max-age'] = 0;
  $variables['hero_rand_test']['#cache']['max-age'] = 0;
}

统计信息

  • 总下载量: 494
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固