Red Route: Adding a class to default images in Drupal maintenance support plans 8

As with many sites, images are an important part of the design for my art gallery listings site, The Gallery Guide. The tricky part is that the site is full of user-generated content, so not all of the listings have images attached.

Where there isn’t an image, we use a placeholder – partly for our ’tiles’ design pattern, but also because the image is an integral part of the design for an individual listing on large screens. On a small screen, this placeholder image takes up a lot of space, and doesn’t add much, so I wanted to hide it at a certain breakpoint.

It took me a while to get my head around how to get into the right place to be able to do this in Drupal maintenance support plans 8, following my old standby of sprinkling my code with dpm statements (replaced in D8 with kint). I should really have cracked out XDebug, but it does slow things down quite a bit, and I was too lazy to uncomment a few lines in php.ini. In this case it would have definitely made sense, because the kint output was so large that it slowed my browser down to a crawl – probably because I hadn’t previously read this Drupal maintenance support plansEasy article.

Having looked at the variables in scope inside template_preprocess_field, I saw that the relevant object was an instance of the class FileFieldItemList, which extends EntityReferenceFieldItemList. This is a good example of where a good IDE like PHPStorm can really help – having found the class, I could quickly navigate to its parent, and see its methods – in this case the relevant one was referencedEntities():

/**
* Implements hook_preprocess_field().
*/
function mytheme_preprocess_field(&$variables, $hook) {
switch ($variables[‘element’][‘#field_name’]) {
// Machine name of the field
case ‘field_image’:
// If this is the default image, add a class.
$images = $variables[‘element’][‘#items’]->referencedEntities();
if (empty($images)) {
$variables[‘attributes’][‘class’][] = ‘image__default’;
}
break;
}
}

Once that class has been added, we can apply CSS to it – in my case it’s in a SASS mixin that gets applied to a few different elements:

@mixin image-main {
&.image__default img {
@include breakpoint($mobile-only) {
display: none;
}
}
}

Here’s an example on the live site of a listing with no image uploaded, and by comparison, a listing with an image – as you can see, the design wouldn’t work on large screens if the placeholder image wasn’t there, but on small screens the placeholder just takes up space without giving the user anything worth looking at.

The solution isn’t perfect, because the browser will still download the image, even if it’s set to display: none, as Tim Kadlec wrote about a while ago. But it’ll do for a side project…

Tags: 

Drupal maintenance support plans
Drupal maintenance support plans 8
The Gallery Guide
theming

All tags

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Red Route: Adding a class to default images in Drupal maintenance support plans 8

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.