Context contributor provides functionality to inject variables in the Liferay theme. It is able to add variables in header of Liferay Theme. In this example, we will add to our website URL by using Context Contributor in Liferay 7.2.

Prerequisites 

  • Java 
  • Liferay portal 7/7.x

Environment Requirement 

  • JDK 8
  • Eclipse
  • MySQL

1. Create context contributor class in your module project.

  1. Go to Liferay workspace project modules new.
  2. Select other Liferay Liferay Module Project and click on “Next”.
  3. Enter the project name.
  4. Select “Project Template Name” as “template-context-contributor” and click on “Next”.
Context-Contributor-in-Liferay-7.2-1
  1. Enter the package name and click on “Finish”. The necessary file structure will be created as below.
Context-Contributor-in-Liferay-7.2-2

2. Insert the following annotation into above the class declaration.

There are two types of contributors.

  • TYPE_THEME field should be set if you only wish to inject context-specific variables for your theme.
  • TYPE_GLOBAL field affects every context execution in Liferay, like themes, ADTs, DDM templates, etc.

Here, we will add to our website URL.

  • Start the Liferay server (if the server is already running then ignore it).
  • Deploy your module

3. If you want to see the changes, follow the steps below.

1. Create structure in Liferay site.
  1. Go to Liferay site content & data and click on Web Content.
  2. Go to Structure and click on the Add(+) button.
  3. Enter the structure name and drag & drop Text Box from the fields.
Context-Contributor-in-Liferay-7.2
  1. Click on the Text Box and go to the Settings.
  2. Change Field Label value to Content and save it.
Context-Contributor-in-Liferay-7.2
2. Create a template for our structure.
  1. Go to our structure and click on the ellipsis icon Manage Templates.
  2. Go to Manage Templates and click on the Add(+) button.
  3. Enter the template name Add below lines in the code section and save it.

${siteURL} 

</br>

${content.getData()}

Context-Contributor-in-Liferay-7.2
3. Create a Web Content.
  1. Goto Web Content and click on the Add(+) button.
  2. Now you can see your structure name and click on it.
  3. Enter the Web Content name and click on publish.
Context-Contributor-in-Liferay-7.2
4. Now, fetch the web content in your Home page.
  1. Go to Liferay site click on the Add(+) button.
  2. Go to Widgets Content Management click on Web Content Display.
  3. Now the screen will look like the image below.
Context-Contributor-in-Liferay-7.2
  1. Click on “Select Web Content to make it visible” select your web content.
  2. After that, you can see Configuration pop-up select “Use a specific template” select your template and save it.
  3. Now, you can see your changes on the Home page. 

Output

Context-Contributor-in-Liferay-7.2