Skip to content

Instantly share code, notes, and snippets.

CustomFieldFactory.setComponent(AllFieldTypes.TextField, (props : FieldWithValueProps<FormField<TextViewModel>>) => {
// Replace markdown style links in text, eg. this is a sentence with [a link](https://google.com) in it
const markdownRegex = /\[([^\]]+)\]\(([^\)]+)\)/;
props.field.model.text = props.field.model.text?.replaceAll('\n','<br/>');
props.field.model.text = props.field.model.text?.replace(/\*\*(.*?)\*\*/g, "<b>$1</b>");
if(props.field.model.text && props.field.model.text.match(markdownRegex)){
props.field.model.text = props.field.model.text.replace(markdownRegex, '<a href="$2">$1</a>');
const Tag = (props.field.model.htmlTag || 'p');
return React.createElement(Tag, { className: props.field.model.cssClass, dangerouslySetInnerHTML: {__html: props.field.model.text} });
}
@moo2u2
moo2u2 / DataSourceSettingsManager
Created April 5, 2023 22:53
Fix for Sitecore Forms truncating long labels in list items (eg. checkbox list)
/// <summary>
/// Overriding this class to fix issue where labels (eg. on checkbox list) are truncated
/// </summary>
public class DataSourceSettingsManager : Sitecore.ExperienceForms.Mvc.DataSource.DataSourceSettingsManager
{
protected override void UpdateSettings(ListFieldItemCollection settings, Item fieldSettingsItem, FieldSettingsContext settingsContext)
{
//base.UpdateSettings(settings, fieldSettingsItem, settingsContext);
Assert.ArgumentNotNull(settings, "settings");
Assert.ArgumentNotNull(fieldSettingsItem, "fieldSettingsItem");
@moo2u2
moo2u2 / custom-field-factory.tsx
Created May 4, 2023 02:25
Sitecore (headless) Forms custom field factory with hidden field using query string value provider. Sets the value of the hidden field as a given value from the query string.
import { useRouter } from 'next/router';
import {
FieldViewModel,
InputViewModel
} from '@sitecore-jss/sitecore-jss-forms';
import {
createDefaultFieldFactory,
ValueFieldProps
} from '@sitecore-jss/sitecore-jss-react-forms';
@moo2u2
moo2u2 / Feature.GraphQL.config
Last active October 7, 2023 03:12
Patching out the standard field filter and adding the security fields
<configuration>
<sitecore xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<api>
<GraphQL>
<defaults>
<content>
<schemaProviders>
<edgeContent type="Sitecore.Services.GraphQL.EdgeSchema.EdgeSchemaProvider, Sitecore.Services.GraphQL.EdgeSchema">
<templates type="Sitecore.Services.GraphQL.Content.TemplateGeneration.Filters.StandardTemplatePredicate, Sitecore.Services.GraphQL.Content">
<paths>
@moo2u2
moo2u2 / normal-mode.ts
Created October 8, 2023 02:30
Sitecore "normal mode" plugin with support for parsing Sitecore item security permissions
// Removes components from placeholders (in the page layout) if given role is not authorized to view them
function removeComponents(placeholders: PlaceholdersData, role: string) {
for (let placeholder in placeholders) {
for (let i = 0; i < placeholders[placeholder].length; i++) {
if ('fields' in placeholders[placeholder][i] || 'placeholders' in placeholders[placeholder][i]) {
const componentRendering = placeholders[placeholder][i] as ComponentRendering;
if (componentRendering?.fields) {
// @ts-expect-error
if('data' in componentRendering.fields && componentRendering.fields.data.datasource?.security) {
const Inheritance = '!*';
export const securityFieldName = '__Security';
const permissionCanRead = '+read';
const permissionCannotRead = '-read';
enum ParseState { au, ar, pe, pd };
export type Permissions = {
[username: string]: {
@moo2u2
moo2u2 / oc_product_sync_msg.json
Created March 7, 2024 04:26
OrderCloud Product Sync sample message
{
"Headers": {
"type": "sitecore.ordercloud.messages.product.updated",
"marketplace": "yourMarketplaceId",
"tenantid": "yourMarketplaceId"
},
"Payload": {
"UserContext": {
"ID": null,
"AnonymousID": null
@moo2u2
moo2u2 / oc_two_variants.json
Last active March 13, 2024 06:13
OrderCloud 2 variants created
{
"Meta": {
"Page": 1,
"PageSize": 20,
"TotalCount": 2,
"TotalPages": 1,
"ItemRange": [
1,
2
],
{
"Meta": {
"Page": 1,
"PageSize": 20,
"TotalCount": 3,
"TotalPages": 1,
"ItemRange": [
1,
3
],
{
"Meta": {
"Page": 1,
"PageSize": 20,
"TotalCount": 2,
"TotalPages": 1,
"ItemRange": [
1,
2
],