Filter a view on the existence of an image for an imagefield field
No this won't work, I do not think filtering per-result row can be done through arguments:
<?php
if (!$field_user_picture) {
$view->filter[0]['value'] = TRUE;
}
return $args; // not that this will do anything!
?>
Begin the journey in #drupal-support
ben-agaric:
how can one filter a view based on the value (given or empty) of a CCK field -- CCK image in this case -- ?
Escalating directly to #drupal-views:
ben-agaric: I would like to filter a view conditional on the existence (value present or not) of a CCK image field.
ben-agaric: It looks like I will have to write a views filter hook? I don't see any way to do this with arguments.[11:02am] • merlinofchaos has no idea what imagefield provides for args or filters.
[11:04am] ben-agaric: it provides an argument, with no operators, and does not provide a filter
[11:05am] ben-agaric: so to filter using it I will have to make it provide a filter, correct?[11:07am] merlinofchaos: Most likely.
[11:09am] ben-agaric: onward.[11:44am] ben-agaric: Well that was too easy, the filter was already added in the head code for imagefield. Which overall doesn't work but the filter does great. I think i have Dopry to thank. Thanks!
[11:48am] merlinofchaos: Oh nice
In most views you want to get rid of non-existent images, but for displaying on nodes you may want to replace with a default. This seems a little too complicated (to do less) but I think Rick Hood provides a good base here, http://drupal.org/node/166603
In your node-whatever.tpl.php add an else, and you can stick in any image you want:
<?php
$primaryimage = $node -> content['field_image_primary']['#value'];
$primaryimagexists = $node -> field_image_primary[0]['fid'];
if ($primaryimagexists != 0) {
print "<div class='primary-image'>" . $primaryimage . "</div>" ;
}
?>
Resolution
The fix was in the HEAD code, which must be built on the version 2 series of the imagefield module.
Agaric's modifications to the latest version 1 series, release candidate 1 version of the module (version = "5.x-1.2-rc1"), taken directly from HEAD at the time, begin where marked:
<?php
/**
* Implementation of hook_field_settings().
*/
function imagefield_field_settings($op, $field) {
switch ($op) {
case 'form':
$form = array();
return $form;
case 'validate':
break;
case 'save':
return array();
case 'database columns':
$columns = array(
'fid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
'title' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
'alt' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
);
return $columns;
// ben-agaric added from head imagefield.module code
case 'filters':
return array(
'not null' => array(
'operator' => array('=' => t('Has Image')),
'list' => 'views_handler_operator_yesno',
'list-type' => 'select',
'handler' => 'imagefield_views_handler_filter_is_not_null',
),
);
}
}
/**
* Custom filter for imagefield NOT NULL
*/
function imagefield_views_handler_filter_is_not_null($op, $filter, $filterinfo, &$query) {
if ($op == 'handler') {
$query->ensure_table($filterinfo['table']);
if ($filter['value']) {
$qs = '%s.%s > 0';
}
else {
$qs = '%s.%s = 0 OR %s.%s IS NULL';
}
$query->add_where($qs, $filterinfo['table'], $filterinfo['field'], $filterinfo['table'], $filterinfo['field']);
}
}
// end ben-agaric added from head code
?>
Comments
I have a need for this
Hi Ben,
I have a need to filter a view that includes an Imagefield based on whether the image exists or not.
I am using Imagefield 5.x-1.x-dev ($Id: imagefield.module,v 1.30.2.2 2007/01/25 01:55:12 dopry Exp $)
That actually has a field for default image, but it doesn't seem to work.
So I tried your solution above, pasting in code so that it looks like below. But it doesn't seem to do anything.
Perhaps it won't work with this version of Imagefield I am using? Or maybe I am missing something else?
Thanks,
Rick
/**
* Implementation of hook_field_settings().
*/
function imagefield_field_settings($op, $field) {
switch ($op) {
case 'form':
$form = array();
return $form;
case 'validate':
break;
case 'save':
return array();
case 'database columns':
$columns = array(
'fid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
'title' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
'alt' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
);
return $columns;
case 'filters':
return array(
'not null' => array(
'operator' => array('=' => t('Has Image')),
'list' => 'views_handler_operator_yesno',
'list-type' => 'select',
'handler' => 'imagefield_views_handler_filter_is_not_null',
),
);
}
}
/**
* Custom filter for imagefield NOT NULL
*/
function imagefield_views_handler_filter_is_not_null($op, $filter, $filterinfo, &$query) {
if ($op == 'handler') {
$query->ensure_table($filterinfo['table']);
if ($filter['value']) {
$qs = '%s.%s > 0';
}
else {
$qs = '%s.%s = 0 OR %s.%s IS NULL';
}
$query->add_where($qs, $filterinfo['table'], $filterinfo['field'], $filterinfo['table'], $filterinfo['field']);
}
}
Ooops
Ah I just realized that what the above does is cause a filter(s) to show up on the view.
OK, that's cool.
My situation is that I have nodes in my list view with three possible fields: Image1, Image2 and Body. I don't want Image1 and Image 2 to show if they don't exist (to keep those missing-images icons away in IE). If I use above filter, nodes will get booted form the view if either of them don't exist, which is no good for my case.
I could do all of this by themimg the view, but seems like a bater way to do it to filter it in the view instead.
Are they really showing up?
First, yes, what you want to do is not filter a view based on the existence of a field, but to control the output of a field of a view– which is easier, at least in the learning curve path Agaric has taken!
But... are missing images really being created? I don't think Views creates empty
img
tags for images that don't exist. Are you theming the view? (That's a trick question: if you are theming a view, that's where the problem is introduced; if you aren't theming the view, you can fix it there!)Get started on industrial strength theming or use only the Views Theme Wizard.
We have notes on how to theme views to use imagecache but I think it's not necessary with the current imagefield views integration.
In any case, once you have a custom list view template, you can definitely do an "if field exists, print field (else don't do anything)" statement in the
.tpl
.User Reference
I used this code to test if a User Reference CCK field is null or not, worked perfectly with a few renames.
Can't thank you enough for the tutorial.
Still not clear on this
I'm looking to add a filter to my D5 views that can filter out nodes that don't have a specific imagefield value filled in. Is that what this does?
I pasted the code in this blog post into my imagefield.module.php file, but didn't see any new filters being created. I'm using imagefield 5.x.2.something.
What am I doing wrong?
So, I'm trying to do what
So, I'm trying to do what you're doing:
Filter a View based on the existence of a certain Image field.
I read your post twice, and still have no idea how to do it.
Clarify? :D
I've been stumped on this forever.
Thanks man!
Post new comment