UNPKG

@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).

204 lines (203 loc) 5 kB
"use strict"; const bootstrap = ({ strapi }) => { }; const destroy = ({ strapi }) => { }; const register = ({ strapi }) => { strapi.customFields.register({ name: "cta-button", plugin: "hero-widget-cta-button", // This must match the pluginId in admin registration type: "json" }); }; const config = {}; const contentTypes = {}; const controller = ({ strapi }) => ({ index(ctx) { ctx.body = strapi.plugin("hero-widget-cta-button").service("service").getWelcomeMessage(); }, async getCases(ctx) { try { const { search } = ctx.query; const filters = {}; 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) => ({ id: entity.id, documentId: entity.documentId, name: entity.name, slug: entity.slug, caseImage: entity.caseImage })) }; } catch (err) { 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 = {}; 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) => ({ id: entity.id, documentId: entity.documentId, h1title: entity.h1title, slug: entity.slug, mainImage: entity.mainImage, categories: entity.categories })) }; } catch (err) { 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 = {}; 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) => ({ 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) { strapi.log.error("Failed to fetch case categories:", err); ctx.throw(500, `Failed to fetch case categories: ${err.message}`); } } }); const controllers = { controller }; const middlewares = {}; const policies = {}; const contentAPIRoutes = [ { method: "GET", path: "/", // name of the controller file & the method. handler: "controller.index", config: { policies: [] } } ]; const adminAPIRoutes = [ { method: "GET", path: "/cases", handler: "controller.getCases", config: { policies: [], auth: false } }, { method: "GET", path: "/articles", handler: "controller.getArticles", config: { policies: [], auth: false } }, { method: "GET", path: "/case-categories", handler: "controller.getCaseCategories", config: { policies: [], auth: false } } ]; const routes = { "content-api": { type: "content-api", routes: contentAPIRoutes }, admin: { type: "admin", routes: adminAPIRoutes } }; const service = ({ strapi }) => ({ getWelcomeMessage() { return "Welcome to Strapi 🚀"; } }); const services = { service }; const index = { register, bootstrap, destroy, config, controllers, routes, services, contentTypes, policies, middlewares }; module.exports = index;