CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • Custom data image, Drupal Views, Thumbnails?
Pages: [1] 2 3 ... 7

Author Topic: Custom data image, Drupal Views, Thumbnails?  (Read 31190 times)

lentilsoup

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 6
Custom data image, Drupal Views, Thumbnails?
October 26, 2009, 09:05:53 am
I've got an image as custom event data, which I'm pulling up in a Drupal view.  The view is returning only the image's ID.  If needed, I can write a custom query to retrieve the path so I can display the image, but I was hoping it wouldn't be necessary.  Am I missing an easy way to do this?

Also, I noticed that the event page displays the image as a thumbnail, but using the full-res image, which is very bandwidth-inefficient (and then when clicking the image to view it, prompts you to download it instead of displaying it in-browser).  Is there any way to generate and cache thumbnails, ideally using Drupal's Image module, which automatically generates thumbs at whatever sizes you configure?

Thanks!

lentilsoup

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 6
Re: Custom data image, Drupal Views, Thumbnails?
October 26, 2009, 10:29:19 am
OK, here's my quick-and-dirty solution.  If there's a better way to do this, I'd love to hear it...

Code: [Select]
function get_civicrm_image($id, $dimensions=array(200,200)) {
civicrm_initialize();
require_once('api/File.php');

// get the file details
$file = array_shift(crm_get_file(array('id'=>$id)));
$filename = $file['uri'];

$root = $_SERVER['DOCUMENT_ROOT'];
$file_ext = strrchr($filename, '.');
$file_base = substr($filename, 0, 0 - strlen($file_ext));
$old_path = $root . '/' . file_directory_path() . '/civicrm/custom/' . $filename;
$new_file = file_directory_path() . '/civicrm/custom/' . $file_base . '_' . $dimensions[0] . 'x' . $dimensions[1] . $file_ext;
if(!file_exists($root . '/' . $new_file)) {
image_scale_and_crop($old_path, $root . '/' . $new_file, $dimensions[0], $dimensions[1]);
}
return $new_file;
}

Then I call it from my view template like so:

Code: [Select]
$img_uri = url(get_civicrm_image($fields['image_1']->content, array(300, 200)));

You'll need to know the custom field ID of the image (in this case "image_1") in order to call the function.  This can be found in the Theme information section of the view -- for instance  "Field CiviCRM Custom: Event Details: Image (ID: image_1)".

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Custom data image, Drupal Views, Thumbnails?
October 26, 2009, 11:03:27 am
I'm assuming that image_scale_and_crop works only in drupal ?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

lentilsoup

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 6
Re: Custom data image, Drupal Views, Thumbnails?
October 26, 2009, 11:18:36 am
Quote from: xavier on October 26, 2009, 11:03:27 am
I'm assuming that image_scale_and_crop works only in drupal ?

Yes, it's part of Drupal's core image API -- docs are here.

nunodonato

  • Guest
Re: Custom data image, Drupal Views, Thumbnails?
November 27, 2009, 01:23:31 am
Hi there,
I'm having the same problems and would appreciate if you could give me some extra help.
I want to give a highlight to a particular type of events in the frontpage, with a custom field (image).
Like you said, views only gets the image id, how can I call that function you wrote from the views output so that I can print the image? The "rewrite output" function of views does not accept PHP :(

lentilsoup

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 6
Re: Custom data image, Drupal Views, Thumbnails?
November 27, 2009, 07:41:36 am
You need to write a custom template for your view.  You can either put the function there or in template.php for your custom theme (I recommend the latter), and then call it from the custom view template. 

DanilaD

  • I post occasionally
  • **
  • Posts: 93
  • Karma: 11
Re: Custom data image, Drupal Views, Thumbnails?
December 08, 2009, 05:52:17 am
Try using Views Custom Field module, it's very nice for small php tricks in views, and does not require a full module to be designed.

Ideally this should be packaged as a views_handler for the image field type.

fm

  • I post occasionally
  • **
  • Posts: 80
  • Karma: 1
Re: Custom data image, Drupal Views, Thumbnails?
February 23, 2010, 09:05:28 pm
I don't seem to be able to get this solution to work. I managed to get the page to stop throwing errors, but instead of an image it displays the numeral "1".
« Last Edit: February 26, 2010, 09:47:17 pm by fm »

mhm

  • I post occasionally
  • **
  • Posts: 82
  • Karma: 2
Re: Custom data image, Drupal Views, Thumbnails?
March 08, 2010, 06:39:48 pm
Me neither.

Can anyone comment on if the new image field in 3.2 will solve this problem?  (If so, I'll just ignore it for now.)

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Custom data image, Drupal Views, Thumbnails?
March 08, 2010, 08:27:27 pm

I dont think this will. We are basically just exposing the image_url in various contexts. check:

http://issues.civicrm.org/jira/browse/CRM-5467

for more details

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

fm

  • I post occasionally
  • **
  • Posts: 80
  • Karma: 1
Re: Custom data image, Drupal Views, Thumbnails?
March 08, 2010, 10:49:46 pm
Would it be possible to provide guidance for where specificially to paste lentilsoup's code snippets?

Are there any other changes to the code we need to make beyond the custom field ID?

Has anyone else successfully implemented this solution?
« Last Edit: March 08, 2010, 11:09:47 pm by fm »

lentilsoup

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 6
Re: Custom data image, Drupal Views, Thumbnails?
March 08, 2010, 11:28:19 pm
The first snippet goes in your template.php file (I'm assuming you're using a custom phptemplate theme) so that you can call it from anywhere.  Alternatively you could put it in a custom module.  The function basically takes the file ID (that number 1 that the view spits out), and gives you the path to a thumbnail of the image with dimensions you specify (creating the thumbnail if it doesn't already exist).

Second snippet goes in your custom View fields template (views-view-fields.tpl.php or views-view-fields--view-name.tpl.php, etc).  You have to replace "image_1" with the ID of your civicrm custom image field.  This info should be available somewhere in the views administration screen (I forget exactly where, and I'm not able to check right at this moment).  The second snippet basically just calls the function in the first snippet and then has Drupal turn the path into a canonical URL.  $img_uri should then contain the URL of the thumbnail, which you can print into the src attribute of an img tag.

I believe that changing the field ID is that only change you have to make, but it's possible that the solution breaks if Drupal is installed somewhere other than the domain root, in which case you'll have to fiddle with the paths in the first snippet -- in particular, this line, which can undoubtedly be rewritten to be more portable:

Code: [Select]
$root = $_SERVER['DOCUMENT_ROOT'];
If you're getting PHP errors, it's a lot easier for us to help you troubleshoot if you tell us what errors you're actually getting.

As DanilaD suggested, you can probably do something with a  Views Custom Field that requires less fiddling with the templates, but I'm not entirely familiar with how that's done, so you'll have to work that out on your own.

fm

  • I post occasionally
  • **
  • Posts: 80
  • Karma: 1
Re: Custom data image, Drupal Views, Thumbnails?
March 09, 2010, 02:55:19 pm
Thank you for the instructions.

I gave it a shot. I pasted your first snippet into my template.php file and the second part into views-view-field--civicrm-events--block-3.tpl.php. My field ID is "image_1"; I retrieved the ID in the manner you prescribed in your post above (dated October 26, 2009, 09:29:19 am). Since this field ID did not change from your example and drupal is installed at my domain root, I didn't modify the code.

The view appears in a block and the view name is "civicrm events"; there are two other blocks that rely on that same view; hence, "block-3". Neither of the other two blocks display an image field. I nested the second snippet in php tags when I put it into the views-view-field--civicrm-events--block-3.tpl.php.

Then, I clicked "Save Configuration" in Administer>Site Building>Themes.

When I go to the front-page, where this subtheme is called and where the block with the image is located, the output is still the numeral "1". I receive no warning or error message on the page. When I looked the "Recent log entries" there appear to be no associated messages.
« Last Edit: March 09, 2010, 02:57:03 pm by fm »

lentilsoup

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 6
Re: Custom data image, Drupal Views, Thumbnails?
March 09, 2010, 03:18:54 pm
fm, the first thing I'd try is to make sure that your custom view block template is being loaded correctly.  I usually do that by putting some nonsense ("asdf") or whatever in it and making sure that nonsense shows up.

I'm not sure if saving the theme configuration will do anything, but you may have to clear your theme cache, which you can do with the Devel module.  Or you can paste the following at the top of your template.php file, and clear the theme cache so it'll start to take effect:

Code: [Select]
drupal_rebuild_theme_registry(); // clear theme cache on each page load
Just remember to remove it again before taking your site live, since it slows page generation times.

fm

  • I post occasionally
  • **
  • Posts: 80
  • Karma: 1
Re: Custom data image, Drupal Views, Thumbnails?
March 09, 2010, 03:37:53 pm
I've added the theme-cache clearing code to the top of the template.php file as you suggested. Tested and working. (I had read that saving the theme configuration will clear the theme cache, which was the sole purpose for my bothering with that. fwiw, it seems to work.)

I've also added the event title field with its corresponding label to the view block. The title field is populated, and both the field and label are displaying properly. I hope this is what you meant.

Pages: [1] 2 3 ... 7
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • Custom data image, Drupal Views, Thumbnails?

This forum was archived on 2017-11-26.