Where could I store custom field attributes for a

2019-08-10 19:38发布

问题:

I would like to be able to find a place to store custom attributes for a field in Sitecore. For example let's say that I have a data template called ProductData. And it has three fields:

  • Title (Single line text)
  • StrengthInPPM (number)
  • AbsoluteStrength (number)

The two numeric fields also have a unit of measure associated with them. The first one is ppm, the second is mg. Eventually this data will need to be displayed on a page. When the data is displayed on a page I would like it to display like this:

My Product Title
Strength In ppm: 27 ppm
Absolute Strength: 1350 mg

But I would like to find a way that the label for the field and the units of measure for the field could somehow be stored in Sitecore in the field definition itself. I think the label could be stored in the field's Title attribute - right? Is there any place that I could store the units of measure text in the field definition? I couldn't find any sort of field that I could use for custom data that goes along with a field definition.

The reason that I am asking this is that in reality for the project I am working on the data template will probably have more like 100 fields. It is all chemical data. I would like to be able to write a routine that could just iterate through all of the fields, get the label, the value and the units of measure and display it all on the page.

Of course another way I could accomplish this is to put the label and units of measure information either in an external database table or in a config file or something. But I would much rather just have it all in Sitecore.

Any ideas how I could accomplish this?

回答1:

I would suggest you split your product data into a main template (containing the product Title) and a secondary template called ProductProperty (containing PropertyName, PropertyValue, PropertyUnits etc). Then organise your content items as follows:

* Product X (uses ProductData template)
  * Property 1 (uses ProductProperty template)
  * Property 2
  * ...

This may seem cumbersome, but the benefits are that your content is easier to manage and you can then render this data more easily by looping over the child (ProductProperty items). You can do all this using the standard <sc:FieldRenderer /> or @Html.Sitecore().Field() methods which will ensure page editor compatibility.

If you frequently have the same set of properties for your products, you could create a branch template defining this set of ProductProperty items meaning you wouldn't have to go and creating those 100 items each time you add a new product.



回答2:

Not sure how you will end up using the data, or if it needs some cleverness involved for searches but to simplify your template you could consider the Name Value List field type. This will allow you to add an unlimited number of properties without having to define them for every template up front.

The only caveat here is that the Name part of the field can only contain letter and numbers, so no spaces. You could either replace something like underscores with spaces on render, or create a custom control yourself.

If you are going to create a custom control then also take a look at the Name Lookup Value List - you may be able to extend one of these to fit your needs, or take inspiration from them. You may be able to create a control with three fields: selectable name lookup, manual value entry, selectable strength unit.

In any case, it should be a lot neater than creating 100 fields in your template, esp if all fields are not applicable for all products.



回答3:

One approach might be to create a custom field type to represent "number and unit" data? (I've had a quick Google, and I don't think something like this exist already - but I may be wrong) A custom field could allow you to have a single field on your Item to represent each of these pairs of data.

The field control could present a text entry control to capture the number, followed by a representation of the units. Depending on what's more appropriate for your solution, it could:

  • Show a choice of units options as a dropdown, with the set of choices taken from the Source value for the Template's definition for the field.
  • Not offer a choice, but take a specific unit from the field's Source configuration and then just display it after the number.

I'd suggest defining the Units as a Data Template type themselves, and creating items for "Parts Per Million" (etc) with whatever extra data that your system needs.

The field control could store both the unit and the value in the underlying data. This would allow you to iterate the fields and process each one in turn as you mentioned. The format you choose for this is up to you, but commonly XML or some other approach to structured text is used. Perhaps something like:

<data value="0.83" units="{d44109f3-659b-43d7-b761-a793c4e2f733}"/>

The value attribute stores the number entered, and the units attribute contains the Item ID of the item defining the appropriate unit data.

In the same way the Sitecore Kernel does, you can then have helper methods or classes which unpack this data when it's processed in your code, to help render it in the published website's UI.

There are plenty of resources on the Internet for how to go about creating the code for a custom field. This Google search lists a few of them to get you started.



回答4:

It is my opinion that the numeric values, the units, and the labels are all content. And if they are content, they should be easily authorable by regular content authors, so don't hide these labels and units in the system. IMO, I would just make the entire line be part of the field, I'm not sure why you wouldn't, e.g.

  • Title (SLT)
  • Strength (SLT)
  • AbsoluteStrength (SLT)

You could even set the standard values for the two latter fields like so:

  • Strength In ppm: VALUE ppm
  • Absolute Strength: VALUE mg

If this is not acceptable because a content author can change the label on a single instance of the item differently than another, then you may want to consider moving the labels and units into dictionary entries.



标签: sitecore