#8 Advanced Formula API
Requirements
Advanced Formula APIs
In this tutorial you'll learn some of the advanced APIs such as:
- Detect if features are enabled new in v14
- Get user information about the current user new in v16
Important: Now using Formulas V2 🆕 in 2sxc 16
Formulas V2 has intellisense, stoppability, support for promises and more.
Formulas V2 has intellisense, stoppability, support for promises and more.
Important: The feature "Public Use of Edit Form" is disabled
If you want this demo to run for anonymous users you would need it. Register your site on patrons.2sxc.org to get access to the feature.Detect if Features are Enabled
Try it:
Show a warning or info based on the feature GoogleTranslate being enabled.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasFeatures.GoogleTranslateWarning
Field.Settings.Visible (Formula-Target: Field.Settings.Visible)
v2((data, context) => { return !context.features.isEnabled("EditUiTranslateWithGoogle"); });
Formulas of FormulasFeatures.GoogleTranslateInfo
Field.Settings.Visible (Formula-Target: Field.Settings.Visible)
v2((data, context) => { return context.features.isEnabled("EditUiTranslateWithGoogle"); });
User Information in Formulas
Try it:
This formula shows information about the user and shows a warning if the user is not logged in.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasUser.UserInformation
Field.Settings.Notes (Formula-Target: Field.Settings.Notes)
v2((data, context) => { return data.default .replace('[name]', context.user.name ?? "unknown") .replace('[userid]', context.user.id) .replace('[isanonymous]', context.user.isAnonymous) .replace('[issiteadmin]', context.user.isSiteAdmin); });
Formulas of FormulasUser.WarningAnonymous
Field.Settings.Visible (Formula-Target: Field.Settings.Visible)
v2((data, context) => { return context.user.isAnonymous; });
#8 Advanced Formula API
Source Code of this file
Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code
@inherits Custom.Hybrid.Razor14 @using ToSic.Razor.Blade; <!-- unimportant stuff, hidden --> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <h2>Advanced Formula APIs</h2> <p>In this tutorial you'll learn some of the advanced APIs such as:</p> <ul> <li> Detect if features are enabled <em>new in v14</em> </li> <li> Get user information about the current user <em>new in v16</em> </li> </ul> @Html.Partial("./Part NowUsingV2.cshtml") </div> </div> @Html.Partial("../shared/WarnAnonymousIfFeatureIsOff.cshtml") @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })