Content and block
Content
Recovering the page content
At any moment,you may recover the page content. To date, a page may contain two versions maximum:
- approved version available for the website users;
- draft version available for the page designer.
Eventually other versions will complete this list (ex.archived version).
The following example shows how to recover a specific version of a page.
public void page_getPageContent( ISiteModule siteModule, IContext context, ISite site ) throws SiteModuleException {
IPageContainer pageContainer = siteModule.getPageContainer( context, site, "my-lovely-page" );
// 1st case: recover the draft version of a page
IContent draftContent = pageContainer.getContent( IContent.IStatus.DRAFT );
// 1st case: recover the approved version of a page
IContent approuvedContent = pageContainer.getContent( IContent.IStatus.APPROVED );
}
Block
Recovering the main block of a page content
The following example shows how to recover the root block of a page. By using this block, or any other tree element, you may achieve treatments such as:
- the add or the construction of components or sub-blocks;
- the hide of tree elements.
public void page_getPageContentBlock( ISiteModule siteModule, IContext context, ISite site ) throws SiteModuleException {
IContent content = null;
// retrieving the main block of a version
IBlock block = content.getBlock();
System.out.println( content.getOwner().getFullName() );
}
Adding components to a page block
From an extension page, you may add components dynamically. This example shows how to use “the components work” to add a title level 1 to the page root block.
public void page_completeBlockWithComponents( ISiteModule siteModule, IContext context, ISite site ) throws SiteModuleException {
// recover the page version
IContent content = null;
// use the components work to get a title component
HeadingComponent headingComponent = siteModule.getComponentsFactory().newHeadingComponent();
headingComponent.setContent( "Hello everyone!!!" );
headingComponent.setLevel( "1" );
// add the component to the main block of the page
content.getBlock().addComponent( headingComponent );
}
Adding blocks to a page block
From an extension page, you may add blocks and sub-blocks dynamically. This example shows how to use “the components work” to add a site form to the page root block.
public void page_completeBlockWithBlocks( ISiteModule siteModule, IContext context, ISite site ) throws SiteModuleException {
// recover the page version
IContent content = null;
// use the blocks work to get a form-type block
FormBlock formBlock = siteModule.getBlocksFactory().newFormBlock();
formBlock.setName( "frmContacts" );
formBlock.setLabel( "Les contacts" );
// create a fields block
FieldsetBlock fieldsetBlock = siteModule.getBlocksFactory().newFieldsetBlock();
// create two text-type fields
TextInputComponent firstName = siteModule.getComponentsFactory().newTextInputComponent();
firstName.setLabel( "First name" );
firstName.setName( "fldFirstName" );
TextInputComponent lastName = siteModule.getComponentsFactory().newTextInputComponent();
lastName.setLabel( "Last name" );
lastName.setName( "fldLastName" );
// add the fields to the block
fieldsetBlock.addComponent( firstName );
fieldsetBlock.addComponent( lastName );
// create two form validation buttons
ButtonInputComponent createButton = siteModule.getComponentsFactory().newButtonInputComponent();
createButton.setLabel( "Create" );
createButton.setValue( "type", ButtonInputComponent.Action.SAVEANDCLOSE );
ButtonInputComponent resetButton = siteModule.getComponentsFactory().newButtonInputComponent();
resetButton.setLabel( "Reset" );
resetButton.setValue( "type", ButtonInputComponent.Action.RESET );
// add the fields block to the form
formBlock.addBlock( fieldsetBlock );
// add the buttons to the form block
formBlock.addComponent( createButton );
formBlock.addComponent( resetButton );
// add the form block to the main block of the page
content.getBlock().addBlock( formBlock );
// save the modifications (transactional context needed)
content.save( context );
}