I mean define a view to show some fields but, in case one of them (not mandatory) is empty, then show a different field?

And any tweaks instead of doing it from the module panel?

Thanx in advance.

Comments

hip’s picture

Just bumping it up.

Spanish Drupal - www.4tres.com : Desarrollo de sitios, páginas web y aplicaciones para Internet eficaces y especializadas en Sevilla.

ransomweaver’s picture

Related: What if the field value is null? I would like to exclude it from the list. I would have thought I could add a filter to exclude it from the view, but since I have chosen this field to display in the view, it has disappeared from the list of available filter fields. Why is this? Anyone suggest a workaround?

hip’s picture

Well, I'm not sure wether this will be the same case, though a similar solution might work for both.

In my case if 'field A' is empty then in the same column (table view), should appear 'field B'

Spanish Drupal - www.4tres.com : Desarrollo de sitios, páginas web y aplicaciones para Internet eficaces y especializadas en Sevilla.

hip’s picture

Bumping it up again. Let's give it one more try.

NukeHavoc’s picture

I'm looking into this as well. I have a situation where I'm creating a list of web links. If a proxied web link is available, I want to use that, if it's not, then I want to use the unproxied web link, but I can't see an easy way to implement such conditional logic through straight-up Views.

mpromber’s picture

I'm looking for exactly the same thing and bumped into this thread.

I have a (grid block) view where long node titles look bad. So I've defined an optional field for that content type called "short title". I want my view to display the short title field if it is not empty, else the node title.

Any pointers?

justageek’s picture

You are going to have to override the view's template at some level to add custom "if this is empty then show this" logic, you can find out the info from click the "Theme: Information" link for the view you are editing. This can get tricky as it depends on the 'Style' of the view (table, grid, etc) and possible the associated row style of the view.

joachim’s picture

I've done it this way with views theming.
It's a bit tricky but doable.

mpromber’s picture

Solved, FYI:

Note that I am a Drupal and PHP newbie, so use this at your own risk. Works for me.

Here is my example:

  • For content type "story", I have a field "Short title" (field_short_title).
  • This field is not required.
  • In my view, I want to show the short title if the user has filled in that field, else I want to show the regular node title.
  1. Install the computed field module.
  2. In your content type, add a new field. For type, you will be able to pick "computed field". Let's call it "field_computed_short_title"
  3. Edit that new computed field. In "Computed code", I put without the surrounding "<?php ... ?>"
    if (!$node->nid) {
    node_save($node);
    }
    if (!$node->field_short_title['0']['value']) {
    $node_field[0]['value'] = check_plain($node->title);
    }
    else {
    $node_field[0]['value'] = check_plain($node->field_short_title[0]['value']);
    }
    

    The first if-statement is needed because in a newly edited node, the node title is empty and would return null.

  4. Leave "Display Format" at its default. Tick "Display this field". Choose data type "varchar" and data length "100" (as you wish). Save field.
  5. Now use this field_computed_short_title in your view.

Notes:
The computed field will have no value for existing nodes, so nothing will show up in your view. You will need to update those (edit and save). Does anyone have a good way to update multiple nodes?

justageek’s picture

without doing any research, looks interesting, although the explicit call to node_save() looks a little scary to me. When is that executed? Has the edit / add node form been validated before your node_save() is called?

mpromber’s picture

Honestly, I don't know this. I picked this up from the code snippet collection, specifically this one: http://drupal.org/node/149232

Would be interesting if a more expert user happens by who could clarify.

WorldFallz’s picture

I'm no expert, but what about something like this:

<?php
if (isset($node->field_short_title[0]['value'])) {
   $node_field[0]['value'] = check_plain($node->field_short_title[0]['value']);
} else {
  $node_field[0]['value'] = check_plain($node->title);
}
?>

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

Axel Pressbutton’s picture

Hi,

I think I have a similar issue but don't know the best solution.

