In previous part, we have created a panel module now we will create a service builder to store the values given from the user and to pass it to theme. Then we will create a theme to get this data and deactivate it from the site.
Creating a service builder (IntelliJ)
To store the state of the portlet on a particular side and to send this information in theme we need a service builder.
To create a service builder this are the following steps:
- Go to File -> New -> Module
2. Update the service.xml file
<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.4.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_4_0.dtd">
<service-builder dependency-injector="ds" package-path="moduleblacklistservice">
<namespace>ModuleBlaclist</namespace>
<!--<entity data-source="sampleDataSource" local-service="true" name="Foo" remote-service="false" session-factory="sampleSessionFactory" table="foo" tx-manager="sampleTransactionManager uuid="true"">-->
<entity local-service="true" name="Portlet" remote-service="true" uuid="true">
<!-- PK fields -->
<column name="id" primary="true" type="long" />
<!-- Group instance -->
<column name="groupId" type="long" />
<!-- Audit fields -->
<column name="companyId" type="long" />
<column name="userId" type="long" />
<column name="userName" type="String" />
<column name="createDate" type="Date" />
<column name="modifiedDate" type="Date" />
<!-- Other fields -->
<column name="portletId" type="String" />
<column name="active" type="boolean" />
<!-- Finder methods -->
<finder name="portletId" return-type="Collection">
<finder-column name="portletId" />
</finder>
<finder name="groupId" return-type="Collection">
<finder-column name="groupId" />
</finder>
<finder name="group_portlet" return-type="Collection">
<finder-column name="groupId" />
<finder-column name="portletId" />
</finder>
</entity>
</service-builder>
3. Update the local service implantation class
package moduleblacklistservice.service.impl;
import com.liferay.portal.aop.AopService;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.OrderByComparator;
import moduleblacklistservice.model.Portlet;
import moduleblacklistservice.service.base.PortletLocalServiceBaseImpl;
import org.osgi.service.component.annotations.Component;
import java.util.Date;
import java.util.List;
@Component(
property = "model.class.name=moduleblacklistservice.model.Portlet",
service = AopService.class
)
public class PortletLocalServiceImpl extends PortletLocalServiceBaseImpl {
public Portlet addPortlet(
long userId, long groupId,String portletId,Boolean active, ServiceContext serviceContext)
throws PortalException {
User user = userLocalService.getUserById(userId);
Date now = new Date();
long id = counterLocalService.increment();
Portlet portlet = portletPersistence.create(id);
portlet.setPortletId(portletId);
portlet.setActive(active);
portlet.setCompanyId(user.getCompanyId());
portlet.setGroupId(groupId);
portlet.setCreateDate(serviceContext.getCreateDate(now));
portlet.setModifiedDate(serviceContext.getModifiedDate(now));
portlet.setUserId(userId);
portlet.setUserName(user.getScreenName());
portlet.setUuid(serviceContext.getUuid());
portlet.setExpandoBridgeAttributes(serviceContext);
portletPersistence.update(portlet);
return portlet;
}
public List<Portlet> getPortletByPortletId(String portletId) {
return portletPersistence.findByportletId(portletId);
}
public List<Portlet> getPortletByGroupId(long groupId) {
return portletPersistence.findBygroupId(groupId);
}
public List<Portlet> getPortlet(long groupId,String portletId) {
return portletPersistence.findBygroup_portlet(groupId,portletId);
}
public List<Portlet> getPortlets(int start, int end) {
return portletPersistence.findAll(start, end);
}
public List<Portlet> getPortlets(int start, int end, OrderByComparator<Portlet> obc) {
return portletPersistence.findAll(start, end,obc);
}
public int getPortletsCount() {
return portletPersistence.countAll();
}
public List<Portlet> getPortlets() {
return portletPersistence.findAll();
}
}
Creating a theme
To generate a theme, you can follow the steps given in link.
After creating theme add the following in portal_normal.ftl file
<div class="deativate-module" style="display: none">
<#list listofPortlets as portlet>
${portlet.portletId} + ${portlet.active?string} + ${portlet.groupId}
<#if portlet.portletId??>
<p class="portletIDs">${portlet.portletId}</p>
</#if>
<#if portlet.active??>
<p class="activated">${portlet.active?string}</p>
</#if>
</#list>
</div>
Add the following in main.js file
var portletIds = document.getElementsByClassName("portletIDs");
var actives = document.getElementsByClassName("activated");
for(var i = 0; i < actives.length; i++) {
if(actives[i].innerText === "false") {
const idToFind = portletIds[i].innerText;
const sections = document.querySelectorAll('section[id*="' + idToFind + '"]');
sections.forEach(function(section) {
section.innerHTML = "<p class='alert alert-danger'>This is deactivated</p>";
});
}
}
Read More : Deactivating and Activating Liferay Objects part 1
After this you would be able to disable a portlet on a particular site. To disable:
- Go to the ModuleBlackListConfiguration tab
- Then go to action and uncheck the active status.
- This will deactivate the module from the site.
We will only be able to deactivate MVC modules which are present in widget panel.
Conclusion
Using this you would be able to disable modules on a particular site without affecting the rest of the site. For code follow this GitHub links:
https://github.com/brijesh-2203/Liferay7.4u42/tree/master/themes/module-based-theme
https://github.com/brijesh-2203/Liferay7.4u42/tree/master/modules/ModuleBlacklistService
https://github.com/brijesh-2203/Liferay7.4u42/tree/master/modules/ModuleBlacklistConfiguration