Can I make fields visible only if image attach has an image - drupal

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.

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.

Related

Display page view inside page.tpl.php drupal 7

I have a file called page--advertsindex.tpl.php.. Now what i want to do is display that page inside the content region of page.tpl.php..
page--advertsindex.tpl.php contains the HTML which need's to be rendered in the content region of page.tpl but what it does currently when i navigate to my_drupal_site/advertsindex it only show's the html of page--advertsindex, not the page.tpl.php file and the page--advertsindex in the content region..
How can i do this? or is there a different way to achieve my goal?
Any page--<identifier>.tpl.php will be used instead of page.tpl.php for the url /identifier. That is by design and the entire purpose of this system. This can be extended for deeper paths too: page--foo-bar-baz.tpl.php for /foo/bar/baz.
If you want certain content to appear on /identifier, you have two options: Conditionally include the content or copy the content. What to choose depends on your situation.
Conditionally include: inside page.tpl.php:
<div class="footer">
<?php if (arg(0) == "advertsindex"): ?>
<?php print theme("advertsindex_disclaimer"); ?>
<?php endif; ?>
</div>
Overrides: introduce a page--advertsindex.tpl.php:
<div id="content">
<?php print $content ?>
</div>
When to choose what:
Only use conditional includes or conditional rendering when:
The content to be included or excluded is small and shared. Patterns like `[large 20+ line of frontpage HTML][normal HTML] are very bad.
The content to be included needs all sorts of fancy new variables pulled in. Patterns like are really bad. Drupals theme system is push (theme gets variables from modules) and never pull (theme requests parameters at modules).
In all other cases you'd use the overrides. And yes, that will include a lot of duplication, but that is by design and accepted as the default practice in Drupal. To avoid having to modify some HTML in 20+ files, "split" or "copy" them as late as possible.
A rule of thumb is that a conditional should be a few lines only and never require variables that are not available in the parent.
If advertsindex is would be one of your content type then you have to create node--advertsindex.tpl.php file instead of page--advertsindex.tpl.php. Or If you wont to display some content in the page.tpl.php page, then it is better to copy all code of page.tpl.php into YOUR-Custom_page.tpl.php and put your code inside the content section of YOUR-Custom_page.tpl.php.
I think it helps.

Drupal 7 manual access to FiveStar module

am trying to override the output of my page so I can add some PHP code, but the FiveStar module only give 2 options which is below or above Content. this is causing me problems as I'd like to include it in my HIDE technique, then I can choose where to place it.
e.g. (I hide all my output before rendering Content, then later I can render each as I choose, I cannot find the render for FiveStar Widget)
hide($content['comments']);
hide($content['links']);
hide($content['field_location']);
hide($content['body']);
hide($content['field_filename']);
hide($content['fivestar_widget']);
print render($content);
If any one can fill in the blanks for me would be awesome!
t.
If you place the widget after <?php print render($content) ?> you will have to use <?php hide($content['fivestar_widget_vote']); ?> before $content is rendered. Otherwise just use <?php print render($content['fivestar_widget_vote']); ?>. If it is before then the printed property will be set to true and it will only be rendered once.

Drupal Views 2: Output using custom markup

I have a simple view that grabs 4 fields, basically it grabs the fields of a specific content type. The fields are as follows:
CSS class (plain text)
Image (image)
Title
Body
Pretty simple stuff. I've got the view created, but I need to output things in a specialized way and I can't determine how this stuff breaks down in conjunction with my custom markup from my build. I need to wrap each row in a container and each row breaks down into it's own containers, take a look at the following code.
<div id="homepage-folio-portlets">
<div class="homepage-folio-portlet [CSS class]">
<div class="homepage-folio-portlet-image"><img src="[Image]" width="450" height="330" alt="" class="[CSS class]-image" /></div>
<div class="homepage-folio-portlet-text">
<strong>[Title]</strong>
<p>[Body]</p>
</div>
</div> <!-- /homepage-folio-portlet -->
</div> <!-- /homepage-folio-portlets -->
So I've got a container, homepage-folio-portlets, and inside of that I want to iterate over views creating a new container using the class homepage-folio-portlet for each row returned including the CSS class from the row.
My biggest hurdle is figuring out how to build either my .tpl files or my theme functions in template.php. I understand the naming conventions, but once inside I don't really know what to do. I have a feeling I'll need to do a little magic in template.php either way to make sure that my row output is aware of the CSS class from the content, but who knows. Any help and direction is appreciated.
After watching the aforementioned video it became a little more clear how to accomplish what I set out to do. The biggest "ah-ha" was that the default code for the "Row style output" template was confusing to me because of the foreach loop. I didn't recognize that I could simply output each field in whatever way I see fit in this file without the loop. The video showed how you could reference your fields individually with the following shorthand $fields['ID-of-field']->content. To get the 'ID-of-field' it's as scrolling past the "Display output", "Style output", and "Row style output" links in the "Theming information" option pane of your specific view.
I used the "Theme information" found in the edit screen of my view to determine the most specific .tpl for "Row style output" to create and created it, in this case view-view-fields--my-view-name--default.tpl.php.
view-view-fields--my-view-name--default.tpl.php - Row output .tpl file
(No longer making use of the default foreach because instead of looping over the fields I know the fields I want and I can simply output them anyway I see fit)
<div id="homepage-folio-portlets">
<div class="homepage-folio-portlet <?php print $fields['CSS_class']->content ?>">
<div class="homepage-folio-portlet-image"><img src="<?php print $fields['Image']->content ?>" width="450" height="330" alt="" class="<?php print $fields['CSS_class']->content ?>-image" /></div>
<div class="homepage-folio-portlet-text">
<strong><?php print $fields['Title']->content ?></strong>
<p><?php print $fields['Body']->content ?></p>
</div>
</div> <!-- /homepage-folio-portlet -->
</div> <!-- /homepage-folio-portlets -->
After that, I did a little recursion into the "Style output" and "Display output" .tpl files to get rid of all that extra markup Drupal adds. Note that all I really cared about was printing out $row (with it's foreach loop) in style .tpl and $rows in the display tpl. It's outputting EXACTLY what I want and I couldn't be happier. Finally, it's making some sense. Hopefully this helps a bunch of other people.
Just for reference...
views-view-unformatted--my-view-name--default.tpl.php - Style .tpl file
(Want to keep the foreach loop in here so each row gets outputted)
<?php foreach ($rows as $id => $row): ?>
<?php print $row; ?>
<?php endforeach; ?>
views-view--my-view-name--default.tpl.php - Display .tpl file
<?php print $rows; ?>
By removing all the extra markup I am losing important stuff specific to views like admin links and such, but for my purposes that's fine.
On the Edit tab for your view, under Basic Settings, look for "Theme:" and click on the "Information" link. Then in the "Default: Theming information" section, the bold filenames are the ones currently being used to theme a particular sub-section of that view. The other names are "suggestions" that can be used to override the defaults and they are ordered from least specific to most specific.
In your case, to start, it sounds like you want to override the "Row style output":
Click on the "Row style output" link, copy the default template code.
Choose one of the suggested filenames to use for the row style, based on whether you want this style to be used for all views, this view, a particular display of this view, etc.
Paste the code copied in Step #1 into the filename chosen in Step #2
Edit the code as necessary, to add the specific classes
Click on the "Rescan template files" to rebuild the template cache
Repeat steps 1-5 for any additional sub-templates you want to customize.
Have you tried using the template files for rows? You should see it in the views module (in the admin). By clicking on "Information" in the lower box on the left side when you are creating the view. You will need to refresh the template cache (you will see a button to do this).

Help with Drupal Search Form Display

When displaying the search form in my header, what is the preferred method to use if it cannot fit into my header region??
should I...
create a custom region?
use some kind of 'print $search_form'
drupal_get_form()??
use the theme() function??
please help! I'm new to Drupal and trying to figure out the best 'Drupal Way' of doing things.
Depends on how you want to define or maintain the search form. If you think of it as part of the theme (that is, it's as static as the page background or colors), consider just using the following in page.tpl.php:
<?php if ($search_box): ?>
<?php print $search_box ?>
<?php endif; ?>
This is de-facto the Drupal Way: if your theme didn't modify the page variables, you get $search_box for free in page.tpl.php. Adding the conditional also lets site maintainers turn it off in the theme settings and specify permissions for it.
If you want to give site maintainers the ability to move it around from region to region, consider using the Search block. This way it can be utilized just as any other block on your site. This would also be considered Drupal Way-ish: you get the block for free if you enable the Search module.
If you want theme the forms, override search-theme-form.tpl.php if you use the first method, and search-box-form.tpl.php if you used the second. Both templates can be found in modules/search.
You could do any of those. You could create a custom region, and use CSS to change the display. You could alter the form with preprocessing hooks or hook_form_alter.
Depends on whatever is easiest for you.

Drupal 6: print all body field content to node template file

I tried to display body content with:
<?php print $node->content['body']['#value']; ?>
However, it doesn't display all body content, it just display first paragraph of body content, sometimes 2 paragraph if it is short :/
I need to print all body. how can I do that?
Thanks a lot! Appreciate helps!
The shortened body content hints on it being filled/rendered for 'teaser' view instead of 'full'. In what context do you issue this print statement?
EDIT: The node templates are usually used for both, teaser and full output, but the decision on what to use, as well as the population of the content entries in the node object happen outside of the node template files. Within the node template file, the variable $teaser will be TRUE, if the node is to be shown as a teaser.
So you need to check in what context your node template gets called, as you'll have to configure that context to render the node as 'full'. This could be in many places, depending on who is responsible to provide the nodes you want to theme, e.g. if the node template gets called from a view, you'll need to configure the view to use 'full page' output, if it comes from a module, you'll need to check with the module settings, etc...
In node.tpl.php try
<?php print $content ?>
However,
<?php print $node->content['body']['#value']; ?>
works for me as well.
To get control over your teaser length the master value is set with Post Settings.
(Length of trimmed posts)
To control this by node type try: http://drupal.org/project/teaserbytype
NOTE: Teasers are cached so you'll need to http://drupal.org/project/retease
However, if you want to just get it done in the node template you could run a node_load() and have everything... but that's not the best practice.
FYI: you can control what CCK fields show up in $content under Display Options.
PS: In teaser mode I often make use of truncate_utf8().

Resources