Mastering Liferay Design Elements

Course Overview

Creating Clarity's Custom Form Fragment

Liferay fragments are highly customizable web elements with dynamic functionality and styling. Clarity plans to leverage fragments to enhance their Contact Us form with a file uploader field for gathering additional user information. In these exercises, you'll create a file uploader fragment and add it to Clarity's Contact Us form.

Exercise: Creating the Advanced File Uploader Fragment

Here, you'll build a file uploader fragment that displays a loading screen upon file selection.

  1. Open the Site Menu (Site Menu), expand Design, and click Fragments.

  2. Under Fragment Sets, click Clarity Components.

  3. Click New, select Form Fragment, and click Next.

    Create a new form fragment.

  4. For Name, enter Advanced File Uploader.

  5. For Supported Field Types, check the File checkbox.
    This determines the type of field you can map to the fragment.

  6. Click Add.

    Click Add to finish creating the Advanced File Uploader form fragment.

    This redirects you to the fragment editor menu.

  7. In your course workspace, navigate to the exercises/module-3/file-uploader-fragment-code/ folder and open the fragment.html file in a text editor or IDE.

  8. Copy and paste the contents of this file into the HTML window.

    This HTML code contains two main elements: the file uploader component and the loading screen template.

  9. Copy and paste the contents of the fragment-style.css file into the CSS window.
    This hides the default HTML input element and styles the file uploader button.

  10. Copy and paste the contents of the fragment-functionality.js file into the JavaScript window.
    This implements the custom upload functionality and triggers the loading screen.

  11. Click Publish.

    Publish the Advanced File Uploader form fragment.

Great! You've created a file uploader fragment. Now you can add it to Clarity's pages.

Exercise: Adding the Fragment to Clarity's Form

Here you'll add the Advanced File Uploader fragment to Clarity's Contact Us form and map it to the object's Shared Files field.

  1. Open the Site Menu (Site Menu), expand Design, and click Page Templates.

  2. Go to the Display Page Templates tab and select the Contact Us - Step 2 template.

    Go to the Display Page Templates tab and select the Contact Us - Step 2 template.

  3. Open the Fragments and Widgets tab in the left side panel.

  4. Drag and drop the Advanced File Uploader fragment below the Comment field.

    Drag and drop the Advanced File Uploader fragment below the Comment field.

  5. Select the fragment and configure these settings in the configuration side panel:

    Tab Setting Value
    General Field Shared Files
    Styles Spacing > Padding Bottom Spacer 3 (1rem)
  6. Click Publish.
    With the new form field set up, you're ready to test it.

  7. Go to the Contact Us page in Clarity's website.

  8. Test the form by submitting an entry with an attachment.

Great! Now users can use the custom form fragment to upload files. Next, you'll use FreeMarker to make the fragment more dynamic.

Exercise: Adding Dynamic Functionality to the Fragment

