Page 1 of 1

[SOLVED] News Image Issue when No Image Selected In Summary

Posted: Thu Dec 11, 2014 12:57 pm
by govicinity
CMS Made Simple™ 1.11.11 “San Cristobal”
News 2.14.4
CGSmartImage 1.17.1
CGSimpleSmarty 1.7.4
SiteMapMadeSimple 1.2.8
ListIt2 1.4.1
FormBuilder 0.8

I'm having an issue with images in the summary view of the news module, basically image placeholders, or the alt text is appearing when no image is uploaded/selected in the news module on an article. I am hoping the wider CMSMS community have a simple fix to this relatively simple problem I am having trouble with for some reason at the moment. If I have no image selected on any articles then the placeholders/alt text disappear, but it I have just one image selected on one of the articles then the placeholders/alt text appear on every other article!

So the else text... in this instance for now "Text alternative or nothing here if no image chosen" doesn't show on any of the articles that do not have an image attached to them. Basically for the else I don't want anything at all, no image or no text.

Code: Select all

<!-- Start News Display Template -->
{if $pagecount > 1}
<p>
{if $pagenumber > 1}
{$firstpage}&nbsp;{$prevpage}&nbsp;
{/if}
{$pagetext}&nbsp;{$pagenumber}&nbsp;{$oftext}&nbsp;{$pagecount}
{if $pagenumber < $pagecount}
&nbsp;{$nextpage}&nbsp;{$lastpage}
{/if}
</p>
{/if}
{foreach from=$items item=entry}
<div class="News_Page">

{cgsi_convert filter_croptofit='170,170,c,1' quality='100'}
{if isset($entry->fields)}
  {foreach from=$entry->fields item='field'}
     <div class="NewsSummaryField">
        {if $field->type == 'file'}
          <a href="{$entry->moreurl}" title="{$entry->title|cms_escape:htmlall}"><img src="{$entry->file_location}/{$field->value}" alt="News image" /></a>
        {else}
          Text alternative or nothing here if no image chosen
        {/if}
     </div>
  {/foreach}
{/if}
{/cgsi_convert}

<div class="NewsSummary">

{if $entry->postdate}
    <div class="NewsSummaryPostdate">
		<p>{$entry->postdate|cms_date_format}</p>
	</div>
{/if}

<div class="NewsSummaryLink">
<h2><a href="{$entry->moreurl}" title="{$entry->title|cms_escape:htmlall}">{$entry->title|cms_escape}</a></h2>
</div>

{if $entry->summary}
	<div class="NewsSummarySummary">
		<p>{eval var=$entry->summary|strip_tags|summarize:20:"..."}</p>
	</div>

	<div class="NewsSummaryMorelink">
		<p>{$entry->morelink}...</p>
	</div>

{else if $entry->content}

	<div class="NewsSummaryContent">
		<p>{eval var=$entry->content|strip_tags|summarize:20:"…"}</p>
	</div>
{/if}

{if isset($entry->extra)}
    <div class="NewsSummaryExtra">
        {eval var=$entry->extra}
	{* {cms_module module='Uploads' mode='simpleurl' upload_id=$entry->extravalue} *}
    </div>
{/if}
</div>

</div>
{/foreach}
<!-- End News Display Template -->

Re: News Image Issue when No Image Selected/Uploaded In Summ

Posted: Thu Dec 11, 2014 1:19 pm
by velden
I think it's not good practice to stick to the foreach loop that comes with the default template.

As you know the fields you create you don't need to loop and do (a lot of) checks on them.

And I think that cgsi_convert is expensive too compared to a cgsmartimage call.

Then, let's see what you're doing:

Code: Select all


{cgsi_convert filter_croptofit='170,170,c,1' quality='100'}

So processing whole output between these tags to search for images while you already know where the image is used. 

{if isset($entry->fields)}
  You know you've defined fields

  {foreach from=$entry->fields item='field'}
     Now you're going to interate those fields. That's handy for a default/sample template as the developer doesn't know if and which fields you're going to add. But you do.

     <div class="NewsSummaryField">
        {if $field->type == 'file'}

            Ok, so if  type is file (image in your case)
          <a href="{$entry->moreurl}" title="{$entry->title|cms_escape:htmlall}"><img src="{$entry->file_location}/{$field->value}" alt="News image" /></a>
        
        {else}
          Here we go. So if type is NOT file. So any other added field is processed here.

          Text alternative or nothing here if no image chosen
        {/if}
     </div>
  {/foreach}
{/if}
{/cgsi_convert}
My suggestion (for the image field):

Code: Select all

{if $entry->fields.FIELDALIAS->displayvalue}
  {CGSmartImage src1=$entry->file_location src2=$entry->fields.FIELDALIAS->displayvalue filter_croptofit='170,170,c,1' quality='100' noembed=1 alt=""}{/if}
where FIELDALIAS of course is the alias of the added image field.
Other fields can be processed that way too. Exact where you want and how.

Re: News Image Issue when No Image Selected/Uploaded In Summ

Posted: Thu Dec 11, 2014 3:14 pm
by govicinity
Hi Velden, that is fantastic thanks, cheers for the heads up on this, been scratching my head for ages! Works a treat - I knew one of you guys (or girls) would know the answer.

Here's my code for anyone else who needs help or gets stuck on this.

Code: Select all

<!-- Start News Display Template -->
{if $pagecount > 1}
<p>
{if $pagenumber > 1}
{$firstpage}&nbsp;{$prevpage}&nbsp;
{/if}
{$pagetext}&nbsp;{$pagenumber}&nbsp;{$oftext}&nbsp;{$pagecount}
{if $pagenumber < $pagecount}
&nbsp;{$nextpage}&nbsp;{$lastpage}
{/if}
</p>
{/if}
{foreach from=$items item=entry}
<div class="News_Page">

<!-- Here's the bit I've changed from original forum post code with help from Velden -->

{if $entry->fields.News_Image->displayvalue}
<div class="NewsSummaryField">
  {CGSmartImage src1=$entry->file_location src2=$entry->fields.News_Image->displayvalue filter_croptofit='170,170,c,1' quality='100' noembed=1 alt=""}
</div>
{/if}

<!-- End of the changed code here -->

<div class="NewsSummary">

{if $entry->postdate}
    <div class="NewsSummaryPostdate">
		<p>{$entry->postdate|cms_date_format}</p>
	</div>
{/if}

<div class="NewsSummaryLink">
<h2><a href="{$entry->moreurl}" title="{$entry->title|cms_escape:htmlall}">{$entry->title|cms_escape}</a></h2>
</div>

{if $entry->summary}
	<div class="NewsSummarySummary">
		<p>{eval var=$entry->summary|strip_tags|summarize:20:"..."}</p>
	</div>

	<div class="NewsSummaryMorelink">
		<p>{$entry->morelink}...</p>
	</div>

{else if $entry->content}

	<div class="NewsSummaryContent">
		<p>{eval var=$entry->content|strip_tags|summarize:20:"…"}</p>
	</div>
{/if}

{if isset($entry->extra)}
    <div class="NewsSummaryExtra">
        {eval var=$entry->extra}
	{* {cms_module module='Uploads' mode='simpleurl' upload_id=$entry->extravalue} *}
    </div>
{/if}
</div>

</div>
{/foreach}
<!-- End News Display Template -->
Always nice to learn something new! I can put this into practice now on lots of sites we run!