I have a CCK integer field that is a simple set of No/Yes radio buttons that only an administrator can see on a particular content type (this is so we can flag it as having a product/file download attached - don't ask, long story). Values are 0|No and 1|Yes....I'd like to be able to display a link/button on my view results if the value is set to 1|Yes.

I get the impression there could be two ways to do this...one is to follow the above method (but not sure how) and add a Computed CCK field that says....if field X is set to yes then write the link/button code and then I can use this in the view.

The other method I've seen mention of is to possibly edit the views-view-fields.tpl.php template....or at least that's how I understood it to be.

I guess my question would then be, is it possible to do and if so, which would people recommend as the better option? I thought that maybe the computed field option would be safer with regards to module updates, would this be the case?

Many thanks for any help as it's always greatly appreciated

WorldFallz’s picture

I usually do this type of thing with computed fields-- very flexible method.

Axel Pressbutton’s picture

Sort of suspected you'd say that WorldFallz after your previous help that gave me my first insight into computed fields ;)

I've managed to get it to do what I want. But not being too hot at this could you/anyone just confirm for me that the following is OK;

if (($node->field_plan_attached[0]['value'])==1) {
   $node_field[0]['value'] = l('Download available', 'node/' . $node->nid);
} else {
  $node_field[0]['value'] ="";
}

I love the way that it'll contstruct the url in it's aliased format, awesome stuff.

Thanks

WorldFallz’s picture

Yep--- looks good. computed_fields are extremely useful for all sorts of things. The views counterpart is http://drupal.org/project/views_customfield.

drupal is awesome-- but then I'm biased. ;-)

justageek’s picture

You are extremely intelligent :)

