Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document how to add craft singles to index #357

Open
npegman opened this issue Jan 23, 2025 · 3 comments
Open

document how to add craft singles to index #357

npegman opened this issue Jan 23, 2025 · 3 comments

Comments

@npegman
Copy link

npegman commented Jan 23, 2025

Is there a way to add Craft 'singles' to an algolia index? Did I miss this in the documentation?

@janhenckens
Copy link
Member

Hey @npegman

Adding singles works the same as any other section - Craft allows us to pass an array of handles to query->section, like so: query->section(['sectionHandle', 'sectionHandle'])

Then in your transformer you can check the element's section and parse the fields per section/single/channel

Does that help?

@npegman
Copy link
Author

npegman commented Jan 24, 2025

There's no way to do it dynamically? I knew about the hardcoded method you describe above but trying to avoid it to eliminate the need to have to add/remove new singles. Craft must have some means of querying for all singles as they list them in the cp.

@janhenckens
Copy link
Member

You can of course query all singles, parse those and pass them to the query. For example (for Craft 5):

$sections = Craft::$app->getEntries()->getSectionsByType('single');
$handles = collect($sections)->map(function ($section) {
    return $section->handle;
})->toArray();
query->section($handles);

You can of course then also merge that array with a different one of different sections

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants