How to add a field to a Content-Type in OrchardCoreCMS? - orchardcms

Using code, I am trying to create a "Widget" Content-Type called FeaturedProperties with an editable field called MaxVisibleField. I want the MaxVisibleField field to show up under the Fields section when editing the content-type.
I used migration to create a new Contact-Part called MaxPropertiesToShow and created a field called MaxVisibleField for the MaxPropertiesToShow part.
I am expecting the MaxVisibleField field to show up when the “Widget” is edited in the admin panel. When I edit the FeaturedProperties content-type, I see no fields listed. However, I do see the part.
What else do I need to do to make my field MaxVisibleField show up when my Widget is being edited?
Here is my migration code
public int Create()
{
_contentDefinitionManager.AlterPartDefinition("MaxPropertiesToShow", (part) =>
{
part.WithField("MaxVisibleField", c =>
{
c.OfType(nameof(NumericField))
.WithDisplayName("Max Properties To Show")
.WithDescription("The app with display up to this many properties")
.WithSettings(new NumericFieldSettings()
{
Required = true,
Maximum = 50,
DefaultValue = "8",
});
});
});
_contentDefinitionManager.AlterTypeDefinition("FeaturedProperties", (type) =>
{
type.Stereotype("Widget")
.Creatable(false)
.Listable(false)
.Versionable(false)
.WithPart(nameof(FeaturedPropertyPart));
});
return 1;
}
Here is a screenshot of what I am seeing
Also, in the Widget-FeaturedProperties.cshtml view, how can I render the title in a place I want it to show up instead of the standard position the app displays it in?

Related

How can I get a text field in the mobile app to allow large entries of text

