Showing posts with label attributes. Show all posts
Showing posts with label attributes. Show all posts

Thursday 8 July 2021

How to convert image in webp format programmatically?

We are giving an example for convert normal image to webp image format programmatically. Here all code/examples are already used in our projects.

Example:

use Drupal\image\Entity\ImageStyle;
use Drupal\media\Entity\Media;
use Drupal\responsive_background_image\ResponsiveBackgroundImage;

if ($tab_img = $tab_entity->get('field_image')->getValue()) {

          $tab_img = array_shift($tab_img);

          $mid = $tab_img['target_id'];

          $media = Media::load($mid);

          $fid = $media->field_image->target_id; 

          // if you want simple image url..

          $style = 'max_160w';  //pass image format

          $url = content_asset_image_url($fid, $style);

          $variables['tab_titles'][$tab['target_id']]['icon'] = $url;

          // if you want image convert into webp image format..

          $file = File::load($fid);

          $uri = $file->getFileUri();

          $variables['tab_titles'][$tab['target_id']]['icon']  = [

            '#theme' => 'responsive_image',

            '#responsive_image_style_id' => 'responsive_asset_300x300',  //pass responsive image format

            '#alt' => 'Resource Content Asset',

            '#uri' => $uri,

            // Add `#attributes` key with alt text.

            '#attributes' => [

              'data-lazy'=> TRUE,

            ],

          ];

        }

 // Content Asset function to get Image URL

function content_asset_image_url($file_id, $style_choice) {

  $file = File::load($file_id);

  $image_uri = $file->getFileUri();

  $style = ImageStyle::load($style_choice);

  $url = $style->buildUrl($image_uri);

  return $url;

}


Note: If have any suggestions or issue regarding 'How to convert image in webp format programmatically?' then you can ask by comments. 

Friday 20 September 2019

How to add attributes in drupal 8 twig file?

Easy to use and to add attributes in drupal 8 twig file, here are some examples for attributes:
Add Class attributes..
Syntax: {{ attributes.addClass('myclass') }}
Example: <div {{ attributes.addClass('myclass') }}> <!-- any stuff here --> </div>
//here classes is the class name.
Remove Class attributes..
Syntax: {{ attributes.removeClass('myclass') }}

Example: <div {{ attributes.removeClass('myclass') }}> <!-- any stuff here --> </div>
here classes is the class name.
Add and Remove both attributes..
<div {{ attributes.addClass('myclass').removeClass('node') }}>

</div>
Add Multiple classes attribute..
{%
set classes = ['region','region-header']
%}
<div {{ attributes.addClass(classes)>
result: <div class="region region-header">

Add a setAttribute..
attributes.setAttribute($attribute, $value)
Syntax: <div{{ attributes.setAttribute('id', 'myID') }}></div>
Example: <div id="myID"></div>
 
Remove attributes..
attributes.removeAttribute($attribute)
Syntax: <div{{ attributes.removeAttribute('id') }}></div>

Check class exist or not..
attributes.hasClass($class)
Syntax:
{% if attributes.hasClass('myclass') %}
  {# here stuff #}
{% endif %}

Note: How to add attributes in drupal 8 twig file?

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...