Part 2: the back-office
By the end of this part, you will have:
- an Articles page listing the
POSTtable in a listbox; - an Article editing page, with a form pre-filled from the opened article;
- two 4D functions,
savePostanddeletePost, wired to the form's buttons.
1. Create the two pages
In BWEB, a page is a menu entry of the domain. Open the administration (https://myblog.test/bweb), then the domain's Pages screen.
The Articles page
- Click Add a page and name it
Articles. - Tick Published and save.
The Article editing page
This page is dynamic: its address contains the identifier of the article to open, and BWEB loads that article into the Entity process variable before rendering the page.
- Add a page named
Article editingand tick Published. - In its URL parameters section, add a parameter:
| Setting | Value | Effect |
|---|---|---|
URL parameter name (name) | postUuid | The {postUuid} segment of the address |
Table name (tableName) | POST | The table the article is looked up in |
Field name (fieldName) | uuidKey|new | The article is looked up by uuidKey; the word new in its place opens the page with no article, to create one |
Process variable name (processVariableName) | Entity | The article found goes into Entity |
BWEB builds the page's address from its name and its parameters, for example /article-editing/{postUuid}. Note the exact address in the page's sheet: you will need it later.
A page whose Published box is not ticked answers 404 to everyone except the people who may edit it. Logged in as
DEV, you still see it: test logged out too, or in a private browsing window.
2. Open the dev panel
- Open the Articles page in the browser, logged in as
DEV(the administration's See the site link, then the page's address). - At the top right, the eye button opens the dev panel on hover. Its toolbar buttons let you pin it (it stays open) or dock it next to the page.
- Find its tabs: Objects (the palette and the block tree), Parameters (the settings of the selected block), Layout (styling), Structure (the database tables), Files and Page.
To select a block, use the pencil of its line in the object tree, or right-click: a click in the page does not select it. Each setting is saved as soon as you change it; the dev panel's Undo and Redo buttons step back and forth.
3. The article list
Place the listbox
- In the Structure tab, find the
POSTtable and drag it onto the page: BWEB creates a listbox bound to that table. - Select the listbox. In Parameters, give it the block name
postList. The block name is required and unique within the page: your code and your events refer to it. - Check that the table (
vt_TableName) is indeedPOST.
Choose the columns
In the listbox's column list, add (Add a column) the fields title, isPublished and publicationDate, and give each one a label. A boolean is shown as a ticked box.
To make a column sortable, turn on its sorting (vb_IsSortable): its header becomes clickable. The sorted path (vt_sortable) is the field name, with no direction: the direction comes from the header arrow.
Sort on opening
By default, a listbox bound to a table shows ds.POST.all(). To show the newest articles first, fill in the First request (vt_FirstRequest):
ds.POST.all().orderBy("publicationDate desc")
This expression must return an entity selection of the listbox's table, and must contain no $ local variable: it is evaluated by EXECUTE FORMULA.
Open an article with a click
Going from a row to its detail page is an event of the listbox:
- In the listbox's events, add an
onSelectevent. - Pick the
linkrender function (classrenderFunction) and, as destination page,Article editing. - Give the
postUuidparameter the value{vt_pk}: BWEB replaces it with the primary key of the clicked row.
The New article link
Above the listbox, place a Link block (link). In its https page link setting (vt_CustomLink), enter the address of the editing page with new as the last segment, for example /article-editing/new. To make it look like a button, turn on Is a button? (vb_IsButton). Put a text block "New article" inside it.
4. The editing form
Open the editing page on an existing article (click a row of the listbox), then compose the form inside a container.
- Place a Container (
wrapper) and name itpostForm. All the form's fields go inside it: this is the container the save button will send. - Inside the container, place the following blocks:
| Block | Field name (vt_FieldName) | Settings |
|---|---|---|
Input field (fieldText) | title | Label "Title", Required field |
| Input field | summary | Field type Multiline Text, label "Summary" |
| Input field | content | Type Multiline Text, Rich text editor on, label "Content" |
Switch (fieldToggle) | isPublished | Label "Published" |
On each of these four fields, turn on Is linked to the module entity? (vb_isLinkedToModuleEntity): the field is then pre-filled with the value of the article loaded in Entity. The field name must be exactly the name of the table's attribute.
Finally, add two Buttons (button) below the container: "Save" and "Delete" (Button label setting). You will wire an action to them in step 6.
There is no
<form>tag in a BWEB page: the button's event names the container whose fields are sent. The same field can therefore serve several actions.
5. Write the 4D functions
Open the POST class and add these two functions:
Class extends DataClass
Function savePost($vo_POST : Object; $Entity : Object; $vo_Session : Object; $vo_Infos : Object)->$vo_WebResponse : cs.bspkComponent.WebFormController
/* parameters
vt_PostForm:select:getBlocsNameCollection:["wrapper"]:mandatory:tomSelect:label:BSPK_sendAllObjectsOfBlocName
vt_SendAllObjectsOfBlocName:hidden:vt_PostForm
*/
var $post : Object
var $vo_Status : Object
var $vt_Slug; $vt_Char : Text
$vo_WebResponse:=cs.bspkComponent.WebFormController.new()
// Creation (address .../new: Entity is Null) or update
If ($Entity=Null)
$post:=This.new()
Else
$post:=$Entity
End if
$post.title:=String($vo_POST.vt_FieldValue.title)
$post.summary:=String($vo_POST.vt_FieldValue.summary)
// The rich text editor posts base64-encoded HTML:
// BSPK_RTE_DECODE decodes and sanitises it
$post.content:=BSPK_RTE_DECODE(String($vo_POST.vt_FieldValue.content))
// Publication date set when the article becomes published
If (Bool($vo_POST.vt_FieldValue.isPublished)) && (Not(Bool($post.isPublished)))
$post.publicationDate:=Current date
End if
$post.isPublished:=Bool($vo_POST.vt_FieldValue.isPublished)
// Readable address, computed from the title (simplified)
$vt_Slug:=Lowercase($post.title)
For each ($vt_Char; [" "; "'"; "!"; "?"; ","; "."; ":"])
$vt_Slug:=Replace string($vt_Slug; $vt_Char; "-")
End for each
$post.slug:=$vt_Slug
$vo_Status:=$post.save()
If ($vo_Status.success)
$vo_WebResponse.sendAlertAfterRedirect("success"; "MyBlog"; "Article saved")
$vo_WebResponse.redirectToRoute("Articles"; $vo_Infos.WEB_vt_Iso_Code)
Else
$vo_WebResponse.sendAlert("error"; "MyBlog"; String($vo_Status.statusText))
End if
Function deletePost($vo_POST : Object; $Entity : Object; $vo_Session : Object; $vo_Infos : Object)->$vo_WebResponse : cs.bspkComponent.WebFormController
$vo_WebResponse:=cs.bspkComponent.WebFormController.new()
If ($Entity#Null)
$Entity.drop()
$vo_WebResponse.sendAlertAfterRedirect("success"; "MyBlog"; "Article deleted")
$vo_WebResponse.redirectToRoute("Articles"; $vo_Infos.WEB_vt_Iso_Code)
End if
What to remember about these functions
- The declaration fits on one line and names its return
$vo_WebResponse. The dev panel only offers a function whose declaration line ends with->$vo_WebResponsefollowed by the type. A function declared: cs.bspkComponent.WebFormController, with no named return variable, does not appear in the list. - The name is camelCase (
savePost, notSavePost): BWEB converts the name to camelCase before looking the function up, and a function that is not found does nothing, with no error. - Four arguments are always passed: the POST, the page's entity (
Entity, or Null), the web session and the request information ($vo_Infos, which holds among othersWEB_vt_Iso_Code, the page language). Declare the ones you need. - The
/* parameters */block describes the parameters the dev panel will show under the event. Here: a list of the page's containers (label "Send all objects in the block") and a hidden parametervt_SendAllObjectsOfBlocNameholding the name of the other parameter,vt_PostForm. This pair is what brings the container's fields into$vo_POST.vt_FieldValue, under each field's name. - Values arrive as text, except a switch's, which is a real boolean.
String()andBool()guard against a missing value. - The answer is a
WebFormController, replayed in the browser after the function ends. A message sent withsendAlertright before a redirect would be lost with the page:sendAlertAfterRedirectshows it on the next page.redirectToRoutetargets the page by its name, not its address.
Make the functions known to the dev panel
The list of functions offered by the dev panel is a registry built at startup. After adding or renaming a function, run the BSPK_REFRESH_STORAGE method (or restart 4D). If you edit the .4dm files outside the 4D editor, reload the code first: see Reload or restart.
6. Wire the buttons
The Save button
- Select the "Save" button and add an
onClickevent to it. - Pick the
POSTclass, then thesavePostfunction. - In the Send all objects in the block parameter, pick the
postFormcontainer.
BWEB checks the required fields of the container sent, in the browser and then on the server: an empty title is reported to the user.
The Delete button
- Add an
onClickevent to the "Delete" button: classPOST, functiondeletePost. - Fill in its confirmation message, for example "Delete this article?". The action only runs if the user confirms; Cancel does nothing.
- In the button's Conditional display section, enter the formula
Entity#Nulland pick the Not loaded effect: on the.../newaddress there is no article yet, and the button is not generated at all.
Conditional display is a 4D formula evaluated by the server at render time, with the page's context (
Entity, the user, process variables). The Hide block effect still sends the block to the browser, hidden; Not loaded does not generate it. Logged in with the right to edit the page, you still see the block, flagged with a badge.
7. Test
- From the Articles page, click New article, fill in the form, tick Published and save: you are back on the list, with the message "Article saved".
- Click a row: the form opens pre-filled. Change the title and save.
- Open an article and delete it, confirming.
The click does nothing? In order: look at the browser's network tab. A 403 answer on
/bweb/call-actionafter the page reloads means the event check refused it (the reason is inbspk.log). Ifvt_FieldValuearrives empty, the "Send all objects in the block" parameter does not point at the right container, or a field has no name. If nothing reaches 4D at all, check the camelCase name and that the class is thePOSTdataclass. Details are in Events and the POST.
Next: Part 3: the public blog.