(You've answered more questions for me, mostly not something I even posted, hope someone pays you to do Drupal stuff so you stick around!)

WorldFallz’s picture

lol, thanks. drupal is part of my day job, but even if it wasnt i'd still be hanging around- i'm just that geeky, lol.

ramones79’s picture

As Joachim has mentioned - it can be done via Views templating and then just PHP to check the desired conditions.

I just did a simple thing - to show a "promotion" next to a product, if there is a promotion set for that product.

Here's my approach to it:

Created a CCK radio button with only 1 option available "promotion"

The CCK automatically sets another option "N/A" - which, if selected returns empty (in PHPTemplating).

Then in my View and the Display for these products I added the Field (in Fields) for Promotion (name of the field in my case: field_promo_yesno), Label set to None.

Then click on Theme: Information link

Here I see the name of the field and the possible template file names. In my case I selected views-view-field--field-promo-yesno-value.tpl.php

And here is the content of this file :

<?php if ($fields['promo_yesno']->content !== ""): ?>
<?php print '<span style="color: red">'?>
<?php print $output; ?>
<?php print '</span>' ?>
<?php endif; ?>

Saved the file, uploaded it to my Drupal current template folder.

After that I click on the "Rescan Template Files" button (it's on the same page, where you go after clicking the "Theme: Information" link)

If my new mini-views-template is successfully found by Views - the bolded file name for currently used template for my desired field will change from views-view-field.tpl.php to views-view-field--field-promo-yesno-value.tpl.php

After that I enabled/disabled the value for some product and tested it on the Views generated page (my products catalogue) - everything is OK and working :)

jimmi61’s picture

No way for me to make it working :( I created a computed fields like this:

 if (!$node->nid) node_save($node);
if (!$node->field_prezzo['0']['value']) {
$node_field['0']['value'] = check_plain($node->field_prezzocopertina['0']['value']);
}
else {
$node_field['0']['value'] = check_plain($node->field_prezzo['0']['value']);
} 

Included it in a View but the result is always null. I also included a default value in the Database Storage Settings and the result is the same.
A few questions:
- There could be any issue related to Data Type of stored data and Format of View field?
- Should the Format of View field be Computed Value or Text?
- Is the instruction if (!$node->nid) node_save($node); really necessary in this case or not?
- How can be possible that even setting a default value in the Computed field nothing appears in the View field?

Thanks in advance :)

Cablestein’s picture

FYI the code for all this is slightly different in the D7 version of Computed Fields.

baxterjones’s picture

I have a site setup (d6.17, cck, views, conditional fields, views attach - plenty of other modules) (system setup: linux OS, apache 5.2.x, plenty of php_memory, mysql)
The owner wants to allow for users to add a taxonomy term, without 'tagging' (this almost always results in content forking).
So my solution is that the content type has a conditional field which is ticked yes/no box (other*) when ticked (*yes) then there is a webform displayed with a submission for a new taxonomy term. I was thinking of embedding the webform with views attach to the certain content types (*edit page: build modes).

Then in theory the user will be able to submit a new taxonomy term if the relevant one is not available.

Can anyone advise on a better solution?

queryblitz’s picture

Views If Then module or something. This: http://drupal.org/node/945268 looks like the same issue, as does this: http://drupal.org/node/869286. I'm not adept enough yet to build a module, but it seems like there's enough demand for sure.

Druper’s picture

I understand that field output in Views 2 cannot be managed conditionally in the UI. Can it in Views 3 (D7)? So far I’ve hit a wall, suggesting it cannot.

I’m building a D7 site for our German Shepherd rescue group and need to solve the problem of presenting fields conditionally within views. Here are the bones of the scenario:

Query pulls all unadopted dogs from DB to display in a mini-pane. Conditional fields needed:

  • If ‘date_added’ field is less than 21 days ago, display “new” icon or text.
  • If ‘living_with’ field is not Foster OR Permanent Foster OR Family OR Shelter, display “Foster Needed” icon or text.
  • Both fields are required in CCK, so testing for NULL or replacing a NULL is not an issue

Managing both of these manually is possible but would offend the DB gods.

My thought was to do another query to identify the new dogs, for example, and join that with the unadopted dogs query to add the necessary ‘new’ field. Same with ‘Foster Needed’. I guess that’s not what Views Relationships is for, huh. Is there some way to accomplish this that I’m not seeing?

In searching I’ve come across a PHP solutions outlined in this thread and the CSS solution in jakepyne’s tutorial. I have even considered the terrifying power (power for a noob like me to screw up) of Computed Field and Views PHP. I guess what I’m hoping for is the moron solution as, until I started with Drupal, I thought PHP was that new drug the kids were on and CSS was the acronym for and embarrassing skin condition.

Any help with a simple solution to what seems to be a common problem would be greatly appreciated. I am hoping that D7 offers some functionality in this area that I simply haven’t stumbled upon.

Cablestein’s picture

Just a run down of the solutions...

-Computed Fields solution posted by mpromber seems like a winner, I'm gonna try it. My only gripe is that I'd rather keep all logic stuff within Views, and just leave Content Types alone.

-Compound Fields (not 'Computed') for Views can do the same thing, without PHP coding, but the module is only available for Drupal 6. I used it once to give importance to a manual thumbnail for image nodes, worked like a charm. No programming required.

-Custom Field for Views is useful too but all it will give you is a spot to do PHP code as a Views field. You'll still have to do PHP code to do something, although it would be similar to above.

-People mentioned doing PHP in Views theme files, that can work too, but again you have to code and plus it just adds to your files in the theme folder. Personally I try to avoid as many files as possible in the themes folder.

zilverdistel’s picture

Don't forget to mention

- Views If Empty

You need Views If Empty when ...

You have a content type with an embedded video and an imagefield thumbnail. The nodes always have the images but occasionally have a video. You want your view to display the image field when the video field is empty, display the video field when available, and never display both.

I needed to display a field depending on the value of the checkbox field (boolean). To get it working properly I had to specify a value for the "on" state of the checkbox in my field settings (I used "yes").

Furthermore, it's pretty easy to fork this module to make it do all other kinds of conditional rendering.

For example, I didn't want the "empty field" shown at all. Unaltered, the module shows the "outputfield" when the "emptyfield" is empty, otherwise it shows the emptyfield. But it always shows one of both. I only wanted the emptyfield to control the visibility of the outputfield. So, I altered the code in views_ifempty_handler_field.inc (render() method) as follows ...

        if (empty($fields[$emptyfield]->last_render)) {
          $this->last_render = $fields[$outputfield]->last_render;
        }
        else {
          $this->last_render = $fields[$emptyfield]->last_render;
        }

to

        if (empty($fields[$emptyfield]->last_render)) {
          $this->last_render = $fields[$outputfield]->last_render;
        }
        else {
          $this->last_render = '';
        }

I hope this helps ...

guykit’s picture

I used this which is great except that it gives me an error for the first entry in my view and doesn't show the conditional field. The error is

Notice: Undefined property: views_handler_field_field::$last_render in views_ifempty_handler_field->render() (line 114 of /home/smarkit3/public_html/www.intnews.com/TTIBuyersGuide/sites/all/modules/views_ifempty/includes/...).

Which I presume is related to the code alteration. Any ideas for stopping this? I'm using D7.

Thanks

capellic’s picture

p55mac’s picture

Using this solution https://drupal.org/node/1935092 helped me.

You can make conditional fields in views without using additional modules like Views PHP.

In this example, we make the path for a node title automatically link to the node path, to a URL link or to a file path, depending on the content type. Note that this is just an illustration of a concept which can be changed depending on the requirements.

illutek’s picture

A table display of products (Ubercart) the 'Add to Cart' only show if the value of the 'In Stock' field is greather than 0, wonderful.

parsicoola’s picture

If there are two fields that you only want one of them to show => add both fields to views, and then exclude second one from display (check "Exclude from display"). Now in first field go to "No results behavior" setting and enter the "Replacement pattern" of second link. (Just remember that second field should be upper that first one in list in order to see it as Replacement)
Now every thing's OK. Whenever first link is null the second appears instead...