...
...
Custom Media Items and the Is Used in Widget
Anchor |
---|
| custom_media_items_and_the_is_used_in_widget |
---|
| custom_media_items_and_the_is_used_in_widget |
---|
|
Panel |
---|
borderColor | #0081C0 |
---|
titleColor | #0081C0 |
---|
|
The following applies to XperienCentral versions 10.22.0 and higher. |
The Is Used In widget shows an overview of content items that are using the currently selected content item. Before XperienCentral 10.22.0, the usage of content items referring to a custom content type was not recorded. I.e., for content items from a custom content type, the Is-Used-In widget would always show a count of 0 used content items. Now, the API of several content items in XC is extended with a method for implementing this used-in relationship for custom content types. This functionality will be available via the new abstract method:
Code Block |
---|
|
ContentItem<?>[] getReferencedContentItems(); |
For all custom content items that implement or extend one of the following interfaces and classes:
Element
ElementImpl
MediaItemVersion
MediaItemVersionImpl
TemporaryRichTextInlineElement
The result of this method is the list of used content items (as defined by the projects requirements). The following is a code snippet from a custom content type (extending MediaItemVersion
) which implements the method getReferencedContentItems()
.
Code Block |
---|
|
@Override
@Property
public String getReferencedContentIds() {
return JcrUtil.getString(getPrivateNode(), WCBConstants.NAMESPACE_PREFIX + ":referencedcontentids");
}
@Override
public void setReferencedContentIds(String contentids) {
JcrUtil.setString(getPrivateNode(), WCBConstants.NAMESPACE_PREFIX + ":referencedcontentids", contentids);
}
public ContentItem<?>[] getReferencedContentItems() { List<ContentItem<?>> result = new ArrayList<ContentItem<?>>();
if (getReferencedContentIds() != null && !"".equals(getReferencedContentIds())) {
Session session = getSessionManager().getActiveSession();
if (session != null) {
for (String contentid : getReferencedContentIds().split(",")) {
try {
result.add((MediaItem) session.getWrapper(new Integer(contentid).intValue(), MediaItem.class));
} catch (NumberFormatException e) {}
}
}
}
return result.toArray(new ContentItem[result.size()]);
} |
Use in Page Metadata
It is also possible to define this method for custom page metadata. The method must be added to the page metadata class. Additionally, an adapter must be implemented that supplies the result of the method to the indexer service. The following are examples of implementations of both the method definition and the adapter:
Method Implementation
Code Block |
---|
|
/**
* Returns the content items that this element refers to.
*
* @return List of referred content items, or <code>null</code> if no content item is referenced.
*/
@ReferField(stored = false, indexed = true, adapter = ContentReferenceFieldAdapter.class) ContentItem<?>[] getReferencedContentItems(); |
Adapter Implementation
Code Block |
---|
|
package com.gxwebmanager.helloworld.helloworldpagemetadata.pagemetadata;
import java.util.Collections;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.gx.webmanager.cms.core.RelatedDownloadLink;
import nl.gx.webmanager.cms.core.RelatedInternalLink;
import nl.gx.webmanager.cms.core.RelatedLink;
import nl.gx.webmanager.cms.core.RelatedMediaItemLink;
import nl.gx.webmanager.cms.core.RelatedResourceLink;
import nl.gx.webmanager.cms.mediarepository.MediaItemDownloadVersion;
import nl.gx.webmanager.services.contentdomain.api.ContentDomainException;
import nl.gx.webmanager.services.contentdomain.api.ContentDomainResolver;
import nl.gx.webmanager.services.contentindex.adapter.FieldAdapter;
import nl.gx.webmanager.services.framework.spi.FrameworkException;
import nl.gx.webmanager.services.framework.spi.FrameworkFactory;
/**
* Adapter that takes a content item, and returns a String-reference to it, e.g. page-32423. Logs
* a warning and returns null if the given item could not be resolved.
*
* @see ContentDomainResolver
*/
public class ContentReferenceFieldAdapter implements FieldAdapter<Object> {
private static final Logger LOG = Logger.getLogger(ContentReferenceFieldAdapter.class.getName());
@Override
public boolean isLanguageSpecific() {
return false;
}
@Override
public Object adapt(Object item, Locale forLocale) {
if (item == null) {
return null;
}
if (item instanceof RelatedLink) {
return adaptLinkReference(item, forLocale);
}
return getItemId(item);
}
private String getItemId(Object item) {
try {
ContentDomainResolver resolver = (ContentDomainResolver) FrameworkFactory.getInstance().getFramework().getService(ContentDomainResolver.class.getName());
return resolver.entityToId(item);
} catch (FrameworkException | ContentDomainException e) {
LOG.log(Level.WARNING, "Failed to retrieve content reference for " + item, e);
}
return null;
}
private Object adaptLinkReference(Object link, Locale forLocale) {
if (link instanceof RelatedInternalLink) {
return adapt(((RelatedInternalLink) link).getPage(), forLocale);
} else if (link instanceof RelatedMediaItemLink) {
return adapt(((RelatedMediaItemLink) link).getMediaItem(), forLocale);
} else if (link instanceof RelatedDownloadLink) {
MediaItemDownloadVersion downloadVersion = ((RelatedDownloadLink) link).getInContextDownloadMediaItemDownloadVersion();
if (downloadVersion != null) {
String itemId = getItemId(downloadVersion.getContentItem());
if (itemId != null) {
return Collections.singletonMap("download", itemId);
}
}
} else if (link instanceof RelatedResourceLink) {
return adapt(((RelatedResourceLink) link).getResourceInstance(), forLocale);
}
return null;
}
} |
...
Credentials Service Provider
...