views:

13

answers:

1

Is it possible to use CCK to add a conditional to the image attach module form where unless I have selected an image to use for a content node, certain fields are not visible?

Currently I do not have any operations available for my image attach field in my content type definition where configure and remove are available for all other fields.

+1  A: 

This would be really simple in your theme, e.g. node-foo.tpl.php for the content-type foo, that has a field "video"

<?php if(!empty($field_video[0]['view'])): ?>
  <div class="block video">
     <?php print $field_video[0]['view'] ?>
  </div>
     <?php print $field_some_other[0]['view'] ?>
     <?php print $field_the_other_one[0]['view'] ?>
<?php endif; ?>

Some notes on style and best practices:

I prefer the if/endif in templates, others prefer the if() {}. Technically little difference, I think elseif; is more readable in HTML.

Technically it is not correct to simply print a value, but one should use drupal_render(). I personally still prefer print, because of its transparancy and simplicity. Drupal_render(), however, registers what it has "rendered" and allows you to drupal_render($node) at the end, to render all unrendered fields; very usefull if you decide to add fields later on, whithout having to change the entire template every time you do so. Drupal_render is not available in the tpl.php, but in the preprocessing: as sayd, a lot less transparent and slightly more complex.

Dont! Ever! print the $field_foo[0]['value'], always the ['view'] part: the first is unescaped and may (will!) contain XSS injections and the likes.

The strange nested array ($field_foo[0]['value']) is a result of the multiple-fields option in Drupal. A better way would be to always iterate over each field and never just render, hardcoded, the first ([0]) item. However, for reasons of readability, simplicity and transparancy, I prefer to hardcode the indexes in my template. Others (rightfully) disagree with me on this.

berkes
IMHO, there's nothing wrong with hardcoding the first ([0]) item as long as you're reasonably sure that the field will be single-value in the foreseeable future. Wrapping it in a foreach would be premature optimization (which is of course the root of all evil :-)).
marcvangend
"as long as you can be reasonable sure that" is fine indeed. However, your site should not break (as in: trow warnings, errors, open up sec.holes) either :)
berkes