6

I'm currently using Drupal 8.2.3 for my website design. My aim is to use it for publishing my blogs to social media sites, such as Facebook, Twitter, LinkedIN, etc. With that in mind, i'm using image styles to create OpenGraph and Twitter meta tags in my blog posts to properly display content on various platforms.

The problem comes when I make a change to the imagestyle. When I flush the contents, it doesn't automatically regenerate the imagestyle content for my metadata. Is there a way to automatically regenerate the imagestyles and content?

Any help much appreciated.

5 Answers 5

7

There is a setting to flush the image style at admin/config/media/image-styles: enter image description here

Or you can use drush:

drush image-flush --all (flush all styles)
drush image-flush thumbnail (flush thumbnail style)
1
  • Also, don't forget about the browser cache ;) Ctrl+Shift+R to do a "hard" page reload.
    – bpile
    Commented Jul 2, 2021 at 15:36
2

If you are using Drupal Console (and let's face it, why not?) then you can use:

drupal image:styles:flush image_style

Documenation: https://docs.drupalconsole.com/en/commands/image-styles-flush.html

0

The problem comes when I make a change to the imagestyle. When I flush the contents, it doesn't automatically regenerate the imagestyle content for my metadata. Is there a way to automatically regenerate the imagestyles and content?

I have a couple of questions to you:

  • What kind of changes you make to the image style?
  • What does flushint the contents mean?
  • By imagestyle content you mean the derivated image from the original one? (so generated image file itself)
0

Sometimes - images are not showing, when seeing files in contents.

This mostly caused by server permissions.

For CentOS/Redhat/Fedora/Rocky Linux systems :

chown -R apache:apache /var/www/html/demodrupal/sites/default/
chcon -R -t httpd_sys_rw_content_t sites/default/

Two commands are required/

-3

It seems you want to generate proper metatags for your node pages (blogs). To generate them dynamically i propose to add to your module.module file next code with hook_entity_view function:

  function your_module_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

  if ($entity->getEntityTypeId() == 'node') {
      $bundle = $entity->bundle();
      switch ($bundle){
          case 'blog':
              $image = $entity->get('field_image');
              $image_id = $image->getValue()[0]['target_id'];
              $image_file = \Drupal\file\Entity\File::load($image_id);
              $uri = $image_file->uri->value;
              $facebook_image_url = Drupal\image\Entity\ImageStyle::load('your_facebook_style')->buildUrl($uri);
              $facebook_metatag = [
                  '#tag' => 'meta',
                  '#attributes' => [
                    'property' => 'og:image',
                    'content' => $facebook_image_url,
                  ],
                ];
               $build['#attached']['html_head'][] = [$facebook_metatag, 'facebook_metatag'];
// the same for other social networks:

$twitter_image_url = Drupal\image\Entity\ImageStyle::load('your_twitter_style')->buildUrl($uri);
//etc
                  break;

          }

      }

    }

And you get this metatag type meta property="og:image" content="http://static01.nyt.com/images/2015/02/19/arts/international/19iht-btnumbers19A/19iht-btnumbers19A-facebookJumbo-v2.jpg" Also you can add any metatag in such a manner:

 $facebook_metatag = [
    '#tag' => 'meta',
    '#attributes' => [
             'property' => 'og:title',
             'content' => $your_node_title,
          ],
   ];
   $build['#attached']['html_head'][] = [$facebook_metatag, 'facebook_metatag'];
3
  • I have no issue generating the metatags per se. The problem is regenerating the imagestyles (ie. thumbnails, etc.) so they will be visible for social media websites. I use the 'metatag' module and 'Share Message' module for this purpose.
    – user189842
    Commented Nov 24, 2016 at 19:05
  • As i know there is no aption in metatag module to generate different image styles. So that's why i propose to add metatags programmatically. You can generate dynamically any metatags you need including metatags with image styles. Of course it is a little more complicated without the UI of the metatag module.
    – Andrew Nim
    Commented Nov 24, 2016 at 19:46
  • token.module in 8.x has built in support for displaying images with a certaion image style.
    – Berdir
    Commented Nov 24, 2016 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.