@inspirer-dev/hero-widget-cta-button
Version:
A custom field plugin for Strapi v5 that provides a CTA button selector with different action types (external link, internal link, case, article, copy link, copy nickname).
133 lines (118 loc) • 3.69 kB
text/typescript
import type { Core } from '@strapi/strapi';
const controller = ({ strapi }: { strapi: Core.Strapi }) => ({
index(ctx) {
ctx.body = strapi
.plugin('hero-widget-cta-button')
// the name of the service file & the method.
.service('service')
.getWelcomeMessage();
},
async getCases(ctx) {
try {
const { search } = ctx.query;
const filters: any = {};
if (search) {
filters.$or = [{ name: { $containsi: search } }, { slug: { $containsi: search } }];
}
const entities = await strapi.entityService.findMany('api::case.case', {
fields: ['id', 'documentId', 'name', 'slug'],
populate: {
caseImage: {
fields: ['id', 'documentId', 'name', 'url', 'alternativeText'],
},
},
filters,
pagination: {
pageSize: 100,
},
});
ctx.body = {
results: entities.map((entity: any) => ({
id: entity.id,
documentId: entity.documentId,
name: entity.name,
slug: entity.slug,
caseImage: entity.caseImage,
})),
};
} catch (err: any) {
strapi.log.error('Failed to fetch cases:', err);
ctx.throw(500, `Failed to fetch cases: ${err.message}`);
}
},
async getArticles(ctx) {
try {
const { search } = ctx.query;
const filters: any = {};
if (search) {
filters.$or = [{ h1title: { $containsi: search } }, { slug: { $containsi: search } }];
}
const entities = await strapi.entityService.findMany('api::article.article', {
fields: ['id', 'documentId', 'h1title', 'slug'],
populate: {
mainImage: {
fields: ['id', 'documentId', 'name', 'url', 'alternativeText'],
},
categories: {
fields: ['id', 'documentId', 'name', 'slug'],
},
},
filters,
pagination: {
pageSize: 100,
},
});
ctx.body = {
results: entities.map((entity: any) => ({
id: entity.id,
documentId: entity.documentId,
h1title: entity.h1title,
slug: entity.slug,
mainImage: entity.mainImage,
categories: entity.categories,
})),
};
} catch (err: any) {
strapi.log.error('Failed to fetch articles:', err);
ctx.throw(500, `Failed to fetch articles: ${err.message}`);
}
},
async getCaseCategories(ctx) {
try {
const { search } = ctx.query;
const filters: any = {};
if (search) {
filters.$or = [{ name: { $containsi: search } }];
}
const entities = await strapi.entityService.findMany('api::case-category.case-category', {
fields: ['id', 'documentId', 'name', 'sortWeight', 'shortDescription', 'isFree', 'isFarm'],
populate: {
icon: {
fields: ['id', 'documentId', 'name', 'url', 'alternativeText'],
},
},
filters,
sort: { sortWeight: 'asc' },
pagination: {
pageSize: 100,
},
});
ctx.body = {
results: entities.map((entity: any) => ({
id: entity.id,
documentId: entity.documentId,
name: entity.name,
sortWeight: entity.sortWeight,
shortDescription: entity.shortDescription,
isFree: entity.isFree,
isFarm: entity.isFarm,
icon: entity.icon,
})),
};
} catch (err: any) {
strapi.log.error('Failed to fetch case categories:', err);
ctx.throw(500, `Failed to fetch case categories: ${err.message}`);
}
},
});
export default controller;