Showing posts with label entity. Show all posts
Showing posts with label entity. 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 13 March 2020

Difference between hook_entity_presave and hook_entity_insert ?

Simple way to differentiate both hooks is the 'ID' is not available in hook_entity_presave() ("pre meaning before, and without saved ID is not assigned in database),
But this 'ID' will be available in hook_entity_insert().

Example:
function mymodule_node_insert(NodeInterface $node) { 
  $nid = $node->id();
}
OR
function mymodule_node_insert($node) {
  if ($node->getType() == 'your content type') {
        $node->setTitle('New entity ' . $node->id()); // to update title with node id
        $node->field_test = 'Id' . $node->id();  // or, to update a text field with node id
        $node->save();
  }
}

function mymodule_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->getEntityType()->id() == 'node') {
    $entity->setTitle('New Title here');
    //Note : Not to save, because it's automatic.   
  }
}
OR
function mymodule_node_presave(Drupal\node\NodeInterface $node) {
  $node->setTitle('Edited Title');
  $node->set('body', 'this is the new body section');
  //Note : Not to save, because it's automatic.
}

Note: If have any suggestions or issue regarding 'Difference between hook_entity_presave and hook_entity_insert ?' then you can ask by comments.  

Thursday 8 August 2019

How to delete content node programmatically in drupal 8?

Here we have simple way to 'delete content programmatically'. We can use this code in our "custom module/plugin" or  "hook_entity_predelete" or in "hook_entity_delete". Just use that simple code..

Syntax:

 $result = \Drupal::entityQuery("node")
                ->condition('type', 'resources');  // here change the content type of your
 $nids = $result->execute();

  if (!empty($nids)) {
    $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
    $nodes->delete();
  }


#Extra notes: here we can add more conditions in 'entityQuery' as per our requirement.









Note: How to delete content node programmatically in drupal 8?

Thursday 6 September 2018

Media entity fields render in drupal 8 Node Twig template.

If we want to load media entity fields on node twig template. It's easy to load media entity, Please have a look..

{# call to media entity fields on node twig #}

{% for key, item in node.field_document_entity_browser %} 
// This is the node field which is connect to media field `field_document_entity_browser`..
{% if node.field_document_entity_browser[key].entity %}
// here we are rendering media entity field which machine name is `field_capt2120`..                           
{{ node.field_document_entity_browser.entity.field_capt2120[key].value }}

// here we are rendering media entity image field which machine name is `field_media_image`..        
{%if item.entity %}
{% set media = item.entity %}
{% set file = media.field_media_image.entity %}
{% set uri = file_url(file.uri.value) %}
                              
<img src="{{ uri }}" alt="{{ media.name.value }}" />
                          
 {% endif %}                           
                                                                                     
 {% endif %}
  {% endfor %}



Note: If have any suggestions or issue regarding 'Media entity fields render in drupal 8 Node Twig template.' then you can ask by comments.  

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...