Does not work in a custom block type template

Created on 24 October 2023, about 1 year ago
Updated 14 December 2023, 11 months ago

Problem/Motivation

I can't make the block work as I need it to. When the "Internal Page Cache" module is turned off, I see my banner on the desktop, but not on the mobile device. And vice versa - if I log in on mobile first, I won't see the banner on the desktop.

Steps to reproduce

1) Create your own custom block type
2) Add a description to the theme file to redefine templates of your own block type:

function THEME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
	if (isset($variables['elements']['content']['#block_content'])) {
	  array_splice($suggestions, 1, 0, 'block__custom__' . $variables['elements']['content']['#block_content']->bundle());
	}
}

3) In the template, try to apply the logic of the module, my example:

{% block content %}
	{% if is_mobile() %}
		<div class="container-fluid">	
			<div class="row">	
				<div class="d-block d-md-none col-12 text-center p-0">
					{% if content.field_banner_link|field_value is not empty %}
						<a href="{{ content.field_banner_link|field_value }}">
							<img src="{{ content.field_banner_pic_mobile|field_value }}" class="img-fluid" alt="{{ label }}">
						</a>
					{% else %}
						<img src="{{ content.field_banner_pic_mobile|field_value }}" class="img-fluid" alt="{{ label }}">
					{% endif %}
				</div>
			</div><!-- / row -->
		</div><!-- / container -->
	{% else %}
		<div class="container">	
			<div class="row">	
				<div class="d-none d-md-block col-12">
					{% if content.field_banner_link|field_value is not empty %}
						<a href="{{ content.field_banner_link|field_value }}">
							<img src="{{ content.field_banner_pic|field_value }}" class="img-fluid" alt="{{ label }}">
						</a>
					{% else %}
						<img src="{{ content.field_banner_pic|field_value }}" class="img-fluid" alt="{{ label }}">
					{% endif %}
				</div>
			</div>
		</div>
	{% endif %}
{% endblock %}

Proposed resolution

As I understood from other messages, the problem is not in the module, but in the dynamic cache of pages. Perhaps you should correct the description on the module page that this solution is not suitable for working with blocks, and also forces you to sacrifice performance.

To be honest, I'm at a loss, a good and convenient module, the syntax on twig is especially convenient, but how to solve such a simple task is not clear enough? You can create separate banners for mobile and desktop devices, but this is inconvenient and will lead to garbage on the blocks page.

Drupal 9.5.11, php 8.1.

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇷🇺Russia mr.pomelov

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

Comments & Activities

Production build 0.71.5 2024