0

There is a field collection in a content type, it's name is field_jeld. This field collection has an image field, it is field_image. I also defined an image style called book_mini. I need to load the images using lightbox, to do so I set the following image format in manage display of every display mode:

enter image description here

I have to load the images in the front page so I used entity metadata wrapper, this is my code:

$nid = "A NID GET FROM SOMEWHERE ELSE";
$node = node_load($nid);
$wrapper = entity_metadata_wrapper('node',$node);
$jelds = $wrapper->field_jeld->value();
foreach ($jelds as $jeld){
    $item_id = $jeld->item_id;
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $item_id);
    $photo_field = $fc_wrapper->field_image->value();
    $image = theme('image_style',
        array(
            'style_name' => 'book_mini',
            'path' => $photo_field['uri'],
        )
    );
    print $image;
}

This code prints the image in book_mini format very well but the problem is, when I click on it, it is not loaded by lightbox and no popup is shown, in fact the image is not a link. How do I can solve this problem?


UPDATE1

I also tried this

$nid = "A NID GET FROM SOMEWHERE ELSE";
$node = node_load($nid);
$wrapper = entity_metadata_wrapper('node',$node);
$jelds = $wrapper->field_jeld->value();
foreach ($jelds as $jeld){
    $item_id = $jeld->item_id;
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $item_id);
    $photo_field = $fc_wrapper->field_image->value();
    $image = theme('image_style',
        array(
            'style_name' => 'book_mini',
            'path' => $photo_field['uri'],
        )
    );
    $name = $photo_field['filename'];
    $url=  $base_url."/sites/default/files/image/".$name;
    print "<a href='".$url."' rel='lightbox'>".$image."</a>";
}

I'm sure the $url points to the right file, after clicking the browser opens the lightbox but it doesn't show image, only white screen!


UPDATE2

The only solution I found is to use filed collection views and Views. I created a View called loadcover and selected field_jeld as a field. I also created a View of field collection views, called fc_jeld, and selected the required fields. This is the Formatter configuration of field_jeld :

enter image description here

Then using this code

print views_embed_view("loadcover","block",$nid);

it worked ! sounds the lighbox works with field collection only in the Views not the node!!!

0

In the lightbox configuration settings it allows you to define a certain class, so it popups whenever an image has that certain class. So all you have to do is to find a way to add a class to your image.

Perhaps you could use the Image URL Formater module. Set your image field format to url. (So when it prints, it will print the image url instead of the actual picture)

then concatenate a string to add html to your code.

print '<img src="' . $image . '" class="lightbox">'; 
| improve this answer | |
  • I updated the question. Ok I try your solution – M a m a D Jun 5 '15 at 6:32
  • I think you could also use rel="lightbox" aslong as the rel is inside the <img> tag. – No Sssweat Jun 5 '15 at 6:35
  • But lightbox uses the rel inside the a not img drupal.org/node/356124 – M a m a D Jun 5 '15 at 6:40
  • oh yes, you're correct. – No Sssweat Jun 5 '15 at 6:43
  • I tried 'attributes' => array('class' => 'lightbox', 'rel' => 'lightbox'), but it didn;t work – M a m a D Jun 5 '15 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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