Skip to main content

Change picture mapping conditionally according to field value drupal 7

Hi Drupalars,

Recently i got a task 

"to provide an option in paragraph item to display a image grid 3 by 3 or 2 by 2"
3 by 3 grid




2 by 2 grid

Adding conditional classes was fairly easy for each case , which is achieved by following code

 /**  
  * Implements hook_preprocess_entity().  
  */  
 function YOURTHEME_preprocess_entity(&$variables) {  
 if (!empty($variables['elements']['#entity']->field_show_50_50)) {  
    if ($variables['elements']['#entity']->field_show_50_50['und'][0]['value'] == "1") {  
     $variables['classes_array'][] = 'show-50-50';  
    };  
   }  
      if (!empty($variables['elements']['#entity']->field_show_100)) {  
    if ($variables['elements']['#entity']->field_show_100['und'][0]['value'] == "1") {  
     $variables['classes_array'][] = 'show-100';  
    };  
   }  
 }  

By doing , new problem arises about image aspect ratio getting distrupted.

As i was using Picture module for creating different version of image for each breakpoint for our responsive web site.

So i had to also change the picture mapping of image. So i created two new picture mapping and applied conditionally by using following code

 function YOURTHEME_field_display_paragraphs_item_alter(&$display, $context){  
  $entity = $context['entity'];  
  if($context['field']['field_name'] == 'field_image' && $context['entity']->bundle == 'voices_tile'){  
  $hostEntity = $context['entity']->hostEntity(); // image field inside a parent paragraph 
  if($hostEntity->field_show_100[LANGUAGE_NONE][0]['value'] == 1){  
  $display['settings']['picture_mapping'] = 'voice_picture_mappping_1368x455_';   
  }  
  if($hostEntity->field_show_50_50[LANGUAGE_NONE][0]['value'] == 1){  
  $display['settings']['picture_mapping'] = 'voice_images_50_50';   
  }  
  }  
 }  











Comments

Popular posts from this blog

Check validity of one time login url by Drupal 7 Services module?

Create mytut_service.info and put following code in file. I am assuming you know how to create module. name = Mytut service description = My custom Service. package = Other core = 7.x dependencies[] = services dependencies[] = rest_server Put following code in mytut_service.module file. function mytut_service_services_resources() { $api = array( 'mytut_service_service' => array( 'operations' => array( 'retrieve' => array( 'help' => 'Retrieves story by nid', 'callback' => 'mycallback', 'access callback' => 'user_access', 'access arguments' => array('access content'), 'access arguments append' => FALSE, 'args' => array( array( 'name' => 'fn', 'type...

Expense Tracking with Google Sheets

  Expense Tracking with Google Sheets: A Simple and Effective Solution Introduction I have tried numerous expense tracking apps and even Payzapp for keeping track of my monthly expenses. However, none of them quite met my requirements. Each app had its limitations, whether it was lack of customization, complicated interfaces, or simply not aligning with my personal finance management style. The Decision to Use Google Sheets Frustrated with the existing options, I decided to take a different approach and started tracking my expenses using Google Sheets. It turned out to be a simple yet powerful tool that perfectly fits my needs. Why Google Sheets? Customization : Unlike most apps, Google Sheets allows you to create and customize your tracking system. You can add any number of categories, columns, or even formulas to suit your specific needs. Accessibility : Google Sheets is available on all devices with internet access. Whether you’re on your phone, tablet, or computer, you can upda...

Register user with profile fields using services module

This json format worked for me creating user with profile2 term reference fields in which multiple selection can be done. Below  profile_student_profile is term reference field with multiple selection. { "name":"oiu", "mail":"oiu@aol.com", "field_mobile_number":{ "und":{ "0":{ "value":"987393" } } }, "pass":"adsa", "field_requirement":{ "und":{ "0":{ "value":"vzxvczx" } } }, "field_gender":{ "und":"female" }, "field_class":{ "und":[ "12" ] }, "field_area":{ "und":[ "31" ] }, "status":1, "field_preferred_fee...