Widget to show Data - orchardcms

I created a Deposit module. My main goal with this module is to save some data on the DB.
I already made my custom type (Deposit) with my custom part (DepositPart) and it worked like I expected.
This DepositPart save the name, currency, liquidity, month, and url on the database.
But now I want to make a simple widget with 3 combobox and a button.
On those combobox I have some static text where the user can choose from. When the user hit the button I want to make a query and return a list with some Deposits and show only the name and the liquidity.
What is the best way to achieved this?

The best is creating your own controller.
For the part you have you can easily present as a widget adding some lines to migrations.
example:
(you can find more at this link: http://docs.orchardproject.net/Documentation/Writing-a-widget )
public int UpdateFrom1()
{
// Create a new widget content type with our map
ContentDefinitionManager.AlterTypeDefinition("MapWidget", cfg => cfg
.WithPart("MapPart")
.WithPart("WidgetPart")
.WithPart("CommonPart")
.WithSetting("Stereotype", "Widget"));
return 2;
}
Note that you simple add WidgetPart and add stereotype "Widget" to a new Content Type that also has yourpart (in this example MapPart).
For the result you may use a controller.
Having more information about your part I can help you more.

Related

How to display dynamic default value in field of Strapi collection page?

I want to display some read only URL in STRAPI collection page.
let's say I am at
/admin/plugins/content-manager/collectionType/application::my-public-form.my-public-form/1
now I have one field that need to display URL like this
https://myurl/1
is there is any way to get current url i.e. /admin/plugins/content-manager/collectionType/application::my-public-form.my-public-form/1
and in field I display https://myurl/1
This is going to be a lengthy answer, but let me try to simplify as much as possible.
Explanation:
To achieve this you need to simply create a field in the collection type and mark it as non-editable in the admin interface. This will make it a read-only property. Next, we need to override the afterFindOne from the lifecycle hooks of the model so that when it fetches the entry from the db, we can pre-populate the url for that particular entry, so that it's displayed in the read-only field in the UI.
Implementation:
Let's assume we are creating a collection-type called Student, which some basic field like first_name, last_name, dob and student_url. We are going to make the student_url field read-only in the following way:
Create the collection type Student as show in the screenshot.
Visit the newly created collection-type in the content-manager, click on Create new entry button, and then click on Configure view in the right hand panel.
Click on the small pencil icon which is besides the student_url field to open a popup, and then set the Editable property to False and click Finish.
At this point you're almost done, the only thing left to do is set the lifecycle hook to populate the student_url field when it's fetched from the database so that it's displayed correctly in our UI.
Create a lifecycles.js file in your model folder.
// src/api/student/content-types/student/lifecycles.js
module.exports = {
afterFindOne(event) {
const { result } = event;
// You can easily change this line to `/admin/plugins/content-manager/collectionType/application::my-public-form.my-public-form/${result.id}` in your project.
if (result) result.student_url = `https://example.com/${result.id}`;
},
};
And that's it. Now create an entry in the admin UI with the rest of editable fields and see entry being populated automatically with the student_url. Cheers!

Alternate shape for EditorTemplate of Field is not being recognized

I need an alternate for the EditorTemplate of an Enumerator Field that's used when the Field has a particular name (PublishingMethod).
Based on the docs, I created a view with the pattern [ShapeType__FieldName] in the same folder as the original shape:
This is not working and still uses the original. I've thought of changing the Editor method in the Driver, but I think that defeats the purpose of alternates, which is that Orchard automatically detects the correct shape as I understand from the docs:
The Orchard framework automatically creates many alternates that you can use in your application. However, you can create templates for these alternate shapes.
Note: I can't use the Shape Tracing module, it never worked even with a clean Orchard install.
The editors in Orchard work different to how Display works. I guess it is so you get a MVC-style experience. Basically, the actual shape returned is of type EditorTemplate, which then binds your model and prefix then renders a partial view with the template name you gave it. What this means is alternates wont work as expected, or as the docs state. The alternates for your field name are actually added to the EditorTemplate shape. So what you can do is add a view called EditorTemplate-PublishingMethod.cshtml with contents like:
#{
var m = (Orchard.Fields.Fields.EnumerationField)Model.Model;
}
#Html.Partial("PublishingMethodEditor", m, new ViewDataDictionary {
TemplateInfo = new TemplateInfo { HtmlFieldPrefix = Model.Prefix }
})
Then add another view called PublishingMethodEditor.cshtml with the overrides you want for your editor. All these views should go in the root of your Views folder.
Another approach would be to implement the IShapeTableProvider interface and adjust the TemplateName property on a certain condition but, meh, that requires code...
Edit 1
If you have that field name on other content types that you don't want to override you can use the override EditorTemplate-ContentTypeName-PublishingMethod.cshtml

OrchardCMS: How to access Content Menu Item boolean field in cshtml view