I'm trying to setup a custom screen in the mobile app, and there are a few things that are still a mystery.
One of them is how to get a PXTextEdit field to show multiple lines, or even allow entry into that field without it just highlighting the field and no way to add to it.
The other is how to add a Rich Text control to the mobile app ala the Cases screen. I've tried using the code to add it the way the Cases screen does. I have a PXRichTextEdit field, called 'Details', but this doesn't show up on the mobile app at all:
add field "Details" {
textType = HTML
}
In this context:
add screen AC503000 {
add container "MeetingAgenda" {
add field "MeetingID"
add field "Subject"
add field "Status"
add field "MeetingDate"
add field "MeetingTime"
add field "Details" {
textType = HTML
}
add recordAction "Save" {
behavior = Save
}
add recordAction "Cancel" {
behavior = Cancel
}
add recordAction "Delete" {
behavior = Delete
}
}
I've also tried to add a container called "Details" since this shows up in the WSDL file, and that shows a menu item, but takes you nowhere, and sends the app into a tailspin where it can't recover and has to be restarted.
At this point I'm lost as to how to do these two things...
To enable multiline mode, use
add field "Details" { textType = PlainMultiLine }
To show your PXRichTextEdit field try to use {Container Name}#{Field Name} from the WSDL
add field "Details#Details" { textType = HTML }

Changing the title of the header

I use the SAPUI5 Framework.
Within a page there are two possibilities for action, to make a new patient and to edit a patient. The new and edit, control/view pages are compiled into one, one control and one view for both new and edit.
Since they both are within one-page control/view, whenever I try to make a new patient or to edit a patient I get the same title for both of these Options.
My approach.
Get the URL of the page -> New or Edit. -> Split till I get, if it's new or edit.
If its new, change the title to New if edit that the title to Edit.
var completurl = window.location.href;
var split = completurl.split("/");
if(split[7] == 'new'){
this.byId("semantic:DetailPageID").setTitle("New");
}
else {
this.byId("semantic:DetailPageID").setTitle("Edit");
}
Expected: To change the title of the header.
Actuality: Doesn't do shit
First you need the different buttons in your main.view.xml.
main.view.xml:
<Button press="onPressNew" text="New"/>
<Button press="onPressEdit" text="Edit"/>
Now you could use a local model for saving the action
editNew.controller.js:
onPressEdit: function () {
this.myLocalModel.setProperty("/Action", "Edit");
//Navigate to editNew.view.xml
},
onPressNew: function () {
this.myLocalModel.setProperty("/Action", "New");
//Navigate to editNew.view.xml
}
In your editNew.view.xml, you could use expression binding
editNew.view.xml:
<Page id="pageEditNew" title="{= ${localModel>/Action} === 'Edit' ? ${i18n>title_edit} : ${i18n>title_new}}">

How to override template file item-list.html.twig for field_slider_images in Drupal 8?

I want to override the item listing template file core/themes/classy/templates/dataset/item-list.html.twig for listing the fields field_slider_images as well as field_blog_tags respectively of their's multiple values of the field.
I have selected "Unordered List" in the view.
Please do check the attached image.
I have created following files :
item-list--field-blog-tags.html.twig
item-list--field-slider-images.html.twig
But, this is not rendered for the listing of the fields.
When I have created item-list.html.twig then only it will access.
However, both fields have different data to style and I am not able to get the current field name which is loading it's data in item-list.html.twig.
Had a brief look at this and it doesn't seem that 'item-list' to have suggestions, which is quite unfortunate.
In this situation there are two options:
Create your own suggestion which would accomplish exactly what you need.
You'll have to do something like this:
/
/*add new variable to theme with suggestion name*/
function hook_theme_registry_alter(&$theme_registry) {
$theme_registry['item_list']['variables']['suggestion'] = '';
}
//send a value to newly added variable to use it build the suggestion
function hook_ENTITY_TYPE_view(array &$build, $entity, $display, $view_mode) {
//add condition here if field exists or whatever, do the same for other field
$build['field_slider_images']['#suggestion'] = 'field_slider_images';
}
//use newly added variable to build suggestion
function hook_theme_suggestions_THEME_HOOK(array $variables) {//THEME_HOOK=item_list
$suggestions = array();
if(isset($variables['suggestion'])){
$suggestions[] = 'item_list__' . $variables['suggestion'];
}
return $suggestions;
}
Now you should be able to use item-list--field-slider-images.html.twig
Second option is to do what others in core did: use a new theme
function hook_ENTITY_TYPE_view(array &$build, $entity, $display, $view_mode) {
//add condition here if field exists or whatever, do the same for other field
$build['field_slider_images']['#theme'] = array(
'item_list',
'item_list__field_slider_images',
);
}

Orchard CMS custom field admin settings

I have a custom field in Orchard, I want it to have its own admin view to set the fields default value so that when it is used with other parts - it will always use this default display value.
I have the part, and the admin menu, currently the admin menu goes to the fields settings controller, but how do I create the fields shape for display?
I imagine something like this, but can't figure out what is the correct way to create the fields editor shape:
[Admin]
public class FieldAdminController : Controller
{
public ActionResult TimezoneSettings()
{
// var timezoneShape = Shape.Create("?");
// var model = new TimezoneViewModel(timezoneShape);
// return View(model);
// Or:
// TimezonePart part = Services.ContentManager.New<ITimezoneField>("TimezoneField");
//var model = Services.ContentManager.BuildEditor(part);
// return View(model);
}
}
The field does already work (i.e. the driver is working) when you attach the field to a content part via the admin UI, but I will only be using it with other code created custom parts in my modules.

Trying to hide Content Parts on Content Type

I am building Content Types and adding Content Parts specific to a Client and Attorney. All of these parts have fields and/or content pickers, etc.
I want to restrict the Client Role to see only Client Content Parts, while I just allow the Attorney Role to see any Content Parts, including it's own Attorney Content Part for a particular Content Type. Again, these are all on the same Content Type, so Content Permissions will not work (except on the Content Type in general).
I want to hide the Attorney Content Parts when a Client is logged on.
I have tried using this:
public override void Displaying(ShapeDisplayingContext context)
{
context.ShapeMetadata.OnDisplaying(displayedContext => {
var shape = context.Shape;
if (context.Shape.Part.Name == "Parts_AttorneyMatterPart")
{
var workContext = _workContextAccessor.GetContext();
var user = workContext.CurrentUser;
var roles = user.As<UserRolesPart>().Roles;
if (!roles.Contains("Spaces Attorney"))
{
shape = null;
}
}
});
}
Where I have a Content Part named "AttorneyMatterPart", and where the Attorney Role is "Spaces Attorney".
These Content Types and Parts were all created in the Orchard Admin. The only thing in my module is this class file.
But this won't hide the Content Part when the Client is logged in. I know that I have to work on the logic of what roles can see things (going to add || conditions for Admin, etc.). For now I am just testing this out.
Any help is appreciated.
EDIT (Bounty Added)
I am really stumped as to whether or not this is even possible. This Content Part is created through the Admin UI. Under shape tracing I can see under the "Content" zone Model > ContentItem > AttorneyMatterPart. I have tried ShapeTableBuilder and I have tried OnDisplaying and OnDisplayed from a ShapeDisplayingContext.
If someone could provide a working sample it would be much appreciated.
When a content part is created through the admin dashboard, there isn't really a shape to render it, only individual shapes for inner content fields...
So, try this
public override void Displaying(ShapeDisplayingContext context) {
context.ShapeMetadata.OnDisplaying(displayedContext => {
var shape = displayedContext.Shape;
if (shape.ContentPart != null
&& shape.ContentPart.PartDefinition.Name == "PartName") {
var workContext = _workContextAccessor.GetContext();
var user = workContext.CurrentUser;
if (user == null || !user.Has<UserRolesPart>()
|| !user.As<UserRolesPart>().Roles.Contains("RoleName")) {
displayedContext.ChildContent = new System.Web.HtmlString("");
}
}
});
}
See my answer on OrchardPros
http://orchardpros.net/tickets/6914
Best
Nulling the shape variable will just clear the local reference. Setting the following however should hide the shape:
displayedContext.ShapeMetadata.Position = "-";
Also FYI it's better not to check on roles the user have but rather create a custom permission, add that to the user role and then check for the permission through
IAuthorizationService.TryCheckAccess()

Resources