Conditional Statement In Visualforce? - if-statement

I'm trying to display records on a page (not a form) via VisualForce based on a checkbox value.
For example, if checkbox "Active_c" is checked, I would like to display five subsequent fields. If checkbox "Active_c" is not checked, I would like to display nothing.
The only example I can find are display text in the output, however, they don't support displaying multiple field output. For example:
{! IF ( CONTAINS('salesforce.com','force.com'), 'Yep', 'Nah') }
Anyone have experience with a conditional?

Apex Controller:
public YourObject__c YourObject { get; set; }
public YourClass(){
YourObject = [ Select Active__c, Field1__c, Field2__c From YourObject__c Limit 1 ];
}
Visualforce Page:
<apex:actionFunction name="showHideFields" reRender="myFields" />
<apex:inputField value="{!YourObject.Active_c}" onChange="showHideFields()"/>
<apex:outputPanel id="myFields">
<apex:outputPanel rendered="{!YourObject.Active_c}">
<apex:outputField value="YourObject.Field1__c" />
<apex:outputField value="YourObject.Field2__c" />
</apex:outputPanel>
</apex:outputPanel>
Another example. Without reRendering, just checking the value of the checkbox on the runtime:
<apex:outputPanel>
<apex:outputField value="YourObject.Field1__c" rendered="{!YourObject.Active_c}" />
<apex:outputField value="YourObject.Field2__c" rendered="{!YourObject.Active_c}" />
</apex:outputPanel>

Related

Salesforce lightning Input with list doesn't work

I'm trying use input with datalist in a lightning component and doesn't seem to work. I've looked around and can't seem to find anything that says i can't. So basically,
<input list="acctlist"/>
<datalist id="acctlist">
<option value="somevalue">
</datalist>
does not work. I want to have an input in a form that a user can type but also able to select from a list returned from the controller. Is there a workaround that would be as simple or is this the following route the best i got.
https://developer.salesforce.com/blogs/developer-relations/2015/06/salesforce-lightning-inputlookup-missing-component.html
The list attribute of input tag is not compatible with lightning component.
When you deploy the components, the attribute is removed.
If you want to use input with datalist, you need to add the attribute in Renderer.js.
datalist.cmp
<input aura:id="acctlistInput" />
<datalist id="acctlist">
<option value="somevalue" />
</datalist>
datalistRenderer.js
afterRender : function(component, helper) {
var acctlistInputCmp = component.find("acctlistInput");
var acctlistInput = acctlistInputCmp.getElement();
acctlistInput.setAttribute("list", "acctlist");
return this.superAfterRender();
}

Add enclosing tag for only UnOrdered list from Rich Text Editor in UL

I need to style UL's coming from Rich Text Editor in Sitecore. I am trying to find out if there is a class that I can add to all UL's coming from Sitecore's Rich Text Editor.
Thanks in Advance
Ashok
The easiest solution is just to wrap your FieldRenderer with an HTML element with appropriate class applied in code:
<div class="rich-text">
<sc:FieldRenderer ID="frRichTextField" runat="server" FieldName="MyFieldName" />
</div>
And then add in some CSS styles to handle your UL's within this:
.rich-text ul {
/* add in your styling */
}
You can also use the before and after properties of the FieldRenderer to pass in your tag:
<sc:FieldRenderer ID="frRichTextField" runat="server" FieldName="MyFieldName"
Before="<div class='rich-text'>" After="</div>" />
EDIT:
If you wanted to be more drastic then you could add in your own renderField pipeline processor to ensure your control is always wrapped with the required tag or you could make use of the enclosingTag property and patch the AddBeforeAndAfterValues pipeline instead:
namespace MyCustom.Pipelines.RenderField
{
public class AddBeforeAndAfterValues
{
public void Process(RenderFieldArgs args)
{
Assert.ArgumentNotNull((object)args, "args");
if (args.Before.Length > 0)
args.Result.FirstPart = args.Before + args.Result.FirstPart;
if (args.After.Length > 0)
{
RenderFieldResult result = args.Result;
string str = result.LastPart + args.After;
result.LastPart = str;
}
if (args.EnclosingTag.Length == 0 || args.Result.FirstPart.Length <= 0 && args.Result.LastPart.Length <= 0)
return;
// check if a css class paramter has been passed in
string cssClass = args.Parameters.ContainsKey("class") ? args.Parameters["class"] : String.Empty;
// add the class to the enclosing tag property
args.Result.FirstPart = StringExtensions.FormatWith("<{0} class='{1}'>{2}", (object)args.EnclosingTag, cssClass, (object)args.Result.FirstPart);
args.Result.LastPart = StringExtensions.FormatWith("{0}</{1}>", (object)args.Result.LastPart, (object)args.EnclosingTag);
}
}
}
Patch the Sitecore config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<pipelines>
<renderField>
<processor type="Sitecore.Pipelines.RenderField.GetLinkFieldValue, Sitecore.Kernel"
set:type="MyCustom.Pipelines.RenderField.AddBeforeAndAfterValues, MyCustom.Pipelines" />
</renderField>
</pipelines>
</sitecore>
</configuration>
And then call the FieldRenderer with the EnclosingTag set and pass in your class parameter:
<sc:FieldRenderer ID="frRichTextField" runat="server" FieldName="MyFieldName"
EnclosingTag="div" Parameters="class=rich-text" />
This really doesn't add much over using the before/after properties though and I would generally try to stay away from overwriting default Sitecore processors to save heartache when upgrading.
You could either tap into relevant pipelines or update your sublayouts so that you always have a fixed class around every instance of the rich text field rendering:
<div class="rtf">
<sc:Text ID="scContent" runat="server" FieldName="Content" />
</div>
You will have to make sure as a developer that all current and future instances of rich text field renderings are enclosed by a tag with this class.
You could then include in the global CSS, a common style for this class.
.rtf ul {
...
....
}
If you don't want to have to add this wrapper for every single rtf rendering, you could tap into a relevant pipeline. (Note - this might be a better approach with regard to code maintainability)
You could choose to use one of the two:
renderField pipeline
or the
saveRichTextContent pipeline
So you would add a new processor for either of these pipelines, in which you could access the text within rich text fields only and process that as you please (easier to manipulate the html using the html agility pack)
If you use renderField pipeline - the text within the rich text field in sitecore will not change, the code you write will execure only while rendering the field - in preview / page editor or normal mode.
Using saveRichTextContent pipeline on the other hand, will update the in the rich text field when the content author clicks on save (after entering the text) in content editor mode.
You can see the following examples for these:
renderField - http://techmusingz.wordpress.com/2014/05/25/unsupported-iframe-urls-in-sitecore-page-editor-mode/ (Sample of HtmlUtility is also present here - instead of selecting all tags, you could select all and add your desired class attribute.)
saveRichTextContent - http://techmusingz.wordpress.com/2014/06/14/wrapping-rich-text-value-in-paragraph-tag-in-sitecore/
Hope this helps.
Best practice would be to just add the Class to the Rich Text Editor for use in the Editor.
There are lots of good articles on doing this. Here are a couple:
http://sitecoreblog.blogspot.com/2013/11/add-css-class-to-richtext-editor.html
http://markstiles.net/Blog/2011/08/13/add-css-classes-to-sitecore-rich-text-editor.aspx
These are minimal changes that provide you with the ability to put the styling ability in the hands of the Content Author, but still controlling what styles and classes they can use inline.

How to retrieve Sitecore media items from media url?

How to retrieve Sitecore media item from the URL we have?
The URL is dynamic URL e.g. /~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx.
This is generated when you add item in rich text field.
You can use the code below:
DynamicLink dynamicLink;
if (!DynamicLink.TryParse("/~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx", out dynamicLink))
return;
MediaItem mediaItem = Sitecore.Context.Database.GetItem(dynamicLink.ItemId, dynamicLink.Language ?? Sitecore.Context.Language);
When adding an item in the Rich Text field, you can use the FieldRenderer to render out the output - Sitecore will then create the correct URL automatically. That way you won't even have to worry about the URL.
The FieldRenderer control can be used like so:
<sc:FieldRenderer ID="renderer" runat="server" FieldName="fieldname" />
Or if you're using XSLT:
<sc:text field="fieldname" />
In codebehind you could do something like
FieldRenderer.Render(Sitecore.Context.Item, fieldname);

.NET usercontrol code-behind not rendering inside XSLT Umbraco macro

I have a XSLT macro that renders some markup and .NET usercontrol macros in a loop. So thats multiple usercontrol macros inside a xslt macro:
<xsl:for-each select="$node/* [#isDoc]">
//Some markup etc...
<xsl:value-of select="umbraco.library:RenderMacroContent('<?UMBRACO_MACRO NodeId="#id" macroAlias="LogTag"></?UMBRACO_MACRO>', #id)" disable-output-escaping="yes"/>
</xsl:for-each>
The markup from the usercontrols markup is rendered just fine, but everything in the Page_load is not executed. Why is that?
Heres the usercontrol markup:
<div class="logTag" id="Tag" runat="server">
<img src="someimage.png" alt="image"/>
</div>
And code-behind:
public int NodeId { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)return;
var tools = new Tools();
var userId = (Int32)Membership.GetUser().ProviderUserKey;
Tag.Visible = tools.GetLog(NodeId, userId);
Response.Write("Node=" + NodeId + " - User=" + userId + "<br />");
}
Neither the Response.Write or Tag.Visible is working. I've tried testing the usercontrol macro directly on a blank - works fine! Is this not possible, or am I doing something wrong?
Unfortunately due to the nature of XSLT macros and where the render event is called in the page lifecycle means that the library.RenderMacroContent() method will not work inside XSLTs.
There is a workaround discussed on Hendy Racher's blog which is used to create "widgets" functionality, if this is what you are after. This utilises an ASP.NET repeater which injects the usercontrols at the correct phase of the page cycle to allow usercontrol macros to work. Otherwise, use the Macro Container datatype in Umbraco 4.7 and upwards, which allows drag-drop functionality of macros inside the Umbraco backoffice, and can be successfully rendered (usercontrols, Razor or XSLT) using an <umbraco:Item /> tag.

How to exclude fields form being indexed with Sitecore search (new method)

How can I specify which fields to index with lucene indexing with Sitecore (new method)?
For example I'd like to index only the fields 'title' and 'text'. There seems to be a IndexAllField parameter that can be set to False but how can I set which fields needs to be indexed?
I'm using Sitecore.Search.Crawlers.DatabaseCrawler.
Are you using the Advanced Database Crawler? If so, there are sections you can add to include specific fields by their GUIDs and exclude specific fields by their GUIDs. Below I've provided a snippet where the hint attribute of the <include> node defines whether the fields should be included or excluded
<master type="Sitecore.SharedSource.Search.Crawlers.AdvancedDatabaseCrawler,Sitecore.SharedSource.Search">
<Database>master</Database>
<Root>/sitecore/content</Root>
<IndexAllFields>false</IndexAllFields>
<include hint="list:IncludeField">
<!-- some field you'd want to include -->
<fieldId>{8CDC337E-A112-42FB-BBB4-4143751E123F}</fieldId>
</include>
<include hint="list:ExcludeField">
<!-- __revision field -->
<fieldId>{8CDC337E-A112-42FB-BBB4-4143751E123F}</fieldId>
<!-- __context menu field -->
<fieldId>{D3AE7222-425D-4B77-95D8-EE33AC2B6730}</fieldId>
<!-- __security field -->
<fieldId>{DEC8D2D5-E3CF-48B6-A653-8E69E2716641}</fieldId>
<!-- __renderings field -->
<fieldId>{F1A1FE9E-A60C-4DDB-A3A0-BB5B29FE732E}</fieldId>
</include>
You can see a sample search config for the Advanced Database Crawler on SVN.
If you are using the Standard Sitecore DatabaseCrawler I would suggest you create a custom crawler that inherits from the Sitecore Database crawler and then override the AddAllFieldsMethod. Then just configure your index to use your custom crawler
You can look at the source code for the Advanced Database Crawler for an example of how that can be done. Something like this: (NOTE THIS HAS NOT BEEN TESTED)
public class DatabaseCrawler : Sitecore.Search.Crawlers.DatabaseCrawler
{
protected override void AddAllFields(Lucene.Net.Documents.Document document, Sitecore.Data.Items.Item item, bool versionSpecific)
{
if(IndexAllFields)
{
base.AddAllFields(document, item, versionSpecific);
}
else
{
var fieldsToIndex = new List<string>() {"title", "Text"};
foreach (var field in fieldsToIndex)
{
var scField = item.Fields[field];
document.Add(new LuceneField(scField.Key, scField.Value, LuceneField.Store.NO, LuceneField.Index.UN_TOKENIZED));
}
}
}
}