In orchard, I've added a boolean field called "IsDone" to the built in Content Menu Item content part via that Admin interface. I've then picked an item in Navigation and set the option to "yes" for the corresponding field i added.
In my custom theme, I've copied over MenuItem.cshtml.
How would I get the value of my custom "IsDone" field here?
I've tried something like
dynamic item = Model.ContentItem;
var myValue = item.MenuItem.IsDone.Value;
but I'm pretty sure my syntax is incorrect (because i get null binding errors at runtime).
thanks in advance!
First i suggest you use the shape alternate MenuItemLink-ContentMenuItem.cshtml instead of MenuItem.cshtml to target the content menu item directly.
Secondly, the field is attached to the ContentPart of the menu item. The following code retrieves the boolean field from this content part:
#using Orchard.ContentManagement;
#using System.Linq;
#{
Orchard.ContentManagement.ContentItem lContentItem = Model.Content.ContentItem;
var lBooleanField = lContentItem
.Parts
.Where(p => p.PartDefinition.Name == "ContentMenuItem") // *1
.SelectMany(p => p.Fields.Where(f => f.Name == "IsDone"))
.FirstOrDefault() as Orchard.Fields.Fields.BooleanField;
if (lBooleanField != null)
{
bool? v = lBooleanField.Value;
if (v.HasValue)
{
if (v.Value)
{
#("done")
}
else
{
#("not done")
}
}
else
{
#("not done")
}
}
}
*1
Sadly you cannot simply write lContentItem.As<Orchard.ContentManagement.ContentPart>() here as the first part in the part list is derived from this type, thus you would receive the wrong part.
While #ViRuSTriNiTy's answer is probably correct, it doesn't take advantage of the power of the dynamic objects that Orchard provides.
This is working for me but is a much shorter version:
#Model.Text
#{
bool? IsDone = Model.Content.ContentMenuItem.IsDone.Value;
var IsItDoneThough = (IsDone.HasValue ? IsDone.Value : false);
}
<p>Is it done? #IsItDoneThough</p>
You can see that in the first line I pull in the IsDone field using the dynamic nature of the Model.
For some reason (I'm sure there is a good one somewhere) the BooleanField uses a bool? as its backing value. This means that if you create the new menu item and just leave the checkbox blank it will be null when you query it. After you have saved it as checked it will be true and then if you go back and uncheck it then it will have the value false.
The second line that I've provided IsItDoneThough checks if it has a value yet. If it does then it uses that, otherwise it assumes it to be false.
Shape Alternate
#ViRuSTriNiTy's other advice, to change it to use the MenuItemLink-ContentMenuItem.cshtml instead of MenuItem.cshtml is also important.
The field doesn't exist on other menu items so it will crash if you try to access it. Just rename the .cshtml file to fix this.
Dynamic Model
Just to wrap this up with a little bit of insight as to how I got there (I'm still learning this as well) the way I figured it out is as follows:
.Content is a way of casting the current content item to dynamic, so you can use the dynamic advantages with the rest of line;
When you add the field in the admin panel it looks like it should be right there on the ContentItem, however it actually creates an invisible ContentPart to contain them and calls it whatever the ContentItem's type is.
So if you had added this field to a Page content type you would have used Model.Content.Page.IsDone.Value. If you had made a new content type called banana it would be Model.Content.Banana.IsDone.Value, etc.
Once you are inside the "invisible" part which holds the fields you can finally get at IsDone. This won't give you the actual value yet though. Each Field has its own properties which you can look up in the source code. the IsDone is actually a BooleanField and it exposes its data via the Value property.
Try doing a solution-wide search for : ContentField to see the classes for each of the fields you have available.
Hopefully this will have explained things clearly but I have actually written about using fields in a blog post and as part of my getting started with modules course over on the official docs (its way down in part 3 if you're curious).
Using built-in features instead of IsDone
This seems like a strange approach to do it this way. If you have a Content Item like a Page then you can just use the "Show on a menu" setting on the page.
Go to admin > content > open the page > down near the bottom you will find "Show on a menu":
This will automatically put it into your navigation and then you can move it around to where you want:
After it "IsDone" you can just go back and untick the "Show on a menu" option.
Setting up the alternative .cshtml
To clarify your comments about how to use the alternative, you need to
Copy the file you have at Orchard.Core/Shapes/Views/MenuItem.cshtml over to your theme's view folder so its /Views/MenuItem.cshtml
Rename the copy in your theme to MenuItem-ContentMenuItem.cshtml
Delete probably everything in it and paste in my sample at the start of this post. You don't want most of the original MenuItem.cshtml code in there as it is doing some special tricks to change itself into a different shape which isn't what you want.
Reset your original Orchard.Core/Shapes/Views/MenuItem.cshtml back to the factory default, grab it from the official Orchard repository
Understanding the view names
From your comments you asked about creating more specific views (known as alternates). You can use something call the Shape Tracer to view these. The name of them follows a certain pattern which makes them more and more specific.
You can learn about the alternates on the official docs site:
Accessing and Rendering Shapes
Alternates
To figure out what shape is being used and what alternates are available you can use the shape tracing module which is documented here:
Getting Started with Shape Tracing

Lightswitch Search screen - choosing what you can search by

I have a search screen which shows results from a 'projects' entity. One of the fields is a link to a 'Clients' entity, which has a client name.
I can use the search box above the data to search by project name, date, etc but I cannot search by client name. i guess because it is actually a reference to a seperate entity.
How can i make it so that I can search for projects, using the search box, by client name?
To try to explain clearer here is the database layout.
Project
------
ProjName
ProjType
ProjComment
Client --------------- Client
-----
Name
Address
I can search by the project fields, but not the client name.
Create a custom query for Project entity and add optional parameters for all the Project and Client properties that you wish to search for. Wire up the custom query filter.
For full control do not wire up the custom query filter, instead filter in code behind, the PreProcess event.
if your using the HTML Client it can be done as follows on a browse screen associated with in your case the Project Table. if you look at the left navigation bar, click on edit query and within here, you can see 3 options, Filter, Sort and Parameters.
Filter should say Client.Name based on your table structure, the second should be changed to "contains", the 3rd should be set as parameter and finally the 4th box is where you create the new parameter you are going to use as a search box...
you can rename the Parameter at the bottom and I always find its best when on this, to set it as optional in the bottom right property box. (This means all the data will be displayed rather than nothing until searched)
Now if you were to go back to your main screen page, drag this parameter from the left onto the main screen page and set it as a text box. Using this and pressing enter after you have typed something will display the results that match what you have typed so far.
a little more to the above, if you were to click "Edit PostRender Code" and add the below then after 3 characters are entered, the table list your searching through will be updated automatically after each finger stroke...
$searchBox = $("input", $(element));
setTimeout(function () {
$searchBox.focus();
}, 1);
onInputAsYouType(element, 1, function (text) {
contentItem.screen.[CREATED PARAM NAME] = text; //SearchText here is the data item in the screen designer linked to the query parameter
});
function onInputAsYouType(element, numberOfRequiredChars, done) {
var inputbox = $("input", $(element));
inputbox.on("input", function (e) {
var text = $(this).val();
if (text.length >= numberOfRequiredChars)
done(text);
});
};
};

Person & Group custom field type sharepoint

I have created a custom field type as it is there in sharepoint OOTB, the difference is only that the end user does not need to check the name i.e I have replaced it with DropDownList. The dropdownlist suggest the no. of users available in the web site for that I have created a FieldClass which inherits from SPFieldUser and a FieldControlClass which inherits from UserField. It is working fine in all conditions i.e when I create a List or Document Libarary it shows me the DropDownList
with respective users after saying OK it creates an item for me. I have overriden a Value property in FieldControlClass as follows,
public override object Value
{
get
{
SPUserCollection userscollection = rootWeb.SiteUsers;
//ddlInfoBox is a DropDownList to which I have Binded the collection of users in the form of string
SPUser user = userscollection.Web.EnsureUser(this.ddlInfoBox.SelectedValue);
SPFieldUserValue userval = new SPFieldUserValue(user.ParentWeb, user.ID, user.LoginName);
return userval;
}
set
{
SPFieldUserValue userval = (SPFieldUserValue) this.ItemFieldValue;
this.ddlInfoBox.SelectedValue = userval.Lookupvalue; //Here look up value is nothing but a Login name e.g In-Wai-Svr2\tjagtap
}
}
Due to above property the Custom Field's Value for this current ListItem will be stored as SPFieldUserValue e.g 27#;In-Wai-Svr2\tjagtap.
The main problem is here, when this particular ListItem is shown in the list page views e.g on AllItems.aspx or the custom view pages associated with it, it shows the
number as 27 as a FieldValue insted of HyperLink with text as "In-Wai-Svr2\tjagtap" and PostBackURL as "/_layouts/userdisp.aspx?ID=27".
When I edit this Item it makes the respective value selected in the dropdownlist, also while viewing this item i.e on DispForm.aspx it also shows the hyperlink. I have
acheived it by writting a custom logic in createchildcontrol() method i.e by using ControlMode if it is New or Edit then fill the dropdown list, if it is Display then get the ItemFieldValue Type Cast it into SPFieldUserValue and get corresponding lookupid and value for making the URL and showing Text of the HyperLink.
I have spent a lot of time on searching and bringing the HyperLink as the user name with navigation insted of UserID (27) as a string on the list view pages e.g AllItem.aspx but to no avail, then after a lot of research I found that there might be a way of achieving such kind of functionality by using field type definition xml file where there is a provision to define a DisplayPatteren as you wish by specifying the html code. But here is a problem How can I get the UserID (27) with respective UserName e.g In-Wai-Svr2\tjagtap inorder to make an anchor tag like In-Wai-Svr2\tjagtap which will solve my problem. I have hard coded this anchor tag within the Default case statement of a switch under DisplayPatteren but it shows me the field value on AllItems.aspx as
In-Wai-Svr2\tjagtap27 i.e the value defined in xml file is concatenating with the string value (27).
Please help me to resolve the above mentioned 2 issue. I am really in need of solving this problem ASAP.
Thanks & Regards,
Tejas Jagtap
Have u tried to override the GetFieldValueAsHtml() method in the the custom field class or maybe the RenderFieldForDisplay() method in the custom field control class.
Can you use the DisplayPattern CAML from the User field type?

Resources