Here, you'll improve the Advanced File Uploader fragment by enabling users to dynamically control its help text visibility and content.

  1. Open the Site Menu (Site Menu), expand Design, and select Fragments.

  2. Begin editing the Advanced File Uploader form fragment.

  3. In the HTML window, locate this <p> element:

    <p class="mb-0 text-secondary text-center" id="${fragmentEntryLinkNamespace}-file-upload-help-text">Accepted File Extensions: .jpeg,.jpg,.pdf,.png</p>
    
  4. Replace the <p> element with this code:

    
    [#if (input.showHelpText && input.helpText?has_content)]
    	<p class="mb-0 text-secondary text-center" id="${fragmentEntryLinkNamespace}-file-upload-help-text">${input.helpText}</p>
    [/#if]

    This Freemarker conditional statement dynamically toggles the <p> element's visibility, displaying it only if the fragment's help text is enabled and contains content. Additionally, the inline ${input.helpText} code populates the fragment's content with the value entered in the help text field.

  5. Click Publish.
    You can now dynamically control the fragment's help text. Before testing it, you'll propagate these changes to all pages that use the fragment.

  6. Click Actions (Actions) for the Advanced File Uploader fragment and select View Usages.

    Click Actions for the Advanced File Uploader fragment and select View Usages.

  7. Check all items in the list and click Propagate.

    Check all items in the list and click Propagate.

    With all pages utilizing the fragment updated, you can now test the new functionality.

    NOTE
    By default, changes made to a custom fragment need to be manually propagated to all pages that use the fragment. You can configure your instance to propagate fragment changes automatically. However, you should only enable this feature in testing and development environments, since automatic propagation can lead to errors being published. See Propagating Fragment Changes for more information.
  8. Open the Site Menu (Site Menu), expand Design, and click Page Templates.

  9. Go to the Display Page Templates tab and select the Contact Us - Step 2 template.

  10. Select the Advanced File Uploader fragment and configure these settings:

    Tab Setting Value
    General Show Help Text Enabled
    General Help Text Only upload .jpeg, .jpg, .pdf, and .png files.
     

    Select the Advanced File Uploader fragment and configure these settings.

  11. Click Publish.

Great! You've used Freemarker to dynamically control the visibility of the fragment's help text. Next, you'll add new configuration options to the fragment for greater flexibility.

Exercise: Adding Fragment Configuration Options

Here, you'll add configuration options to the Advanced File Uploader fragment for modifying the text of the file selection button and loading screen.

  1. Go to the Fragments application in the Site Menu (Site Menu) and start editing the Advanced File Uploader form fragment.

  2. Go to the Configuration tab.
    This page contains the fragment's supported field type settings and the custom JSON configuration.

  3. From the exercises/module-3/file-uploader-fragment-code/ folder in your course workspace, copy and paste the contents of the fragment-configuration.json file into the JSON window.

    Copy and paste the contents of the file into the JSON window.

    This adds three configuration options for modifying the upload button text, loading screen title, and help text. You can now reference these options in the fragment's code.

  4. Go to the Code tab.

  5. In the HTML window, locate this <button> element:

    <button class="btn btn-outline-primary text-nowrap" id="${fragmentEntryLinkNamespace}-file-upload-button" type="button">[@clay.icon className="text-primary" symbol="upload" /] Browse File</button>
    
  6. Within the <button> element, replace Browse File with ${configuration.buttonText}.
    This retrieves the buttonText field's value and displays it as the uploader button's text.

    NOTE
    When replacing the content, be sure to preserve the @clay.icon element, as it's required for displaying the button's upload icon.
  7. Ensure the element resembles this:

    <button class="btn btn-outline-primary text-nowrap" id="${fragmentEntryLinkNamespace}-file-upload-button" type="button">[@clay.icon className="text-primary" symbol="upload" /] ${configuration.buttonText}</button>
    
  8. Locate this <h4> element:

    <h4 class="font-weight-normal mb-0">Upload may take a few seconds</h4>
    
  9. Replace Upload may take a few seconds with ${configuration.loadingTitle}.
    This retrieves the loadingTitle field's value and displays it as the loading screen's title.

  10. Find this <p> element:

    <p class="mb-0">Please keep the window open</p>
    
  11. Replace Please keep the window open with ${configuration.loadingHelpText}.
    This displays the loadingHelpText field's value as the loading screen help text.

  12. Both elements should resemble this:

    <h4 class="font-weight-normal mb-0">${configuration.loadingTitle}</h4>
    <p class="mb-0">${configuration.loadingHelpText}</p>
  13. Click Publish.

  14. Propagate the fragment's changes to all uses.

Now that you've implemented configuration options for the fragment, you can test them.

Exercise: Testing the Fragment's Custom Configuration Options

Here, you'll test your custom configuration options for the Advanced File Uploader fragment.

  1. Open the Site Menu (Site Menu), expand Design, and click Page templates.

  2. Go to the Display Page Templates tab and select the Contact Us - Step 2 template.

  3. Select the Advanced File Uploader fragment.

  4. Notice there are two new options in the configuration side panel: Upload Options and Loading Options.

  5. Configure these settings for the fragment:

    Tab Setting Value
    General Button Text Select a File
    General Loading Title Uploading File...
    General Loading Help Text This won't take long
     

    Configure these settings for the fragment.

  6. Click Publish.

  7. Go to the Contact Us page in Clarity's website.

  8. Test the form by submitting an entry with an attachment.

    Test the form by submitting an entry with an attachment.

With this, you've implemented new configuration options for customizing the fragment's display text.

Conclusion

Through these exercises, you've successfully created and customized a file uploader fragment, enhancing Clarity's Contact Us form. By adding custom configuration options, you've improved the form's usability while demonstrating the flexibility and power of fragments in tailoring web experiences.

Next, you'll learn about fragment compositions and migrating fragments between Liferay environments.

  • Exercise: Creating the Advanced File Uploader Fragment

  • Exercise: Adding the Fragment to Clarity's Form

  • Exercise: Adding Dynamic Functionality to the Fragment

  • Exercise: Adding Fragment Configuration Options

  • Exercise: Testing the Fragment's Custom Configuration Options

  • Conclusion

Loading Knowledge

Capabilities

Product

Education

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy