Custom pages

Contents

Content types are nice, but letting the editors compose their own pages is even better.

Intro

By defining a page, editors may drag’n drop cms components into the page, visually.

Set up page rendering

Content types are required before an editor can create content. Similarly, pages components must be defined before they can create pages.

  1. Add the component files:

/src/main/resources/react4xp/components/page/Page.tsx
import {Regions,} from '@enonic/react-components';
import React from 'react'

export const Page = (props: any) => {

    return (
            <Regions {...props} />
    );
};

+ ./src/main/resources/react4xp/components/page/PageProcessor.ts

import {PageComponent} from "@enonic-types/core";
import type {ComponentProcessorFunction} from '@enonic-types/lib-react4xp/DataFetcher';

export const pageProcessor: ComponentProcessorFunction<'com.enonic.app.hmdb:main'> = (props) => {
    const component = props.component as PageComponent;

    const regions = component?.regions || {};

    return {
        regions,
        name: "main"
    };
};

+ ./src/main/resources/react4xp/components/page/Page.module.css

.page {
  padding-bottom: 0;
  margin-bottom: 0;
  display: flow-root;
}
  1. Register the processor and component:

    /src/main/resources/react4xp/dataFetcher.ts
    import {DataFetcher} from '/lib/enonic/react4xp';
    import {commonProcessor} from '/react4xp/components/common/CommonProcessor';
    //import {helloProcessor} from './components/hello/HelloProcessor';
    import {personProcessor} from './components/content/PersonProcessor';
    import {pageProcessor} from './components/page/PageProcessor';
    
    export const dataFetcher = new DataFetcher();
    
    //dataFetcher.addContentType('portal:site', {processor: helloProcessor});
    dataFetcher.addCommon({processor: commonProcessor});
    dataFetcher.addContentType('com.enonic.app.hmdb:person', {processor: personProcessor});
    dataFetcher.addPage('com.enonic.app.hmdb:main', {processor: pageProcessor});
    /src/main/resources/componentRegistry.tsx
    //import {Hello} from './components/hello/Hello';
    import {Factbox} from '/react4xp/components/macro/FactBox';
    import {Page} from '/react4xp/components/page/Page';
    import {ComponentRegistry} from '@enonic/react-components';
    import {Person} from './components/content/Person';
    
    export const componentRegistry = new ComponentRegistry();
    
    //componentRegistry.addContentType('portal:site', {View: Hello});
    componentRegistry.addContentType('com.enonic.app.hmdb:person', {View: Person});
    componentRegistry.addMacro('factbox', {View: Factbox});
    componentRegistry.addPage('com.enonic.app.hmdb:main', {View: Page});
    We also leave "Hello React4xp" behind, by commenting it out

Working with pages

As it turns out, HMDB already contains data for pages, and by reloading your front-page, you should immediately see this:

The text component

The reason why you can see the righ text content on the page, is that Content Studio ships with a Rich text component that can be placed on pages. React4XP also implements a default rendering of this.

Try editing the page, to see how this works.

TODO

Movies

TODO: Verify

Visiting the Movies page, you will components that are missing rendering. Content Studio has a built-in fallback handling for thse, so you can interact with them, even if they are not yet rendering.

TODO

We’ll deal with some of these in the parts chapter.


Contents

Contents

AI-powered search

Juke AI